943,907 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2434
  • C RSS
Mar 17th, 2005
0

arrays

Expand Post »
hi, wonder if anyone can help me.

I have a text file with the following format:

town_name followed by 12 real numbers i.e

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
ZZZZZ

the ZZZZZ is a sentinel value which tells my program to stop reading from the file. I need to read the data file and display it in the console window. However I can only get it to work when I have only one number next to the town name, as soon as the others go in, i get strange results. the entire data set needs to be held in an array as I need to do calculations to the data, below is the code I have developed so far...

  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. float rain[maxrows];
  11. float average, annual, wet, dry;
  12.  
  13. FILE *fp;
  14.  
  15. main ()
  16. {
  17. int i=0;
  18. if ((fp = fopen("data1.txt", "r"))==NULL)
  19. printf("Error opening file\n");
  20. else
  21. {
  22. do
  23. {
  24. printf("\n");
  25. fscanf(fp,"%s",&dummy);
  26. if (strcmp(dummy, "ZZZZZ") !=0)
  27. {
  28. strcpy(place[i],dummy);
  29. fscanf(fp,"%f",&rain[i]);
  30. printf("%10s\t%2.2f", place[i],rain[i]);
  31. i++;
  32. }
  33. } while ((strcmp(dummy,"ZZZZZ") !=0) and (i<maxrows));
  34. }
  35. fclose(fp);
  36. getchar();
  37. }

thanks in advance
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
dello is offline Offline
31 posts
since Dec 2004
Mar 17th, 2005
0

Re: arrays

Think in terms of rows of records consisting of fields instead of rows and columns:
  1. in = fopen ( "data1.txt", "r" );
  2. if ( in == NULL ) {
  3. /* Error */
  4. } else {
  5. int i, j;
  6.  
  7. do {
  8. fscanf ( in, "%s", dummy );
  9. if ( strcmp ( dummy, "ZZZZZ" ) == 0 )
  10. break;
  11.  
  12. /* Fill the ith record */
  13. strcpy ( place[i], dummy );
  14. for ( j = 0; j < max_cols; j++ )
  15. fscanf ( in, "%f", &rain[i] );
  16.  
  17. /* Display the ith record */
  18. printf ( "%10s\t", place[i] );
  19. for ( j = 0; j < max_cols; j++ )
  20. printf ( "%2.2f", rain[i] );
  21. printf ( "\n" );
  22. } while ( i < max_rows );
  23. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 17th, 2005
0

Re: arrays

First thing I saw was you mixed a little basic with C.
while ((strcmp(dummy,"ZZZZZ") !=0) && (i<maxrows));
not and. Other than that you were so close. All you needed to do was make another loop inside your do loop
strcpy(place[i],dummy);
 
	for (int x = 0; x < 12; x++) {
		fscanf(fp,"%f",&rain[x]);
		printf("\n\t%10s\t%2.2f", place[i],rain[x]); }
	i++;
and that solved the problem. I also cleaned up the display a little with the carriage return / tab.

Didn't mean to discredit your response Narue, but it didn't show up until I hit submit.
Last edited by Tight_Coder_Ex; Mar 17th, 2005 at 11:38 am. Reason: Narue's response
Reputation Points: 47
Solved Threads: 17
Posting Whiz in Training
Tight_Coder_Ex is offline Offline
215 posts
since Feb 2005
Mar 17th, 2005
0

Re: arrays

thanks for your response guys,

I had actually just about figured it out before I read the resonses but they were really helpful, just pushed me in the right direction!

thanks again guys
Reputation Points: 10
Solved Threads: 0
Light Poster
dello is offline Offline
31 posts
since Dec 2004
Mar 17th, 2005
0

Re: arrays

>First thing I saw was you mixed a little basic with C.
You're gonna love this. In C, since the 95 ammendment, the header <iso646.h> defines macros such as and, or, and not_eq as a reasonable alternative to the nasty trigraphs for implementations that don't support the requisite characters to represent the base language. In standard C++ all of those macros are language keywords. So it's not quite a stretch to imagine C or C++ code using them.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 17th, 2005
0

Re: arrays

can someone have a look at this for me please? i cannot understand why i am getting all the spare 0.00 numbers when i compile!

a text file is required as before, the details are in my first post in this thread

thanks

  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. float rain[maxrows][maxcols];
  11. float average, annual, wet, dry;
  12.  
  13. FILE *fp;
  14.  
  15. main ()
  16. {
  17. int x=0;
  18. if ((fp = fopen("data1.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[x],dummy);
  29. printf("%10s", place[x]);
  30. annual=0;
  31. for (int i=0; i<maxrows; i++)
  32. {
  33. for(int j=0; j<maxcols; j++)
  34. {
  35. fscanf(fp,"%f",&rain[i][j]);
  36. printf("\t%2.2f",rain[i][j]);
  37. annual=annual+rain[i][j];
  38. }
  39. }printf("\tAnnual rainfall is %3.2f", annual);
  40. x++;
  41. }
  42. } while ((strcmp(dummy,"ZZZZZ") !=0) && (x<maxrows));
  43. }
  44. fclose(fp);
  45. getchar();
  46. }
Reputation Points: 10
Solved Threads: 0
Light Poster
dello is offline Offline
31 posts
since Dec 2004
Mar 17th, 2005
0

Re: arrays

never mind, its sussed now, was a simple error on my part!!
Reputation Points: 10
Solved Threads: 0
Light Poster
dello is offline Offline
31 posts
since Dec 2004
Mar 17th, 2005
0

Re: arrays

Your nested for loops mean that you are trying to read 144 values each line. To me it looks like the do...while is attempting to be what the first for loop should be: rows. I'd do something more like this.
  1. for ( int i = 0; i < maxrows; ++i )
  2. {
  3. fscanf ( fp,"%s", place[i] );
  4. if ( strcmp ( place[i], "ZZZZZ" ) == 0 )
  5. {
  6. break;
  7. }
  8. printf ( "%10s", place[i] );
  9.  
  10. annual = 0;
  11. for ( int j = 0; j < maxcols; ++j )
  12. {
  13. fscanf ( fp, "%f", &rain[i][j] );
  14. printf ( "\t%2.2f", rain[i][j] );
  15. annual = annual + rain[i][j];
  16. }
  17. printf("\tAnnual rainfall is %3.2f\n", annual);
  18. }
Last edited by Dave Sinkula; Mar 17th, 2005 at 6:02 pm. Reason: :o Can't count!
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Mar 17th, 2005
0

Re: arrays

Unless the format of your file has changed, you're trying to read too many floats, and fscanf is failing miserably when you get to the next city. Here's how any logic you use should work:
  1. for each line
  2. read the name
  3. read 12 floats
Anything else will give you incorrect output. As it is, your code is trying to do this:
  1. for each line
  2. read the name
  3. read 144 floats
Notice the difference?
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 17th, 2005
0

Re: arrays

yes, i figured it out now, i realised that it was trying to read in far too many values but it is working fine now..

thanks again
Reputation Points: 10
Solved Threads: 0
Light Poster
dello is offline Offline
31 posts
since Dec 2004

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: Continued Tic Tac Toe help
Next Thread in C Forum Timeline: Listing Integers in Numerical Order





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


Follow us on Twitter


© 2011 DaniWeb® LLC