### Help With Average

I have been doing this project for an assignment and every time I run it with any 3 numbers as the grades, the average comes out as a huge negative number, as if I were trying to divide two integers. But I am using floating point, so I am not sure what the problem is. I would love any help I can get, thanks!

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141`` `````` #include #include #include using namespace std; const int MAXGRADE = 25; // maximum number of grades per student const int MAXCHAR = 30; // maximum characters used in a name typedef char StringType30[MAXCHAR + 1]; // character array data type for names // having 30 characters or less. typedef float GradeType[MAXGRADE]; // one dimensional integer array data type float findGradeAvg(GradeType, int); // finds grade average by taking array of // grades and number of grades as parameters char findLetterGrade(float); // finds letter grade from average given // to it as a parameter int main() { StringType30 firstname, lastname; //two arrays of characters defined int numOfGrades; // holds the number of grades GradeType grades; //grades defined as a one dimensional array float average; //holds the average of a student's grade char moreInput; //determines if there is more input cout << setprecision(2) << fixed << showpoint; // Input the number of grades for each student cout << "Please input the number of grades each student will receive." << endl << "This must be a number between 1 and " << MAXGRADE << " inclusive" << endl; cin >> numOfGrades; while (numOfGrades > MAXGRADE || numOfGrades < 1) { cout << "Please input the number of grades for each student." << endl << "This must be a number between 1 and " << MAXGRADE << " inclusive\n"; cin >> numOfGrades; } // Input names and grades for each student cout << "Please input a y if you want to input more students" << " any other character will stop the input" << endl; cin >> moreInput; while (moreInput == 'y' || moreInput == 'Y') { cout << "Please input the first name of the student" << endl; cin >> firstname; cout << endl << "Please input the last name of the student" << endl; cin >> lastname; for (int count = 0; count < numOfGrades; count++) { cout << endl << "Please input a grade" << endl; cin >> grades[count]; // Fill in the input statement to place grade in the array } average = findGradeAvg(grades, numOfGrades); cout << firstname << " " << lastname << " has an average of "; cout << average; cout << " which gives the letter grade of " << findLetterGrade(findGradeAvg(grades, numOfGrades)); // Fill in code to get and print average of student to screen // Fill in call to get and print letter grade of student to screen cout << endl << endl << endl; cout << "Please input a y if you want to input more students" << " any other character will stop the input" << endl; cin >> moreInput; } _getch(); return 0; } //*********************************************************************** // findGradeAvg // // task: This function finds the average of the // numbers stored in an array. // // data in: an array of integer numbers // data returned: the average of all numbers in the array // //*********************************************************************** float findGradeAvg(GradeType array, int numGrades) { float average = 0.0; for (int counter = 0; counter <= numGrades; counter++) { average += array[counter]; } average = ((float)average)/numGrades; return average; } //*********************************************************************** // findLetterGrade // // task: This function finds the letter grade for the number // passed to it by the calling function // // data in: a floating point number // data returned: the grade (based on a 10 point spread) based on the number // passed to the function // //*********************************************************************** char findLetterGrade(float numGrade) { if (numGrade <= 100 && numGrade >= 90) return 'A'; else if (numGrade <= 89 && numGrade >= 80) return 'B'; else if (numGrade <= 79 && numGrade >= 70) return 'C'; else if (numGrade <= 69 && numGrade >= 60) return 'D'; else return 'F'; } ``````
Line 63: You're (correctly) looping for numOfGrades times:
`for (int count = 0; count < numOfGrades; count++)`
Line 107: You're looping an extra time, and receiving junk:
`for (int counter = 0; counter <= numGrades; counter++)`

Also, kinda sucks that that I got an F for doing the extra credit and getting a 101%. But I ain't even mad.

Edit: fixed line numbers
Last edited on
Thank you so much! I didn't see that even after staring at this for an hour.
You're welcome. I highly suggest using what's called a "debugger", which is available in most modern IDEs. A debugger lets you step through the code line by line, or with break points, so that you can get more information on the state of your program at any given line. Stepping through the code line by line could help you see that the for loop was being executed one-too-many times.

(A poor man's debugger is lots and lots of print (cout) statements at every questionable part of your program.)
Last edited on
Topic archived. No new replies allowed.