Why isn't the math working correctly?

This is homework that was graded last month, but I now have to reuse this as a basis for another program that I have to do. I need to know what I'm doing wrong on the math part to get it to work correctly for the next program.

This program seems to hold the information ok, but when it comes to the grade calculations I get random numbers. Do I need to initiate the variables or is something else going on that I'm not seeing?

What I'm getting looks something like this:

pop tarts -654116545556874659.0 A


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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
  
/*GRADEBOOK - This program will record the first and last names of students, their ID numbers, grades for homework, lab, quizzes,
tests, and the final exam. It will then calculate the grades and display the first and last name of student along with their letter grade.*/

#include <iostream> 
#include <iomanip> 
#include <string>
#include <cstring>
#include <cmath> 

using namespace std; 

					
const int MAXSTDS = 2; //Max Students in class
const int MAXGRDS = 3; //Max Grades per area (ex. 3 graded homework assignments)
const int MINGRDS = 1; //Min Grades per area (ex. 1 graded final test assignment)
const double HMWKPERC = 0.20; //Homework is 20 percent of grade
const double LABPERC = 0.25; //Lab is 25 percent of grade
const double TESTPERC = 0.30; //Tests are 30 percent of grade
const double FEXAMPERC = 0.25; //Final exam is 25 percent of grade


int main() 
{
	//Setting Strings
	string fname[MAXSTDS]; 
	string lname[MAXSTDS]; 
	string stdID[MAXSTDS]; 

						 
	double hmwk[MAXSTDS][MAXGRDS]; 
	double lab[MAXSTDS][MAXGRDS];
	double test[MAXSTDS][MAXGRDS]; 
	double fexam[MAXSTDS][MINGRDS];
	double hmwkTotal[MAXSTDS];
	double labTotal[MAXSTDS]; 
	double testTotal[MAXSTDS]; 
	double fexamTotal[MAXSTDS]; 
	double hmwkAVG[MAXSTDS]; 
	double labAVG[MAXSTDS]; 
	double testAVG[MAXSTDS]; 
	double fexamAVG[MAXSTDS]; 
	double overall[MAXSTDS]; 
	double hmwkOVA[MAXSTDS];
	double labOVA[MAXSTDS];
	double testOVA[MAXSTDS];
	double fexamOVA[MAXSTDS];

							
	for (int i = 0; i < MAXSTDS; i++)
	{
		//system("CLS"); //Clears Screen for the second round
		cout << "\n\nPlease enter the students first name: "; 
		cin >> setw(35) >> fname[i]; //Holding first name info
		cout << "\n\nPlease enter the students last name: "; 
		cin >> setw(35) >> lname[i]; //Holding last name info
		cout << "\n\nPlease enter the students ID number: "; 
		cin >> setw(10) >> stdID[i]; //Holding ID number info

									 
		for (int j = 0; j <= MAXSTDS; j++)
		{
			cout << "Please enter grades for " << fname[j] << " " << lname[j] << ": \n\n"; 
			for (int n = 0; n < MAXGRDS; n++)
			{
				//Gathering homework lesson grades for each student
				cout << "Please enter the grade for homework lesson " << n + 1 << ": ";
				cin >> hmwk[j][n]; //Storing the homework grades
				hmwkTotal[j] += hmwk[j][n];
			}
			hmwkAVG[j] = hmwkTotal[j] / MAXGRDS; 
			hmwkOVA[j] = hmwkAVG[j] * HMWKPERC; 
			for (int r = 0; r < MAXGRDS; r++)
			{
				//Gathering Lab lesson grades for each student
				cout << "Please enter the grade for lab lesson " << r + 1 << ": ";
				cin >> lab[j][r]; //Storing the lab grades
				labTotal[j] += lab[j][r]; 	
			}									  
				labAVG[j] = labTotal[j] / MAXGRDS; 
				labOVA[j] = labAVG[j] * LABPERC; 
			
				for (int q = 0; q < MAXGRDS; q++)
				{
					//Gathering test grades for each student
					cout << "Please enter the grade for test " << q + 1 << ": ";
					cin >> test[j][q]; //Storing the test grades
					testTotal[j] += test[j][q]; 	
				}
				testAVG[j] = testTotal[j] / MAXGRDS; 
				testOVA[j] = testAVG[j] * TESTPERC; 
			
				for (int d = 0; d < MINGRDS; d++)
				{
					//Gathering final test grades for each student
					cout << "Please enter the final test grade: ";
					cin >> fexam[j][d]; 
					fexamTotal[j] += fexam[j][d]; 	
				}
				fexamAVG[j] = testTotal[j] / MINGRDS;
				fexamOVA[j] = fexamAVG[j] * FEXAMPERC; 
			
			

			overall[j] = hmwkOVA[j] + labOVA[j] + testOVA[j] + fexamOVA[j];

			if (overall[j] <= 90)
			{
				cout << fname[j] << " " << lname[j] << " " << setw(5) << fixed << setprecision(2) << overall[j] << setw(5) << "A" << endl;
			}
			else if (overall[j] <= 80)
			{
				cout << fname[j] << " " << lname[j] << " " << setw(5) << fixed << setprecision(2) << overall[j] << setw(5) << "B" << endl;
			}
			else if (overall[j] <= 70)
			{
				cout << fname[j] << " " << lname[j] << " " << setw(5) << fixed << setprecision(2) << overall[j] << setw(5) << "C" << endl;
			}
			else if (overall[j] <= 60)
			{
				cout << fname[j] << " " << lname[j] << " " << setw(5) << fixed << setprecision(2) << overall[j] << setw(5) << "D" << endl;
			}
			else
			{
				cout << fname[j] << " " << lname[j] << " " << setw(5) << fixed << setprecision(2) << overall[j] << setw(5) << "F" << endl;
			}


			system("pause");
			break;
		}

	}
	return 0;
}
Last edited on
Get rid of the loop that begins on line 61. The loop that begins on line 50 is already doing the job of this loop and it's boundary conditions are actually correct.
Last edited on
got rid of the j loop, still giving me the same style answers.
You should initialize the values contained in your arrays, particularly the xxxTotal arrays.
all of my double variables I have I changed to look like this:

