arrays

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Dec 2004
Posts: 26
Reputation: dello is an unknown quantity at this point 
Solved Threads: 0
dello dello is offline Offline
Light Poster

arrays

 
0
  #1
Mar 17th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,847
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 753
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: arrays

 
0
  #2
Mar 17th, 2005
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. }
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 199
Reputation: Tight_Coder_Ex is an unknown quantity at this point 
Solved Threads: 14
Tight_Coder_Ex's Avatar
Tight_Coder_Ex Tight_Coder_Ex is offline Offline
Junior Poster

Re: arrays

 
0
  #3
Mar 17th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 26
Reputation: dello is an unknown quantity at this point 
Solved Threads: 0
dello dello is offline Offline
Light Poster

Re: arrays

 
0
  #4
Mar 17th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,847
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 753
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: arrays

 
0
  #5
Mar 17th, 2005
>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.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 26
Reputation: dello is an unknown quantity at this point 
Solved Threads: 0
dello dello is offline Offline
Light Poster

Re: arrays

 
0
  #6
Mar 17th, 2005
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. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 26
Reputation: dello is an unknown quantity at this point 
Solved Threads: 0
dello dello is offline Offline
Light Poster

Re: arrays

 
0
  #7
Mar 17th, 2005
never mind, its sussed now, was a simple error on my part!!
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,459
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 252
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: arrays

 
0
  #8
Mar 17th, 2005
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!
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,847
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 753
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: arrays

 
0
  #9
Mar 17th, 2005
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?
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 26
Reputation: dello is an unknown quantity at this point 
Solved Threads: 0
dello dello is offline Offline
Light Poster

Re: arrays

 
0
  #10
Mar 17th, 2005
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC