array traversal

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

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
  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

}
Last edited on
you need to initialize at least totalScore with 0 at the beginning otherwise you will get some random numbers.

It's better to use curly braces {} for the loop. So you can see what's inside the loop and what not.

the loop might be saver if you write it like so:

for (tempPtr = arrayStart; tempPtr <= arrayEnd; tempPtr++)
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().

Hope this helps. Good Luck and happy coding.
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 (:
Topic archived. No new replies allowed.