C array and division help

So, I wrote a program to take user input for a high and low temperature, and then took a file name from that user. I then used an array to read the data and store the relevent data, the amount of total days, and then the amount of days that fall within a range. I am now trying to divide the range by the total to find an average, so I can then print out that average to the user. This is in the end of the code. For some reason I can not seem to understand, the program is not dividing properly. I have checked the output, and the program is reading in the file correctly. The segment is towards the bottom of the code, but i am posting it all here for reference. Thanks for any help and advice.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71

//Libraries
#include <stdio.h>


int main(){
    //Declare file input
       FILE * ifp = NULL;
    //Declare Strings
        char filename[30];
    //Define Variables
        int i;
        int month, day, year;
        float temp, cold, hot;


    //Ask User Input
        printf("Tell me about your dragons preferred temperature for flying.\n");
        printf("What is the coldest temperature they can fly in?\n");
        scanf("%f", &cold);
        printf("What is the hottest temperature they can fly in?\n");
        scanf("%f", &hot);

    //Begin filename while loop
    while(ifp == NULL){
        int range[12] = { 0 };
        int total[12] = { 0 };
        float average;
        //Ask user input
        printf("Please enter the name of the weather data file for Dragon Island.\n");
        scanf("%s", filename);

        ifp = fopen(filename, "r");

            fscanf(ifp, "%d", &month); // Sentinel in this case is month
                    while (month!=-1){

                            fscanf(ifp, "%d", &day);
                            fscanf(ifp, "%d", &year);
                            fscanf(ifp, "%f", &temp);

                            if (temp >= cold && temp <= hot){
                                range[month - 1] += 1;

                            } //end temp if loop

                            total[month - 1] += 1; //counter if temp does not fall into range

                            fscanf(ifp, "%d" , &month);



                    } //end Sentinel while loop

                            //calculate best month
                    for(i = 0; i <12; i++){
                            average = range[i] / total[i];
                        //calculate best month
                            //average = range [12] / total [12];
                            printf("Month %d: %.2f percent of days are in range.\n", i +1, average);

                                }//end for loop

                    // Print out the best month

    }//end filename while loop
            fclose(ifp);

    return 0;
}
Hi,

You are doing integer division: both range and total are int

So you need to cast at least one of them to double (preferred ) or float.

Some other things to help you out:

Always initialise your variables to something, note zero is not always a good choice, sometimes 1 or else something wildly invalid is better. Always initialise, even if you are setting that value on the next line.

When using any of the scanf's, make use of it's return value to see if it worked.

When opening a file, see that it worked as well.

Good luck !!
Topic archived. No new replies allowed.