Thread: Using an Array to store the count.

  1. #1
    Unregistered
    Guest

    Using an Array to store the count.

    The basic problem I was given is to write a C++ program to output a report that shows the grade distribution for a course. The program first prompts for and inputs the specified number of students. The program then prompts for and inputs the final grade for each student.
    Finally the program displays a report of the grade distribution.The hint given was There is no need to use an array to store the actual input grades, instead use an array to keep track of the counting, that is, how many times each grade occurs in the input.

    I can manage the input on number of students, but the hint throws me off course.
    I'm guessing I should have an array of size 9
    ie FinalGrade[9]
    But I have no Idea on how to keep track of number of times students get different grades.
    This is my second week using C++, and I've only used arrays to hold the input.
    Any help or more usefull hints would be apreciated.

  2. #2
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    If you have grades 1->5 then you could create an integer array with five elements, and increment it for each grade -

    int grades[5]={0};
    int grade;
    cin >> grade;
    grades[grade-1]++;
    //repeat for all students
    zen

  3. #3
    Registered User Strider's Avatar
    Join Date
    Aug 2001
    Posts
    149
    Here is a simple method using an if / else if/ else structure for checking the grade value as it is entered.
    Code:
    #include <iostream.h>
    
    int main()
    {
        int num_students  = 0;
        int grade = 0;
        int grade_scale[5] = {0}  // A, B, C, D, F
        char letter_grade[5] = {'A', 'B', 'C', 'D', 'F'};
    
        cin >> num_students;
    
        for ( int i = 0; i < num_students; i++)
        {
            cin >> grade;
    
            if ( grade > 89 )
                ++grade_scale[0];  // add an A
            else if ( grade > 79)
                ++grade_scale[1];  // add a B
            else if ( grade > 79)
                ++grade_scale[2];  // add a C
            else if ( grade > 79)
                ++grade_scale[3];  // add a D
            else
                ++grade_scale[4];  // add an F
        } // end for loop
    
        for ( i = 0; i < 5; i++)
            cout << letter_grade[i] << ":  " << grade_scale[i] << endl;
    
        return 0;
    } // end main
    David
    One Ring to rule them all, One Ring to find them,
    One Ring to bring them all and in the darkness bind them
    In the Land of Mordor where the Shadows lie.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 03:55 AM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  4. Replies: 3
    Last Post: 10-15-2005, 09:13 AM
  5. How to store Intergers in ARRAY
    By cgn38 in forum C Programming
    Replies: 3
    Last Post: 01-05-2004, 12:14 PM