double fexamOVA[MAXSTDS] = {};

This is the way to initialize an array, yes?

That will initialize all elements to zero, yes.
That's good, at least I'm at zero this way. What I've done so far is the following. It's still not giving me the correct answers mathematically, but I'm not getting the really screwy numbers like I was getting before.

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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <iostream> //basic, always here
#include <iomanip> //for setw
#include <string> //for string commands
#include <cstring> //for string commands
#include <cmath> //for math commands

using namespace std; //keeps the code shorter for the io commands

					 //Setting Constants these can be adjusted at a later date if necessary
const int MAXSTDS = 2; //Max Students in class
const int MAXGRDS = 3; //Max Grades per area (ex. 3 graded homework assignments)
const int MINGRDS = 1; //Min Grades per area (ex. 1 graded final test assignment)
const double HMWKPERC = 0.20; //Homework is 20 percent of grade
const double LABPERC = 0.25; //Lab is 25 percent of grade
const double TESTPERC = 0.30; //Tests are 30 percent of grade
const double FEXAMPERC = 0.25; //Final exam is 25 percent of grade


							   //Main Program Area
int main() //gotta start somewhere
{
	//Setting Strings
	string fname[MAXSTDS]; //First Name with a maximum that matches the number in MAXSTDS
	string lname[MAXSTDS]; //Last Name with a maximum that matches the number in MAXSTDS
	string stdID[MAXSTDS]; //Student ID number with a maximum that matches the number in MAXSTDS

						   //Setting other variables as needed
	double hmwk[MAXSTDS][MAXGRDS] = {}; //Gets the grades for each student and holds them according to the student number
	double lab[MAXSTDS][MAXGRDS] = {}; //Gets the grades for each student and holds them according to the student number
	double test[MAXSTDS][MAXGRDS] = {}; //Gets the grades for each student and holds them according to the student number
	double fexam[MAXSTDS][MINGRDS] = {}; //Gets the grades for each student and holds them according to the student number
	double hmwkTotal[MAXSTDS] = {}; //Holds total homework grade for each student
	double labTotal[MAXSTDS] = {}; //Holds total lab grade for each student
	double testTotal[MAXSTDS] = {}; //Holds total test grade for each student
	double fexamTotal[MAXSTDS] = {}; //Holds total final exam grade for each student
	double hmwkAVG[MAXSTDS] = {}; //Holds average homework grade for each student
	double labAVG[MAXSTDS] = {}; //Holds average lab grade for each student
	double testAVG[MAXSTDS] = {}; //Holds average test grade for each student
	double fexamAVG[MAXSTDS] = {}; //Holds average final exam grade for each student
	double overall[MAXSTDS] = {}; //Holds overall grade for each student
	double hmwkOVA[MAXSTDS] = {};//Holds hmwk percent total
	double labOVA[MAXSTDS] = {};//Holds hmwk percent total
	double testOVA[MAXSTDS] = {};//Holds hmwk percent total
	double fexamOVA[MAXSTDS] = {};//Holds hmwk percent total



							 //First for loop getting student information
	for (int j = 0; j < MAXSTDS; j++)
	{
		//system("CLS"); //Clears Screen for the second round
		cout << "\n\nPlease enter the students first name: "; //Asking for first name
		cin >> setw(35) >> fname[j]; //Holding first name info
		cout << "\n\nPlease enter the students last name: "; //Asking for last name 
		cin >> setw(35) >> lname[j]; //Holding last name info
		cout << "\n\nPlease enter the students ID number: "; //Asking for student ID number
		cin >> setw(10) >> stdID[j]; //Holding ID number info

									 //Nested for loop gathering the homework grades

		cout << "Please enter grades for " << fname[j] << " " << lname[j] << ": \n\n"; //Starts the grade gathering loops


		for (int n = 0; n < MAXGRDS; n++)
		{
			//Gathering homework lesson grades for each student
			cout << "Please enter the grade for homework lesson " << n + 1 << ": ";
			cin >> hmwk[j][n]; //Storing the homework grades
			hmwkTotal[j] += hmwk[j][n]; //adding homework grades to get a total per student
		}
		hmwkAVG[j] = hmwkTotal[j] / MAXGRDS; //Homework average
		hmwkOVA[j] = hmwkAVG[j] * HMWKPERC; //calculating the homework overall percentage

		for (int r = 0; r < MAXGRDS; r++)
		{
			//Gathering Lab lesson grades for each student
			cout << "Please enter the grade for lab lesson " << r + 1 << ": ";
			cin >> lab[j][r]; //Storing the lab grades
			labTotal[j] += lab[j][r]; //adding lab grades to get a total per student	
		}
		labAVG[j] = labTotal[j] / MAXGRDS; //Lab average
		labOVA[j] = labAVG[j] * LABPERC; //calculating the lab overall percentage

		for (int q = 0; q < MAXGRDS; q++)
		{
			//Gathering test grades for each student
			cout << "Please enter the grade for test " << q + 1 << ": ";
			cin >> test[j][q]; //Storing the test grades
			testTotal[j] += test[j][q]; //adding test grades to get a total per student	
		}
		testAVG[j] = testTotal[j] / MAXGRDS; //Test average
		testOVA[j] = testAVG[j] * TESTPERC; //calculating the lab overall percentage

		for (int d = 0; d < MINGRDS; d++)
		{
			//Gathering final test grades for each student
			cout << "Please enter the final test grade: ";
			cin >> fexam[j][d]; //Storing the final grade
			fexamTotal[j] += fexam[j][d]; //adding final grade to get a total per student	
		}
		fexamAVG[j] = testTotal[j] / MINGRDS; //Final Average

		fexamOVA[j] = fexamAVG[j] * FEXAMPERC; //Calculating the lab overall percentage



		overall[j] = hmwkOVA[j] + labOVA[j] + testOVA[j] + fexamOVA[j];

	}
			system("pause");


			for (int r = 0; r < MAXSTDS; r++)
			{
				if (overall[r] >= 90 && overall[r] <= 100)
				{
					cout << fname[r] << " " << lname[r] << " " << setw(5) << fixed << setprecision(2) << overall[r] << setw(5) << "A" << endl;
				}
				else if (overall[r] >= 80 && overall[r] <= 89)
				{
					cout << fname[r] << " " << lname[r] << " " << setw(5) << fixed << setprecision(2) << overall[r] << setw(5) << "B" << endl;
				}
				else if (overall[r] >= 70 && overall[r] <= 79)
				{
					cout << fname[r] << " " << lname[r] << " " << setw(5) << fixed << setprecision(2) << overall[r] << setw(5) << "C" << endl;
				}
				else if (overall[r] >= 60 && overall[r] <= 69)
				{
					cout << fname[r] << " " << lname[r] << " " << setw(5) << fixed << setprecision(2) << overall[r] << setw(5) << "D" << endl;
				}
				else
				{
					cout << fname[r] << " " << lname[r] << " " << setw(5) << fixed << setprecision(2) << overall[r] << setw(5) << "F" << endl;
				}
				system("pause");
			}
		

		
	return 0;
}
Just eyeballing, this doesn't look right:
fexamAVG[j] = testTotal[j] / MINGRDS; //Final Average
YES!! that was the goof I couldn't find. Thanks a ton cire!!

Now onto working on the program this one has to have to work.
Last edited on
Topic archived. No new replies allowed.