gpt4 book ai didi

c - 数组中元素的数量

转载 作者:太空宇宙 更新时间:2023-11-04 07:09:51 25 4
gpt4 key购买 nike

谁能帮我打印一个循环计算出的结果个数?这是我的代码:

#include <stdio.h>
int
main (void)

{

int monthly_water_arr[30];
int x,num_months;
int count_0=0;
FILE *in;
in = fopen("water.txt","r");
x=0;
while (!feof(in))
{
fscanf(in,"%d",&monthly_water_arr[x]);
x++;
;
}
printf("count is %d\n", count_0);
printf(" Numbers read from file are:\n");
for (x=0;x<30;x++){
printf("%d\n",monthly_water_arr[x]);
}
num_months = sizeof(monthly_water_arr)/ sizeof(monthly_water_arr[0]);
printf("The number of months in file are: %d\n",num_months);
for (x=0;x<30;x++){
if(monthly_water_arr[x]>=71 && monthly_water_arr[x]<=80);
count_0=count_0+1
}

return (0);
}

count_0 应该给出两个,但我已经尝试了很多,实际上只有 2 个数字在 71 和 80 之间,每次我只得到 30

最佳答案

你放错了分号:

if(monthly_water_arr[x]>=71 && monthly_water_arr[x]<=80);
count_0=count_0+1

注意 if 后面的分号。这实际上更准确地写为:

if(monthly_water_arr[x]>=71 && monthly_water_arr[x]<=80) //If this,
; //Do nothing.
count_0=count_0+1 //Either way, increment count.

不要在 ifforwhile 语句后放置分号。删除它,使计数增量在 if 内:

if(monthly_water_arr[x]>=71 && monthly_water_arr[x]<=80)
count_0=count_0+1; //or, more concisely, ++count_0;

关于c - 数组中元素的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29312854/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com