Small problem with this array...

This is a discussion on Small problem with this array... within the C Programming forums, part of the General Programming Boards category; I'm trying to establish for each student from a data file an average, their lowest and maximum grades. The problem ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    8

    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.
    Last edited by Merholtz; 11-02-2008 at 08:16 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    What part with the backslash next to it?

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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?

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    8
    I tried braces on that portion but all it does is print the array and none of the results I need.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    8

    Thumbs up

    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;
    }

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    8
    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;
    }

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Line 28 appears to have this:
    if (grade[si][qi > qmax)
    Count [, and count ], and ponder the difference.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 11:09 AM
  2. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  3. array shift problem
    By splendid bob in forum C++ Programming
    Replies: 3
    Last Post: 07-26-2002, 10:11 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21