I'm having trouble scanning values from a text file, I've never really used file i/o before and I'm still very shaky with user-defined functions so I'm almost positive that the error is in the first function, I don't know how to fix it however, I'm pretty new at programming so please bear with me.
my code so far is
#include <stdio.h>
#include <stdlib.h>
int read_temps(void)
{
int i;
int num;
int fileArray[25];
FILE *file;
file=fopen("temperatures.txt", "r");
if (file == NULL)
{
printf("Error Reading File\n");
exit(0);
}
for (i=0; i<25; i++)
{
fscanf(file,"%d", &fileArray[i]);
}
fclose(file);
return fileArray[i];
}
int calc_results (int a[], int n)
{
int i, j, maximum, minimum, average,sum;
float avg;
printf ("Temperature conditions on October 26th, 2015:\n");
printf("Time Temperature in Degrees F \n");
for(i = 0; i<25;i++)
{
printf("%d\t%d\n", i, a[i]);
if(maximum < a[i])
maximum = a[i];
if(minimum > a[i])
minimum = a[i];
sum+=a[i];
}
printf("Maximum Temperature for the day: %d\n", maximum);
printf("Minimum Temperature for the day: %d\n", minimum);
avg=(float)sum/25;
printf("Average Temperature for the day: %f\n", avg);
return 0;
}
int main ()
{
int average,i;
int temp[25];
for (i=0;i<25;i++)
temp[i]= read_temps(); //calling user function for file input
calc_results(temp,i); //calling user function
return 0;
}
the program is supposed to read the values from the .txt file in read_temps, calculate the max, min, and average value in calc_results. It does all this and compiles, but it doesnt read the values correctly. The .txt file contains numbers like 87, 65, 92 and when I run my program the output is the same large number like 19863578 repeated for almost all the values, expect for max, min and avg. I don't know what to do :(
these are the values on the textfil
67
67
69
71
77
79
80
80
82
85
87
88
91
93
94
95
95
96
94
90
85
82
81
77
74
the output should be
Temperature Conditions on October 9, 2015:
Time of Day Temperature in degrees F
0 85
1 80
2 97
3 90
4 73
........
25 68