Thread: Grades program has me lost

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    5

    Angry Need help with counter and accumulators

    This one is a review for a final exam which I will be taking in about 13 hours. Supposively, my instructor insist that if we can do this program we should be able to do the final. I'm big in trouble. I started the program but I'm stumped. I just started back attending my classes 2 1/2 weeks ago after being out for 8 weeks ( was put on bed rest by doctor due to complication with pregnancy). Should've withdrew from my classes, but I didn't so now I'm trying to catchup on what I missed in four classes and attempt to take the finals. The only thing that my program does is displays the number of all grades entered including valid and non valid. I need it to display total number of grades included in the average; the average grade; and total number of A’s, B’s, C’s, D’s, and F’s. Right now it displays the average but the average includes grades of 0 which it shouldn't.
    Would someone please advise me on what to do. I really want to know how to count the number of letter grades. I missed so much of class and am very lost, tired, and stressed out. I hope I don't go into early labor over this class. I've included a copy of the program description and the code that I have so far.

    PROGRAM DESCRIPTION

    You task is to write an interactive program that determines a student’s letter grade from the numeric grade (an integer), computes the average of all valid grades entered, and displays statistics about the grades entered.

    SPECIFIC DIRECTIONS

    Ask the user for one numeric grade at a time, and display the appropriate letter grade. You must use a separate function to request and input the grade, then return the grade to main.

    The numeric ranges for grades are as follows:

    91 – 100: A
    81 – 90: B
    71 – 80: C
    65 – 70: D
    Below 65: F

    Use another function to test the numeric grade and return the letter equivalent to main .

    You need to determine the average of all valid grades entered: any nonzero grade is valid, but any grades of zero should not be included in the average, since this means the student didn’t even take the test (you get 2 points for writing your name!). So, you don’t want to lower the average by including grades of Ø. When the user wants to quit, display the following: total number of all grades entered, including valid grades and grades of Ø; total number of grades included in the average; the average grade; and total number of A’s, B’s, C’s, D’s, and F’s.

    Program
    __________________________________________________
    #include <iostream>
    using namespace std;

    //function prototypes
    int getAvgGrade();
    char assignGrade(float);

    int main()
    {
    //declare variables

    char grade = ' ';
    float n_grade = 0.0;
    float total_n_grade= 0.0;
    float avg =0.0;
    //get average grade
    n_grade = getAvgGrade();
    //assign grade
    grade = assignGrade(n_grade);
    //display grade
    cout << "Average Grade: " << grade << endl;


    return 0;
    } //end of main function

    //*****program-defined functions*****
    int getAvgGrade()
    {
    //gets and accumulates the scores, then returns the average
    int total_n_grade = 0;
    int total_grades = 0;
    float n_grade = 0.0;
    char letterGrade = ' ';
    char grade = ' ' ;
    cout << "Pleae enter the first numeric grade: ";
    cin >> n_grade;

    while (n_grade > 0)
    {

    total_grades = total_grades + 1;

    total_n_grade = total_n_grade + n_grade;

    cout << "please enter the next numeric grade: ";
    cin >> n_grade;

    }//end while
    if (total_grades >=0)
    n_grade = total_n_grade/ total_grades;
    cout << "Total number of all grades entered: " << total_grades << endl;

    return n_grade;

    } //end of getAvGrade function


    char assignGrade(float n_grade)
    {
    //assigns the letter grade
    char letterGrade = ' ';

    if (n_grade>= 91)
    letterGrade = 'A';
    else if (n_grade >= 81)
    letterGrade = 'B';
    else if (n_grade >= 71)
    letterGrade = 'C';
    else if (n_grade >= 65)
    letterGrade = 'D';
    else letterGrade = 'F';
    //end ifs
    return letterGrade;
    } //end of assignGrade function
    Last edited by adrea; 12-13-2002 at 06:33 AM.

  2. #2
    Registered User Xaviar Khan's Avatar
    Join Date
    Sep 2001
    Posts
    10

    New Approach.

    Adrea,
    Good luck with what you're trying to do, but the class dosen't seem un-doable if you put yourself to it. I have (what seems to me) a better approach though for what you're trying to accomplish. Use it if you wish.


    Code:
    #include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;
    
    do_enter_grades( void );  // prototype
    
    int main( void )
    {
    	do_enter_grades();
    
    return 0;
    }
    
    
    do_enter_grades( void )
    {
    	int A = 0, B = 0, C = 0, D = 0, F = 0;
    	int totalgrades = 0;
    	int grades;
    
    	cout <<"Please Enter Grades (-1 to end): ";
    	cin>>grades;
    
    	while ( grades != -1 )
    	{
    		if ( ( grades > 100 ) || ( grades < 0 ) )
    			cout <<"That is outside the valid grade range.  (0 - 100)";
    
    		if ( grades < 65 )
    		{
    			F++;
    			totalgrades += 1;
    		}
    		if ( ( grades > 65 ) && ( grades <= 70 ) )
    		{
    			D++;
    			totalgrades += 1;
    		}
    		if ( ( grades > 70 ) && ( grades <= 80 ) )
    		{
    			C++;
    			totalgrades += 1;
    		}
    		if ( ( grades > 80 ) && ( grades <= 90 ) )
    		{
    			B++;
    			totalgrades += 1;
    		}
    		if ( ( grades > 90 ) && ( grades <= 100 ) )
    		{
    			A++;
    			totalgrades += 1;
    		}
    
    	cout <<"Please Enter Grades (-1 to end): ";
    	cin>>grades;
    	}
    
    	cout <<"\nTotal A's = '" <<A <<"'"
    		 <<"\nTotal B's = '" <<B <<"'"
    		 <<"\nTotal C's = '" <<C <<"'"
    		 <<"\nTotal D's = '" <<D <<"'"
    		 <<"\nTotal F's = '" <<F <<"'";
    	
    	cout <<endl;
    
    return 0;
    }
    That should take care of tallying the grade letters, all you need to do is pass them numbers to your other function and devide it by the 'totalgrades' variable. If this isn't self-explanatory then I wish you the best with your exam.

    hth
    X

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    82
    Hi adrea-

    I don't see the problem you are describing. The logic appears to be okay, except that as soon as you enter a zero the loop stops and the average is computed; you aren't giving it the option to consider zero grades. One way to fix this is to use a negative grade as the flag to stop counting:
    Code:
    int getAvgGrade()
    {
        //gets and accumulates the scores, then returns the average
        int total_n_grade = 0;
        int total_grades = 0;
        int zero_grades = 0;  // Added this
        float n_grade = 0.0;
        char letterGrade = ' ';
        char grade = ' ' ;
        cout << "Pleae enter the first numeric grade: ";
        cin >> n_grade;
    
        while (n_grade >= 0)  // Changed this here
            {
            if (n_grade > 0) { // Added this if-else
                total_grades++;
                total_n_grade += n_grade;
            } else
                zero_grades++;
    
            cout << "please enter the next numeric grade: ";
            cin >> n_grade;
    
        }//end while
        if (total_grades >=0)
            n_grade = total_n_grade/ total_grades;
    
        cout << "Total number of all grades entered: " 
             << total_grades + zero_grades << endl;  // Changed this
        cout << "Total number of valid grades entered: " 
             << total_grades << endl;  // Added this
        return n_grade;
    } //end of getAvGrade function
    Also, you may want to have something else that deals with the special case where the average ends up being zero (in other words the student didn't take any tests at all. The program with the revisions to the getAvGrade function produced the following run:
    Code:
    akua% a.out
    Pleae enter the first numeric grade: 100
    please enter the next numeric grade: 100
    please enter the next numeric grade: 100
    please enter the next numeric grade: 0
    please enter the next numeric grade: 100
    please enter the next numeric grade: 100
    please enter the next numeric grade: 0
    please enter the next numeric grade: 0
    please enter the next numeric grade: 100
    please enter the next numeric grade: -1
    Total number of all grades entered: 9
    Total number of valid grades entered: 6
    Average Grade: A
    Hope this isn't too late, good luck on your final.
    Last edited by geophyzzer; 12-13-2002 at 11:37 AM.
    Claus Hetzer
    Compiler: Borland 5.5 (on Windows)
    Solaris CC (on Unix)
    Known Languages: C++, MATLAB, Perl, Java

  4. #4
    Registered User
    Join Date
    Dec 2002
    Posts
    5
    Thank you very much for taking the time to read and answer my post. Your examples gave me a better understanding of how to write the program . The final exam was similar to the review. I found out my grade today. I made a 79, but thats only because I didn't complete my flowchart. The best part of all is....... finals are over and I passed my C programming class. I have you all to thank for that. Now, I can relax for the next five weeks until my baby is due Once again thank you all very much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  2. program
    By shawn33 in forum C Programming
    Replies: 1
    Last Post: 12-08-2005, 10:55 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. How to complete program. Any probs now?
    By stehigs321 in forum C Programming
    Replies: 7
    Last Post: 11-19-2003, 04:03 PM
  5. Replies: 2
    Last Post: 05-10-2002, 04:16 PM