Calculating GPA: Find the Average

In summary, The program is designed to prompt the user to enter several GPAs and calculate the average. However, there are a few errors in the code that need to be fixed. First, the average calculation is being done before any input is given. Second, the count variable should only increase if the GPA is not equal to -1. Lastly, the count variable should be an integer since it only accepts integer values. It is also recommended to initialize the average variable to a different value than 0/0.
  • #1
magnifik
360
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
  • #2
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 ?
 
  • #3
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.
 
  • #4
Ditto what they said.

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


I understand how frustrating it can be when a program isn't working as expected. It's important to troubleshoot and identify the issue in order to find a solution. In this case, it seems like the program is not properly counting the number of GPAs entered, which is affecting the calculation of the average.

One possible solution could be to use a separate variable to keep track of the number of GPAs entered, rather than relying on the GPA variable. This way, the count will accurately reflect the number of GPAs entered, and the average calculation will be correct.

Another potential issue could be the use of the -1 as the sentinel value. It's important to make sure that the user is aware of this value and knows to enter it to end the program. Providing clear instructions and prompts can help avoid any confusion.

Overall, it's important to thoroughly test and debug the program to ensure it is functioning as intended. Making small adjustments and identifying any potential errors can help improve the program and make it more user-friendly.
 

1. How is GPA calculated?

To calculate your GPA, you must first determine the grade points for each class. These are typically on a 4.0 scale, with A=4, B=3, C=2, D=1, and F=0. Then, multiply the number of credits for each class by its grade points. Add up the total grade points for all classes and divide by the total number of credits. This will give you your GPA.

2. Are all classes counted in GPA calculation?

No, not all classes are counted in GPA calculation. Only classes that are taken for credit are included. This typically includes core classes, electives, and some extracurricular courses. Classes that are audited or taken pass/fail are not included in GPA calculation.

3. How do weighted classes affect GPA?

Weighted classes, such as honors or AP courses, may have a different scale for assigning grade points. For example, an A in an honors class may be worth 4.5 grade points instead of 4. This can result in a higher GPA. However, different schools may have different policies on how weighted classes are factored into GPA calculation.

4. Can I calculate my GPA for a specific semester or year?

Yes, you can calculate your GPA for a specific semester or year by only including the classes taken during that time period. This can be helpful for tracking academic progress and setting goals for improvement.

5. How do I calculate my cumulative GPA?

To calculate your cumulative GPA, you will need to have your grades and credits for all classes taken throughout your academic career. Follow the same steps as calculating overall GPA, but include all classes and credits. This will give you an average of your performance over all semesters or years.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
754
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
24
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
840
  • Engineering and Comp Sci Homework Help
Replies
6
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
Back
Top