I think the code comments show what is needed.

Essentially, I just need to spit out the members name and score who are below the average score in the list.

void displayLowScore(struct payload *arrayStart  , //first element of array
                  struct payload *arrayEnd    ) //last  element of array
{
   int             memberCount = 0; //total number of members in list/array
   int             totalScore = 0; //accumulated value of all scores added together 
   float           averageScore; //totalScore / memberCount
   struct payload *tempPtr     ; //pointer for traversing

   // label
   // -----
   cout << "Members below average: ";

   // Traverse array accumulating totalScore.
   // (Don't destroy any of the parameters ...
   // you need them for a second traverse: use
   // tempPtr).
   // -------------------------------------------
	for (tempPtr = arrayStart; tempPtr != arrayEnd+1; ++tempPtr)
		totalScore = totalScore + tempPtr; // cannot get this to work correctly. Current code gives exception.


   // calculate member average
   // --------------------------
   if (memberCount == 0)
      averageScore =  0;
   else
      averageScore = (float)totalScore / (float)memberCount;
   cout << averageScore << endl;
   
   // Traverse array & output every member & score below the average
   // Make sure to handle your traversal pointer in both cases:
   // member below average & member>= average.
   // ---------------------------------------------------------------
   //CODE NEEDED HERE
   //CODE NEEDED HERE

Recommended Answers

All 14 Replies

Why are you using float? Line 6 and 27. Use int instead of float.

Why are you using float? Line 6 and 27. Use int instead of float.

I guess cause "average" is a division, so it considers a number with a decimal part

Do he needs to use float? Average can be obtained in a division using integers.
The result doesn´t need to be a number with a decimal.

I don't know if it NEEDS to be it... I didn't not understand his question anyway, have you?

The result doesn´t need to be a number with a decimal.

How do you know? 0_o

Anyway, we need more info:
How is this function called? (e.g. how are arrayStart and arrayEnd initialized).

Could it be a problem of an edge case?

for (tempPtr = arrayStart; tempPtr != arrayEnd+1; ++tempPtr)

Do a print here, and call the function with, say, 3 elements... check if there are 3 iterations - or are there 4?

Clarify 'gives exception'

This is the reason. Formula

Average = Totalscore / Membercount;

The average of the totalscore must be an integer because totalscore is an integer.
It cannot be a number with a decimal because totalscore is an integer.

commented: What if Totalscore is 5 and Membercount is 2? I think you are confused. -1

How do you know? 0_o

Anyway, we need more info:
How is this function called? (e.g. how are arrayStart and arrayEnd initialized).

Could it be a problem of an edge case?

for (tempPtr = arrayStart; tempPtr != arrayEnd+1; ++tempPtr)

Do a print here, and call the function with, say, 3 elements... check if there are 3 iterations - or are there 4?

Clarify 'gives exception'

I call it by passing an array of members and a pointer.
i.e.

struct memberInfo {
    string memberName;
    int score;
}

int main() {
struct memberInfo myArray[20];
struct memberInfo *arrayPtr = &myArray[0];

...

displayLowScore(&myArray[0], arrayPtr-1);

}

The exception i get is error C2440: '=' : cannot convert from 'memberInfo*' to 'int' on the for loop.

I need it to be a float so it will show decimal values of the average score. I can't change the ints to floats because requirements don't want the ability to add decimals.

I guess what I'm looking for is how exactly do I traverse the array?

EDIT:

struct payload *tempPtr ; //pointer for traversing

should be

struct memberInfo *tempPtr ; //pointer for traversing

well can I see your payload struct?
Is that a List structure? Like

struct payload{
int integer;
payload *next;
}

?
If so you should move through the list (if it is a list, cause I still don't get it) not by summing the pointer, but going like
for (tempptr = arrayStart; tempPtr->next !=NULL ;tempPtr = tempPtr->next)

I hope someone else answers you cause I'm gonna sleep. Good luck or see you tomorrow

well can I see your payload struct?
Is that a List structure? Like

struct payload{
int integer;
payload *next;
}

?
If so you should move through the list (if it is a list, cause I still don't get it) not by summing the pointer, but going like
for (tempptr = arrayStart; tempPtr->next !=NULL ;tempPtr = tempPtr->next)

I hope someone else answers you cause I'm gonna sleep. Good luck or see you tomorrow

Thanks for your help. I've change my for loop to emulate what you have there since the structs are just about the same (see the post above your post and you'll see it's format).

I still, however, get the conversion exception. This fever I have is really inhibiting my brain functions and I'm not understanding seemingly simple operations :(

The reason you get error C2440 is in line 19:

totalScore = totalScore + tempPtr;

You are attempting to add an int (totalScore) and a memberInfo struct pointer (tempPtr).

What you want to do is add the score-member of the memberInfo struct, like this:

totalScore = totalScore + tempPtr->score;

The reason you get error C2440 is in line 19:

totalScore = totalScore + tempPtr;

You are attempting to add an int (totalScore) and a memberInfo struct pointer (tempPtr).

What you want to do is add the score-member of the memberInfo struct, like this:

totalScore = totalScore + tempPtr->score;

Even with that change I'm still getting the error. Full error is as follows:
error C2440: '=' : cannot convert from 'int' to 'memberInfo*'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast

I narrowed it down to the actual loop. The calculation is ok but the loop is giving the error. It seems that the tempPtr is being set equal to 'arrayStart' then the last statement it is being set to 'score'. Is this a problem or am I misinterpreting something.

I narrowed it down to the actual loop. The calculation is ok but the loop is giving the error. It seems that the tempPtr is being set equal to 'arrayStart' then the last statement it is being set to 'score'. Is this a problem or am I misinterpreting something.

Can you post the code with fewer comments, but with declarations also?
Declaration of what you're using

Can you post the code with fewer comments, but with declarations also?
Declaration of what you're using

Sorry for the very late reply. I was just diagnosed with Strep throat. Guess that explains the really high fevers, haha.

Here's the code:

struct memberInfo
{
   string memberName;
   int    score;
};

...

int main()
{
   int count;
   struct memberInfo masterArray[50];
   struct memberInfo *arrayPtr = &masterArray[0];

   cout << "Member Stats" << endl;
   cout << "*******************************" << endl;

   count = 0;
   while (arrayPtr <= &masterArray[ARRAY_SIZE-1])
   {
      bool status;
      char name[40];

      cout << setw(2) << count++ << setw(2) << ": Member Name?: ";
      gets_s(name,sizeof(name));
      if (name[0] == NULL)
         break;
      arrayPtr->memberName = name;
      cout << "    Score?: ";
      cinGetValue(arrayPtr->score,status); 

      arrayPtr++;
   }
   cout << endl;

   if (arrayPtr == &masterArray[0])
      cout << endl << "<ERROR: Member list is empty>" << endl;
   else
   {
      cout << endl;
      displayArray(&masterArray[0],arrayPtr-1);
   
      displayLowScore (&masterArray[0],arrayPtr-1);
      cout << endl;

      displayHighScore(&masterArray[0],arrayPtr-1);
   }
}

...


void displayLowScore(struct memberInfo *arrayStart, struct memberInfo*arrayEnd)
{
   int memberCount = 0;
   int totalScore = 0;
   float averageScore;
   struct memberInfo *tempPtr;			

   cout << "Students below average score: ";

   for (tempPtr = arrayStart; tempPtr->testScore !=NULL; tempPtr = tempPtr->testScore)
       totalScore = totalScore + tempPtr->testScore;

   if (studentCount == 0)
      averageScore =  0;
   else
      averageScore = (float)totalScore / (float)studentCount;
   cout << averageScore << endl;
   
   //TODO: Display members who have a score lower than the average score.
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.