Calculating GPA: Find the Average

  • Thread starter Thread starter magnifik
  • Start date Start date
  • Tags Tags
    Average Gpa
AI Thread Summary
The program for calculating GPA is malfunctioning due to the average being calculated before any input is processed. The average variable should only be assigned a value after the while loop concludes, specifically with "average = sum/count." Additionally, the count variable should increment when GPA is not equal to -1, rather than 1. It's suggested to declare count as an integer since it only holds whole number values. Proper initialization of average is also crucial to avoid division by zero errors.
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.
 
Thread 'Have I solved this structural engineering equation correctly?'
Hi all, I have a structural engineering book from 1979. I am trying to follow it as best as I can. I have come to a formula that calculates the rotations in radians at the rigid joint that requires an iterative procedure. This equation comes in the form of: $$ x_i = \frac {Q_ih_i + Q_{i+1}h_{i+1}}{4K} + \frac {C}{K}x_{i-1} + \frac {C}{K}x_{i+1} $$ Where: ## Q ## is the horizontal storey shear ## h ## is the storey height ## K = (6G_i + C_i + C_{i+1}) ## ## G = \frac {I_g}{h} ## ## C...

Similar threads

Replies
2
Views
3K
Replies
6
Views
4K
Replies
5
Views
2K
Replies
3
Views
1K
Replies
3
Views
1K
Replies
2
Views
2K
Replies
8
Views
1K
Replies
15
Views
3K
Back
Top