Calculating GPA: Find the Average

  • Thread starter Thread starter magnifik
  • Start date Start date
  • Tags Tags
    Average Gpa
Click For Summary

Discussion Overview

The discussion revolves around a programming issue related to calculating the average GPA from user input in a C++ program. Participants are addressing coding errors and suggesting improvements to the logic and structure of the program.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant notes that the average calculation is performed before any input is given, suggesting that the average variable should be calculated after the while loop.
  • Another participant points out that the count variable should increment when GPA is not equal to -1, rather than when it is not equal to 1.
  • A suggestion is made to change the type of count to an integer since it only accepts integer values, which could simplify the division operation later.
  • Concerns are raised about initializing the average variable to the value of 0/0, which could lead to undefined behavior.

Areas of Agreement / Disagreement

Participants generally agree on the issues with the program's logic and suggest similar corrections, but there is no consensus on the best approach to implement these changes.

Contextual Notes

Limitations include the potential for division by zero if no valid GPAs are entered before the sentinel value, and the reliance on specific input conditions that may not be clearly defined in the program.

Who May Find This Useful

Readers interested in programming, particularly in C++, and those looking to understand common pitfalls in input handling and average calculations may find this discussion useful.

magnifik
Messages
350
Reaction score
0
i know this is a super easy program, but for some reason it's not working the way i want it to

Code:
// prompt user to enter several GPA's then find the average
// while the user's input is not the sentinel value, -1, add it to the sum
#include <iostream>

using namespace std;

int main(){
	double GPA = 0;
	double sum = 0;
	double count = 0;
	double average = sum / count;

	while (GPA != -1){
		cin >> GPA;
		sum += GPA;
		if (GPA != 1){
			count++;
		}
	}
	cout << average;
	return 0;
}
 
Physics news on Phys.org
For programs like this, its easy to create output statements for the value of variables as the math progresses. You can then imput simple numbers that you can calculate the program output value for on paper and compare them to the answer the program gives, then work backwards to find the error.

Where in your program does the 'average' variable get changed from its initialisation value of 'sum / count' to ... anything else ... before it is output ?
 
First off, you are doing your average calculation before any input is given. In the declarations, put only "double average;" and after your while loop terminates, put "average = sum/count;"

Second, count should go up if GPA != -1, not if GPA != 1

third, (though this isn't ruining your program), I would make count an integer since it only ever accepts integer values. During the average calculation, the double divided by an integer will automatically convert the integer to a double to do double division and store that double in average.
 
Ditto what they said.

It's also probably not a good idea to initialize average to the value 0/0.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 24 ·
Replies
24
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 15 ·
Replies
15
Views
3K