中易网

数组里面判断里面的所有个元素有多少个

答案:1  悬赏:0  
解决时间 2021-01-15 11:33
  • 提问者网友:戎马万世
  • 2021-01-15 07:53
数组里面判断里面的所有个元素有多少个
最佳答案
  • 二级知识专家网友:詩光轨車
  • 2021-01-15 08:38
int str[100];
gets(str); -- 错,gets 用于 char str[100]; 不能 int 型。
===============
一个数组x[]中 有多少个元素:
int x[]={1,2,3,4,5,6,7,8,9,10};
n = sizeof(x) / sizeof(int);
================
控制台输入 2 5 6 7回车 输出结果4(输入了4个数) ,用下法:
#include
int main()
{
int x[]={1,2,3,4,5,6,7,8,9,10};
int i, n,c;
n = sizeof(x) / sizeof(int);
printf("There are %d elem in the array\n",n);
printf("\nintput data\n");
n=0;
while(1){
if ( scanf("%d",&x[n]) ==1) n++; // 统计个数
c = getc(stdin);
if (c=='\n') break; else ungetc(c,stdin); // 判断是否输入结束符 \n到达
}
printf("you typed %d data:\n",n);
for (i=0;iprintf("%d ",x[i]);
return 0;
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息!
大家都在看
推荐信息