944,041 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2823
  • C RSS
Jun 24th, 2005
0

array / data comparision help

Expand Post »
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..

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

  1. #include <string.h>
  2.  
  3. const int maxrows=12;
  4. const int maxcols=12;
  5. const int maxchar=10;
  6.  
  7. static char place[maxrows][maxchar];
  8. static char dummy[maxchar];
  9. int month;
  10. float rain[maxrows][maxcols];
  11. float average, annual, wet, dry;
  12.  
  13. FILE *fp;
  14.  
  15. main ()
  16. {
  17. int i=0;
  18. if ((fp = fopen("data.txt", "r"))==NULL)
  19. printf("Error opening file\n");
  20. else
  21. {
  22. do
  23. {
  24. printf("\n\n");
  25. fscanf(fp,"%s",&dummy);
  26. if (strcmp(dummy, "ZZZZZ") !=0)
  27. {
  28. strcpy(place[i],dummy);
  29. printf("%10s", place[i]);
  30. annual=0;
  31. for (int j=0; j<maxrows; j++)
  32. {
  33. fscanf(fp,"%f",&rain[i][j]);
  34. printf("\t%2.2f",rain[i][j]);
  35. annual=annual+rain[i][j];
  36. }printf("\tAnnual rainfall is %3.2f", annual);
  37. i++;
  38. }
  39. } while ((strcmp(dummy,"ZZZZZ") !=0) && (i<maxrows));
  40.  
  41. printf("\n\nMonthly Average ");
  42. for (int j=0; j<maxcols; j++)
  43. {
  44. annual=0;
  45. average=0;
  46. for (int i=0; i<maxrows; i++)
  47. {
  48. annual=annual+rain[i][j];
  49. average=annual/maxrows;
  50. }printf("%2.2f\t", average);
  51. }
  52. }
  53. fclose(fp);
  54. getchar();
  55. }
Attached Files
File Type: txt data.txt (866 Bytes, 14 views)
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
dello is offline Offline
31 posts
since Dec 2004
Jun 24th, 2005
0

Re: array / data comparision help

Quote originally posted by dello ...
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.
Quote originally posted by dello ...
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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jun 25th, 2005
0

Re: array / data comparision help

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

Quote ...
When you find the month with the highest and lowest, not the index -- this will give you the month.
Reputation Points: 10
Solved Threads: 0
Light Poster
dello is offline Offline
31 posts
since Dec 2004
Jun 25th, 2005
0

Re: array / data comparision help

If rain[month] was the highest, then the highest is in month.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jun 25th, 2005
0

Re: array / data comparision help

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
Reputation Points: 10
Solved Threads: 0
Light Poster
dello is offline Offline
31 posts
since Dec 2004
Jun 25th, 2005
0

Re: array / data comparision help

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
*/
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jun 25th, 2005
0

Re: array / data comparision help

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

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

  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. const int maxrows=12;
  5. const int maxcols=12;
  6. const int maxchar=10;
  7.  
  8. static char place[maxrows][maxchar];
  9. static char dummy[maxchar];
  10. const char *months[12] = {"January","February","March","April","May","June","July","August","September","October","November","December"};
  11. float rain[maxrows][maxcols];
  12. float average, annual, wet, dry, total, lowval, highval, highindex, lowindex;
  13.  
  14. FILE *fp;
  15.  
  16. main ()
  17. {
  18. int i=0;
  19. if ((fp = fopen("data.txt", "r"))==NULL)
  20. printf("Error opening file\n");
  21. else
  22. {
  23. do
  24. {
  25. printf("\n\n");
  26. fscanf(fp,"%s",&dummy);
  27. if (strcmp(dummy, "ZZZZZ") !=0)
  28. {
  29. strcpy(place[i],dummy);
  30. printf("%10s", place[i]);
  31. annual=0;
  32. for (int j=0; j<maxrows; j++)
  33. {
  34. fscanf(fp,"%f",&rain[i][j]);
  35. printf("\t%2.2f",rain[i][j]);
  36. annual=annual+rain[i][j];
  37. }
  38.  
  39. printf("\tAnnual rainfall is %3.2f", annual);
  40. i++;
  41. }
  42. } while ((strcmp(dummy,"ZZZZZ") !=0) && (i<maxrows));
  43. // make a temp varable
  44. int tmp = i;
  45. printf("\n\nMonthly Average ");
  46. //for col 0 to last coll
  47. for (int j=0; j<tmp; j++)
  48. {
  49. annual=0;
  50. average=0;
  51. for (int i=0; i<maxrows; i++)
  52. {
  53. annual=annual+rain[i][j];
  54. }
  55. average=annual/tmp;
  56. printf("%2.2f\t", average);
  57.  
  58. }printf("\n\n");
  59. }
  60. for (int i=0; i < maxcols; i++)
  61. {
  62. lowval = 99999;
  63. highval = 0;
  64.  
  65. if (strcmp("ZZZZZ", place[i])!=0)
  66. {
  67.  
  68. for (int j=0; j<maxrows; j++)
  69. {
  70. if (rain[i][j] > highval)
  71. {
  72. highval=rain[i][j];
  73. highindex = j;
  74. }
  75.  
  76. if (rain[i][j] < lowval)
  77. {
  78. lowval = rain[i][j];
  79. lowindex = j;
  80. }
  81.  
  82. printf("%10s\n", place[i]);
  83. printf("\tDry Month : %s\t\tDry :%.2f\n",months[i],lowval);
  84. printf("\tWet Month : %s\t\tWet :%.2f\n",months[j],highval);
  85. }
  86. }
  87.  
  88.  
  89. fclose(fp);
  90. getchar();
  91.  
  92. }
  93. }
Reputation Points: 10
Solved Threads: 0
Light Poster
dello is offline Offline
31 posts
since Dec 2004
Jun 26th, 2005
0

Re: array / data comparision help

Just a note...

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

Take care,
Bruce
Reputation Points: 10
Solved Threads: 0
Newbie Poster
BruceWilson512 is offline Offline
19 posts
since Jun 2005
Jun 26th, 2005
0

Re: array / data comparision help

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
BruceWilson512 is offline Offline
19 posts
since Jun 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Strings
Next Thread in C Forum Timeline: Using winsock2.h functions to send data





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC