I was wondering if I could get any input and/ or suggestions as to how to traverse through this array and output each student and score that falls below the average. I think I may have the method down for traversing through the list to find the "total score" however, before proceeding under a possible wrong presumption, I would appreciate/ like confirmation as to whether or not I'm understanding this or botching this concept. Thank you for your time and any suggestions. *Please note* I am not asking nor want anyone to provide any code for the "//ADD CODE" section but, rather, if my for loop and totalScore algorithm is logical so far
void dispLowScore(struct payload *arrayStart , //first element of array
struct payload *arrayEnd ) //last element of array
{
int studentCount; //total number of students in list/array
int totalScore ; //accumulated value of all scores added together
float averageScore; //totalScore / studentCount
struct payload *tempPtr ; //pointer for traversing
cout << "Students below average score: ";
// Traverse array to accumulate totalScore using a temporary pointer (2 lines of code)//
for (tempPtr = arrayStart; tempPtr->testScore != NULL; tempPtr++)
totalScore = totalScore + tempPtr->testScore;
// Calculate average score //
if (studentCount == 0)
averageScore = 0;
else
averageScore = (float)totalScore / (float)studentCount;
cout << averageScore << endl;
// Traverse array and output every student and score below the average by
// handling the traversal pointer in both cases wherein...
// ...student < average and, student >= average ... using 3 lines of code
//ADD CODE
dispStudent(tempPtr);
//ADD CODE
}
I don't want to give the solution away being that would be cheating. I don't think your data structures teacher would like that. What I would do is first find a way to initialize studentCount , totalScore, and averageScore in one line. Maybe inside the argument for the for loop.
then maybe use something along the lines of +=.
for the output maybe loop through the array again and check against an if statement before calling dispstudent().
Thanks, coder and nemisis, for the tips and for not giving away the answer; it was important I learned this on my own and not just because I want a good grade but rather because, I'm bound to run into this later in life and should know how to read + understand what is going on. You've both been very helpful (: