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();
}
Attachments
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	
Manchester	12.23	10.67	7.56	4.34	5.55	6.29	8.77	14.77	9.77	7.49	8.34	7.66	
Liverpool	10.12	6.76	7.87	6.66	4.56	7.82	12.63	9.62	6.02	7.94	8.34	9.18	
Bristol 	6.55	8.61	4.67	4.86	5.55	2.39	6.66	4.91	2.93	4.66	4.81	7.39
Preston		2.33	8.41	6.22	4.58	2.11	1.11	3.45	2.65	7.54	8.99	7.84	8.33
Blackburn	7.77	5.42	6.87	4.21	3.25	8.65	10.45	8.61	3.45	7.14	8.24	6.66
Bolton		9.99	4.57	7.89	6.58	9.64	3.24	14.78	12.26	2.36	4.47	5.57	5.64
Newcastle	7.89	5.14	6.35	8.25	9.14	7.25	6.24	8.34	2.45	8.24	2.15	4.25
Leeds		4.12	6.25	7.24	9.65	2.42	9.65	1.56	4.75	7.84	5.14	2.14	4.76
Sheffield	7.84	5.11	1.25	2.63	7.84	3.32	2.84	8.62	7.14	6.54	4.15	7.15
Leicester	10.87	12.36	1.77	5.54	2.56	7.41	9.24	5.45	5.73	9.21	2.15	8.24		
Birmingham	4.76	8.24	2.25	8.54	8.13	4.73	3.47	8.54	8.95	10.24	7.54	7.22
ZZZZZ

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...

The basics...

#include <stdio.h>

int main(void)
{
   static const char filename[] = "data.txt";
   FILE *file = fopen(filename, "r");
   if ( file )
   {
      char line[20][80];
      int j, i = 0;
      while ( fgets(line[i], sizeof line[i], file) != NULL )
      {
         ++i;
      }
      fclose(file);
      printf("i = %d\n", i);
      for ( j = 0; j < i; ++j )
      {
         fputs(line[j], stdout);
      }
   }
   return 0;
}

/* data.txt
London
Manchester
Liverpool
Bristol
*/

/* my output
i = 4
London
Manchester
Liverpool
Bristol
*/

Keep track of how many you read and use that instead of a hard-code value.

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

When you find the month with the highest and lowest, not the index -- this will give you the month.

thanks for this dave - just coudnt understand what you meant by the following!

When you find the month with the highest and lowest, not the index -- this will give you the month.

im still not with it... basically this is what i am trying to acheive

screen display of text file - including town name - with a total at the end of each row and a average at the bottom of each column - i have this done.

i now need to display the town name and then wettest month and dryest month.
so London: Wettest Month - October: 8.61 Dryest Month - June: 2.45

so i need to look at the array again and do a comparison of each value in a row to find the highest and lowest and then need to make sure it assigns the correct month value, im sorry i just dont know how to go about doing this

Canned example:

#include <stdio.h>

int main()
{
   static const char *month[] = 
   {
      "January", "February", "March", "April", "May", "June",
      "July", "August", "September", "October", "November", "December"
   };
   static const double rainfall[] =
   {
      7.24, 8.15, 6.45, 3.24, 3.66, 2.45, 4.71, 6.78, 6.45, 8.61, 7.45, 6.55  
   };
   size_t i, most, least; /* indices */
   for ( most = least = i = 0; i < sizeof rainfall / sizeof *rainfall; ++i )
   {
      if ( rainfall[i] > rainfall[most] )
      {
         most = i; /* save index to highest rainfall */
      }
      if ( rainfall[i] < rainfall[least] )
      {
         least = i; /* save index to lowest rainfall */
      }
   }
   printf("Wettest Month - %s : %g Driest month - %s : %g\n", 
          month[most], rainfall[most], month[least], rainfall[least]);
   return 0;
}

/* my output
Wettest Month - October : 8.61 Driest month - June : 2.45
*/

thanks again for your time dave, but im afraid im still not getting it - its really frustrating me, i just cant seem to grasp the concept of the arrays :sad:

here is the code i have got - the rest of the program seems to work fine but i cant display the wet/dry months correctly - it just keeps coming up with london and listing all the months as wet and dry.. :sad:

#include <stdio.h>
#include <string.h>

const int maxrows=12;
const int maxcols=12;
const int maxchar=10;

static char place[maxrows][maxchar];
static char dummy[maxchar];
const char *months[12] = {"January","February","March","April","May","June","July","August","September","October","November","December"};
float rain[maxrows][maxcols];
float average, annual, wet, dry, total, lowval, highval, highindex, lowindex; 

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));
        // make a temp varable
        int tmp = i;
        printf("\n\nMonthly Average ");
        //for col 0 to last coll
        for (int j=0; j<tmp; j++)
        {
            annual=0;
            average=0;
            for (int i=0; i<maxrows; i++)
            { 
                annual=annual+rain[i][j];
            }
			average=annual/tmp;
			printf("%2.2f\t", average);
			
        }printf("\n\n");
    }
    for (int i=0; i < maxcols; i++)
    {
        lowval = 99999;
        highval = 0;
        
        if (strcmp("ZZZZZ", place[i])!=0)
        {
         
            for (int j=0; j<maxrows; j++)
            {
                if (rain[i][j] > highval)
                {
                    highval=rain[i][j];
                    highindex = j;    
                }
                
                if (rain[i][j] < lowval)
                {
                    lowval = rain[i][j];
                    lowindex = j;
                }
   
            printf("%10s\n", place[i]);
            printf("\tDry Month   : %s\t\tDry   :%.2f\n",months[i],lowval);
            printf("\tWet Month   : %s\t\tWet   :%.2f\n",months[j],highval);
            }
           } 
           
     
    fclose(fp);
    getchar();
           
 }
}

Just a note...

Is this line correct?
for (int j=0; j<maxrows; j++)
Should it be maxcols instead?

Take care,
Bruce

dello,

I have your code running on my computer. Take a look at how you are calculating the average. Is that what you really want?

Take care,
Bruce

This article has been dead for over six months. Start a new discussion instead.