Finding average and dropping lowest test score

Hi, I'm new to programming and having a hard time with a class assignment. Please help me get in the right direction, I'm feeling frustrated and any advice would be greatly appreciated.

Assignment: "Calculate your quiz average for this class. Valid quiz grades are from 0-20 and you should display an error when the grade is not in range. Remember that I will drop the lowest grade if 5 or more quizzes are taken. Display the number of quizzes (including the one dropped), the lowest grade, and the average rounded to an integer (out of 100)."

We haven't gotten to arrays yet and the professor is very adamant that we don't use do while loops.

I can't figure out why when I enter "-1" as the first entry to quit the program that it tries to run and crashes. I know that for a program to crash its probably dividing by 0 but I don't know why its running through that part of the code. Secondly, I'm not sure why it won't actually give the final results (# of quizzes, lowest grade and average). Originally I was getting the display but incorrect average, now its not letting me continue when I enter "-1" to display the results. Please help :(

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;

int main()
{
	//local constants
	const int MIN_QUIZ_GRADE = 0;      //Minimum quiz grade  
	const int MAX_QUIZ_GRADE = 20;     //Maximum quiz grade
	const int QUIT = -1;               //quit program
    const int MIN_NUM_QUIZZES = 5;     //minimum number of quizzes

	
	//local variables
	int QuizGrade;                     //Quiz grades
	int NumQuizzes = 0;                //Number of Quizzes
	int LowestQuiz = MAX_QUIZ_GRADE;   //Lowest quiz grade
	float AvgGrades;                   //Average quiz grades
	int SumGrades = 0;                 //Total quiz grades

    /***************************start main program**********************/

     //Input 1st quiz grade or quit
     cout << "\n\n\n\n\n\n";
     cout << setw(60) << "Enter quiz grade (0-20) or press -1 to quit     : 
 ";
     cin >> QuizGrade;

    //WHILE (quiz grade is not quit)
    while (QuizGrade != QUIT)
    {
          
        //IF (quiz grade is not valid)
        if ((QuizGrade >= MIN_QUIZ_GRADE) && (QuizGrade <= MAX_QUIZ_GRADE))
        {
            //Add grade to total
            SumGrades = SumGrades + QuizGrade;
            
            //Add 1 to number of quizzes
            NumQuizzes ++;
            
            //IF (quiz grade is lowest so far)
            if (QuizGrade <= LowestQuiz)
            
                //Save quiz grade as lowest
                QuizGrade = LowestQuiz;
                
        }//END IF
                
        //ELSE (don't add condition here)
        else
        {
            //Display error message
       	    cout << "\n\n\n\n\n\n";
            cout << setw(48) << "------------------" << endl;
            cout << setw(48) << "Invalid Quiz Grade" << endl;
            cout << setw(48) << "------------------" << endl;
            cout << "\n\n";
        }

        //Input next quiz grade or quit
        cout << "\n\n";
        cout << setw(60) << "Enter next quiz grade (0-20) or press -1 to quit: ";
        cin  >>  QuizGrade;
    
    } //END WHILE
        
    //Clear the screen
	system("cls");
	
	//IF (no quizzes)
	if (NumQuizzes)
    {
          //Display message
          cout << "\n\n\n\n\n\n\n";
          cout << setw(50) << "---------------------" << endl;
          cout << setw(50) << "No quiz grade entered" << endl;
          cout << setw(50) << "---------------------" << endl;
          cout << "\n\n";   
    }  
    //END IF     
          
    //ELSE
    else
    {
          
         //IF (the lowest quiz gets dropped)
         if (NumQuizzes > MIN_NUM_QUIZZES)
         
            //Calc avg by dropping the lowest
            AvgGrades = float((SumGrades - LowestQuiz) / (NumQuizzes - 1) 
            * MIN_NUM_QUIZZES);
    
         //ELSE 
         else
         
             //Calc average without dropping lowest grade
             AvgGrades = float (SumGrades / NumQuizzes) * MIN_NUM_QUIZZES;
         
        //Display Results 
        cout << fixed << setprecision(1);
        cout << "\n\n\n\n\n";
        cout << setw(46) << "Quiz Results" << endl;
        cout << setw(52) << "------------------------" << endl;
        cout << "\n\n"   << setw(48) << "Number of quizzes : " << setw(4) 
                         << NumQuizzes << endl;
        cout << setw(48) << "Lowest grade      : " << setw(4) << LowestQuiz 
                         << endl; 
        cout << setw(48) << "Average quiz grade: " << setw(4) << AvgGrades 
                         << endl; 
        cout << "\n\n\n\n"; 
    } 
     
    system ("pause");
    return 0;
     
} //End main program
Last edited on
Hello of123,

After the system("cls"); and system("pause"); being security risk and only working on a windows computer and nt other platforms.

What I have noticed so far is line 47 is backwards. With what you have "LowestQuiz" will always be set to "MAX_QUIZ_GRAD" and never be changed.

At first glance the program looks OK. I will know more when I get it loaded and tested. I do not want to put you off for to long, but it might be tomorrow morning for me before I get finished.

Hope that helps for now,

Andy
Hello of123,

These re the problems I found and the changes I made to get the program to work.

Based on your above code.

Line 47. These variables are backwards. Assignment is from right to left, so what you are doing here is assigning "QuizGrade" the value of "LowestQuiz" which has a value of 20. you never see his this because at this point changing "QuizGrade" has no use and you do not do anything with "QuizGrade" after this point.

Line 73. Works, but all it does is print a message and says nothing about how many Quiz Grades have been entered. Also you my need to adjust how the information is displayed.

Line 85. This will never be reached unless "NumQuizzes" is equal to zero. I changed the line to:if (NumQuizzes). After some thought it should work if you remove line 85 and the {} that go with it as you really do not need it. You do need the if/else to figure the "AvgGrades". I will have to check this after while.

Lines 92 and 99. I hanged these because you are doing integer division before you type cast to a float. The number are small enough that a "float" may not be a problem, but these days "double" is the preferred type to use. I found this change to work better:
1
2
3
4
5
line 92:
AvgGrades = static_cast<double>(SumGrades) - static_cast<double>(LowestQuiz) / (static_cast<double>(NumQuizzes) - 1.0) * static_cast<double>(MIN_NUM_QUIZZES);

line 99:
AvgGrades = static_cast<double>(SumGrades) / static_cast<double>(NumQuizzes) * static_cast<double>(MIN_NUM_QUIZZES);


This seemed to work the best or you could just define these variables s "double" to start with and void the integer division.

Line 102. I changes the "setprecision" from 1 to 2.

Line 110. I changed the setw from 4 to 7 so the output would line up better.

Give it a try and see what you think,

Hope that helps,

Andy
Hello of123,

After some further testing I found that this line needed chnging. I had to add a set of () to make it work correctly.

1
2
3
AvgGrades = (static_cast<double>(SumGrades) - static_cast<double>(LowestQuiz)) / (static_cast<double>(NumQuizzes - 1.0)) * static_cast<double>(MIN_NUM_QUIZZES);
            ^                                                                                                          ^
            |                                                                                                          |

Hope that helps,

Andy
Last edited on
Andy!!! Thank you so much for taking the time to help! Being new in programming it can be really frustrating at times and difficult to find the problems (and solutions). I really appreciate your help!!!!!!!
Hello of123,

You are welcome. Glad to help. Been there done that.

Andy

P.S. Do not forget to green check the topic if you are done.
Last edited on
Topic archived. No new replies allowed.