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;
}
|