Values Phantom Change
Nov 29, 2016 at 7:27am UTC
Somehow my values change without me doing anything, and i have no idea why. somehow for my quizzes[2] if go from having 5 10 12.5 to 85 100 25 which is the same as my midtermExam. However I never set those values for quizzes[2].
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
struct GradedItem
{
int pointsEarned;
int pointsPossible;
double weight;
};
struct Student
{
GradedItem midtermExam;
GradedItem finalExam;
GradedItem quizzes[2];
};
/***************************************************************
global variables (rare)
***************************************************************/
//none
/***************************************************************
New function prototypes. Include a description of the function
input(s) and the function output. Often some data is helpful:
int largest( int, int, int ); // largest(4,5,-1) returns 5
***************************************************************/
double getCourseGrade(const Student);
void initializeStudentStruct(Student&);
/***************************************************************
main() function code.
The main() function always takes zero parameters/arguments
and returns 0 to indicate that the input was successfully
processed into output.
Later: A return of [some other value] means [something else].
****************************************************************/
int main()
{
int exitCode = 0; // How did the program end. 0 is successful, 3 is file connection failure
Student test;
initializeStudentStruct(test);
test.quizzes[1].pointsEarned = 7;
test.quizzes[2].pointsEarned = 5;
cout << test.quizzes[2].pointsEarned << endl;
cout << test.quizzes[2].pointsPossible << endl;
cout << test.quizzes[2].weight << endl;
test.midtermExam.pointsEarned = 85;
cout << test.quizzes[2].pointsEarned << endl;
cout << test.quizzes[2].pointsPossible << endl;
cout << test.quizzes[2].weight << endl;
test.finalExam.pointsEarned = 73;
cout << test.quizzes[2].pointsEarned << endl;
cout << test.quizzes[2].pointsPossible << endl;
cout << test.quizzes[2].weight << endl;
cout << getCourseGrade(test);
cout << "\nGood-Bye\n" ; // say good-bye
system("pause" ); // why is this here? What does it do? Pauses the screen so the user can look at it
return exitCode; // return 0 to indicate successful execution of main(), 3 indicates failure to open file
}
/***************************************************************
Code for other functions
Use the main() comment above as the model.
***************************************************************/
double getCourseGrade(const Student scores)
{
cout << scores.quizzes[2].pointsEarned << endl;
cout << scores.quizzes[2].pointsPossible << endl;
cout << scores.quizzes[2].weight << endl;
double finalGrade = 0.0;
// weight q1 % * ( earned/possible * 100 )
finalGrade = ( (scores.quizzes[1].weight * (static_cast <double >(scores.quizzes[1].pointsEarned) / static_cast <double >(scores.quizzes[1].pointsPossible )) ));
cout << ( (scores.quizzes[1].weight * (static_cast <double >(scores.quizzes[1].pointsEarned) / static_cast <double >(scores.quizzes[1].pointsPossible )) )) << endl;
// + weight q2 % * ( earned/possible * 100 )
finalGrade = finalGrade + ( (scores.quizzes[2].weight * (static_cast <double >(scores.quizzes[2].pointsEarned) / static_cast <double >(scores.quizzes[2].pointsPossible )) ));
cout << scores.quizzes[2].pointsEarned << endl;
cout << scores.quizzes[2].pointsPossible << endl;
cout << scores.quizzes[2].weight << endl;
cout << ( (scores.quizzes[2].weight * (static_cast <double >(scores.quizzes[2].pointsEarned) / static_cast <double >(scores.quizzes[2].pointsPossible )) )) << endl;
// + weight midterm % * ( earned/possible * 100 )
finalGrade = finalGrade + ( (scores.midtermExam.weight * (static_cast <double >(scores.midtermExam.pointsEarned) / static_cast <double >(scores.midtermExam.pointsPossible )) ));
cout << ( (scores.midtermExam.weight * (static_cast <double >(scores.midtermExam.pointsEarned) / static_cast <double >(scores.midtermExam.pointsPossible )) )) << endl;
// weight final % * ( earned/possible * 100 )
finalGrade = finalGrade + ( (scores.finalExam.weight * (static_cast <double >(scores.finalExam.pointsEarned) / static_cast <double >(scores.finalExam.pointsPossible )) ));
cout << ( (scores.finalExam.weight * (static_cast <double >(scores.finalExam.pointsEarned) / static_cast <double >(scores.finalExam.pointsPossible )) )) << endl;
cout << finalGrade <<endl;
cout << (scores.quizzes[1].weight + scores.quizzes[2].weight + scores.midtermExam.weight + scores.finalExam.weight) << endl;
// sum of grades / total weight
finalGrade = finalGrade / (scores.quizzes[1].weight + scores.quizzes[2].weight + scores.midtermExam.weight + scores.finalExam.weight)*100.0;
return finalGrade;
}
void initializeStudentStruct(Student& subject)
{
subject.quizzes[1].pointsEarned = 0;
subject.quizzes[1].pointsPossible = 10;
subject.quizzes[1].weight = 12.5;
subject.quizzes[2].pointsEarned = 0;
subject.quizzes[2].pointsPossible = 10;
subject.quizzes[2].weight = 12.5;
subject.midtermExam.pointsEarned = 0;
subject.midtermExam.pointsPossible = 100;
subject.midtermExam.weight = 25.0;
subject.finalExam.pointsEarned = 0;
subject.finalExam.pointsPossible = 100;
subject.finalExam.weight = 50.0;
return ;
}
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
output:
5
10
12.5
5
10
12.5
5
10
12.5
85
100
25
8.75
85
100
25
21.25
21.25
36.5
87.75
112.5
78
Good-Bye
Last edited on Nov 29, 2016 at 7:29am UTC
Nov 29, 2016 at 7:35am UTC
Your array indexing is wrong.
An array of length N has indices [0, N-1], rather than [1, N].
Nov 29, 2016 at 2:13pm UTC
I hate myself
Topic archived. No new replies allowed.