Hi all.. wonder if anyone can guide me. I am writing a program which reads values from a text file, the text file includes both characters and float values, below is a sample of the data..
London 7.24 8.15 6.45 3.24 3.66 2.45 4.71 6.78 6.45 8.61 7.45 6.55
The text file (attached) is supposed to hold a maximum of 12 records and is terminated by the value ZZZZZ - so basically when the compiler see's ZZZZZ it will stop reading the file. Each row needs to have a city name and 12 float values to represent January to December
the data is then put into an array - and a total for individual row is a calculated and an average for individual column should also be calculated - i have got the program reading the values in and doing the row total, so im happy with that, but the average only gives the correct value if there are 12 entries - so i need to know how to get it automatically divide by the number of entries and not by 12 each time...
also i need to do comparison which displays the name of the town and then displays the month with the highest value and the month with the lowest value - i cant figure out how to incorporate the month names into the program and how to do the comparison. I have pasted the code i have so far - so if anyone can help it will be greatly appreciated
#include <string.h>
const int maxrows=12;
const int maxcols=12;
const int maxchar=10;
static char place[maxrows][maxchar];
static char dummy[maxchar];
int month;
float rain[maxrows][maxcols];
float average, annual, wet, dry;
FILE *fp;
main ()
{
int i=0;
if ((fp = fopen("data.txt", "r"))==NULL)
printf("Error opening file\n");
else
{
do
{
printf("\n\n");
fscanf(fp,"%s",&dummy);
if (strcmp(dummy, "ZZZZZ") !=0)
{
strcpy(place[i],dummy);
printf("%10s", place[i]);
annual=0;
for (int j=0; j<maxrows; j++)
{
fscanf(fp,"%f",&rain[i][j]);
printf("\t%2.2f",rain[i][j]);
annual=annual+rain[i][j];
}printf("\tAnnual rainfall is %3.2f", annual);
i++;
}
} while ((strcmp(dummy,"ZZZZZ") !=0) && (i<maxrows));
printf("\n\nMonthly Average ");
for (int j=0; j<maxcols; j++)
{
annual=0;
average=0;
for (int i=0; i<maxrows; i++)
{
annual=annual+rain[i][j];
average=annual/maxrows;
}printf("%2.2f\t", average);
}
}
fclose(fp);
getchar();
}