数组中的元素数

时间:2015-03-28 02:04:38

标签: c arrays loops

任何人都可以帮我如何打印循环计算的结果数量吗? 这是我的代码:

#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

1 个答案:

答案 0 :(得分:3)

你错了分号:

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.

请勿在{{1​​}},iffor语句后放置分号。删除它,使计数增量在if:

while