| | |
array / data comparision help
![]() |
•
•
Join Date: Dec 2004
Posts: 26
Reputation:
Solved Threads: 0
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..
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
•
•
•
•
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 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
C Syntax (Toggle Plain Text)
#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(); }
•
•
•
•
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...
#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
*/•
•
•
•
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
"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
If rain[month] was the highest, then the highest is in month.
"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
•
•
Join Date: Dec 2004
Posts: 26
Reputation:
Solved Threads: 0
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
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
*/ "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
•
•
Join Date: Dec 2004
Posts: 26
Reputation:
Solved Threads: 0
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..
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..
C Syntax (Toggle Plain Text)
#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(); } }
![]() |
Similar Threads
- Array data not setting (C++)
Other Threads in the C Forum
- Previous Thread: Strings
- Next Thread: Using winsock2.h functions to send data
| Thread Tools | Search this Thread |
#include * ansi api array arrays asterisks binarysearch calculate centimeter changingto char character convert copyanyfile copyimagefile copypdffile creafecopyofanytypeoffileinc createcopyoffile createprocess() database dynamic execv fflush fgets file floatingpointvalidation fork forloop frequency function getlogicaldrivestrin givemetehcodez grade graphics gtkwinlinux histogram homework i/o inches include infiniteloop input interest intmain() iso keyboard km license linked linkedlist linux list looping loopinsideloop. lowest matrix microsoft mysql oddnumber open opendocumentformat openwebfoundation pdf pointer posix power program programming pyramidusingturboccodes radix read recursion recv recvblocked reversing scanf scheduling segmentationfault send sequential shape single socket socketprogramming stack standard strchr string suggestions test threads turboc unix urboc user variable whythiscodecausesegmentationfault win32api windowsapi






