# Small problem with this array...

• 11-02-2008
Merholtz
Small problem with this array...
I'm trying to establish for each student from a data file an average, their lowest and maximum grades. The problem comes when I add in the portion with a back slash next to it. Not only does it not input the values but it completely messes up the array. Any ideas? Am I just mis-placing my code?

Code:

```#define GRADES 5 #define STUDENTS 10 #define <stdio.h> int main() { /* grade[0][0] is student 0 quiz 0 */ /* grade[0][1] is student 0 quiz 1 */ int qi = 0, si = 0, grade[STUDENTS][GRADES], mins = 9999, maxs = 0, avgs = 0 , sums = 0, minq = 9999, maxq = 0, sumq = 0, avgq = 0 ; while (si < STUDENTS) {     qi = 0 ;     while (qi < GRADES) {         if (scanf("%d",&grade[si][qi]) == EOF) //          avgs = sums / GRADES; //            printf("Student #%d stats: high %d, low %d, avg %d. \n", si, maxs, mins, avgs);             break;                 //    else if (grade[si][qi] < mins)     //        mins = grade[si][qi];     //    else if(grade[si][qi] > maxs)     //        maxs = grade[si][qi];     //  sums += grade[si][qi];         qi++;     }     si++; } for (si = 0; si < STUDENTS ; si++){     printf("Student #%2d: ",si);     for (qi = 0; qi < GRADES; qi++)         printf("%6d ",grade[si][qi]);     printf("\n"); } return 0; }```
Here is the input file.

64
251
680
221
892
122
242
109
566
743
364
455
148
167
138
413
456
149
0
245
422
268
184
110
433
221
222
0
347
182
205
485
116
200
206
184
332
286
122
206
194
286
335
341
146
228
248
992
620
283
2
8

Edit: Put in the backslash modifiers.
• 11-02-2008
tabstop
What part with the backslash next to it?
• 11-02-2008
tabstop
Quote:

Code:

```      if (scanf("%d",&grade[si][qi]) == EOF) //          avgs = sums / GRADES; //            printf("Student #%d stats: high %d, low %d, avg %d. \n", si, maxs, mins, avgs);             break;```

You weren't trying to make indentation play the role of curly braces, were you?
• 11-02-2008
Merholtz
I tried braces on that portion but all it does is print the array and none of the results I need.
• 11-02-2008
tabstop
Well, consider your algorithm:
Code:

```FOR EACH student     READ IN a bunch of grades     CALCULATE some averages```
Note that (1) no EOF is checking is involved (if your data file ends in the middle of a student, there's nothing you can do about it anyway); (2) averages are calculated anyway without relying on EOF to happen.
• 11-02-2008
Merholtz
Ah, I see what you mean. It reads the first set of numbers I want and even prints them. I just need a reliable way to print them off before I reset them. New code is

Code:

``` #define GRADES 5 #define STUDENTS 10 #include <stdio.h> int main() { /* grade[0][0] is student 0 quiz 0 */ /* grade[0][1] is student 0 quiz 1 */ int qi = 0, si = 0, grade[STUDENTS][GRADES], mins = 9999, maxs = 0, avgs = 0 , sums = 0, minq = 9999, maxq = 0, sumq = 0, avgq = 0 ; while (si < STUDENTS) {     qi = 0 ;     while (qi < GRADES) {         if (scanf("%d",&grade[si][qi]) == EOF)             break;         else if (grade[si][qi] < mins)             mins = grade[si][qi];         else if(grade[si][qi] > maxs)             maxs = grade[si][qi];         sums += grade[si][qi];         avgs = sums / GRADES;         qi++;     }     si++; } for (si = 0; si < STUDENTS ; si++){     printf("Student #%2d: ",si);     for (qi = 0; qi < GRADES; qi++)         printf("%6d ",grade[si][qi]);     printf("\n");     printf("Student #%d stats: high %d, low %d, avg %d. \n", si, maxs, mins,  avgs); } return 0; }```
• 11-03-2008
Merholtz
Well, I got the student portion set but now it says there is a parse error before the quiz portion on line 28.

Code:

``` #define GRADES 5 #define STUDENTS 10 #include <stdio.h> int main() { /* grade[0][0] is student 0 quiz 0 */ /* grade[0][1] is student 0 quiz 1 */ int qi = 0, si = 0, grade[STUDENTS][GRADES], smax, smin, ssum, savg; int qmax, qmin, qsum, qavg; while (si < STUDENTS) {     qi = 0 ;     while (qi < GRADES) {         if (scanf("%d",&grade[si][qi]) == EOF) break;         qi++;     }     si++; } for (qi = 0; qi < GRADES; qi++)     {       qmax = 0;     qmin = 9999;     qsum = 0;     for(si = 0; si < STUDENTS; si++){         if (grade[si][qi > qmax)             qmax = grade[si][qi];         if (grade[si][qi] < qmin)             qmin = grade[si][qi];         qsum += grade[si][qi];         }     qavg = qsum / STUDENTS;     printf("Quiz #%d stats: low %d, high %d, avg %d.\n", qi, qmin, qmax, qavg);     } for (si = 0; si < STUDENTS; si++){     smax = 0;     smin = 9999;     ssum = 0;     for (qi = 0; qi < GRADES; qi++){         if (grade[si][qi] > smax)             smax = grade[si][qi];         if (grade[si][qi] < smin)             smin = grade[si][qi];             ssum += grade[si][qi];         }     savg = ssum / GRADES;     printf("Student #%d stats: low %d, high %d, avg %d.\n", si, smin, smax, savg );     } for (si = 0; si < STUDENTS ; si++){     printf("Student #%2d: ",si);     for (qi = 0; qi < GRADES; qi++)         printf("%6d ",grade[si][qi]);     printf("\n"); } return 0; }```
• 11-03-2008
tabstop
Line 28 appears to have this:
Quote:

if (grade[si][qi > qmax)
Count [, and count ], and ponder the difference.