0

I am fairly new to c++ and I have a problem that has been stumping me for the longest time. I have to write a program that dynamically allocates two arrays large enough to hold a user-defined number of player names and player scores from a game I created. Allow users to enter the scored in name-score pairs. For each player that has played the game, the user types a string representing the name of the student, followed by an integer representing the player's score. Once the names and the corresponding scored have been entered, the arrays should be passed into a function that sorts the data from the highest score to the lowest score (descending order). Another function should be called that calculates the average score. The program should display the player list from highest scoring player to lowest scoring player and the score averages with appropriate headings. Use pointer notation rather than array notation wherever possible

here is my code:

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

void sortPlayers(string[],int[], int);
void calcAvg(int[], int);

int main()
{
    int *scores;
    string *names;
    int numPlayers,
    count;

    cout << "How many players are there?: " << endl;
    cin >> numPlayers;

    scores = new int[numPlayers];
    names = new string[numPlayers];

    for (count = 0; count < numPlayers; count++)
    {
        cout << "Enter the name and score of player " << (count + 1)<< ":" << endl;
        cin >> names[count] >> scores[count];

    }


    sortPlayers(names, scores, numPlayers);

    cout << "Here is what you entered: " << endl;
    for (count = 0; count < numPlayers; count++)
    {
         cout << names[count]<< " " << scores[count] << endl;
    }
    calcAvg(scores, numPlayers);


    delete [] scores, names;
    scores = 0;
    names = 0;

    return 0;


}

void sortPlayers(string names[], int scores[], int numPlayers)
{
    int startScan, maxIndex, maxValue;
    string tempid;

    for (startScan = 0; startScan < (numPlayers - 1); startScan++)
    {
        maxIndex = startScan;
        maxValue = scores[startScan];
        tempid = names[startScan];
        for(int index = startScan + 1; index < numPlayers; index++)
        {
            if (scores[index] > maxValue)
            {
                maxValue = scores[index];
                tempid = names[index];
                maxIndex = index;
                }
            }
            scores[maxIndex] = scores[startScan];
            names[maxIndex] = names[startScan];
            scores[startScan] = maxValue;
            names[startScan] = tempid;
        }
}

void calcAvg(int scores[], int numPlayers)
{ 
    int total = 0;
    double avg = 0;

    for(int i = 0; i < numPlayers; i++)

        total += scores[numPlayers];

    avg = total/numPlayers; 

    cout << "The average of all the scores is: " << fixed << avg << endl;
}

The sorting part works fine, but I am having trouble with the average displaying properly. It is being displayed as a negative number every time (ex -3157838390) Can anyone help me solve this problem? Does it have anything to do with my pointers?

3
  • the "Here is what you entered" line is just to check to see that the array was working correctly... Aug 31, 2014 at 2:55
  • 5
    I cannot overstress the importance of learning to prepare a minimal complete example.
    – Beta
    Aug 31, 2014 at 2:59
  • 1
    If your using a debugger, i'd put a breakpoint in on the calcAvg function and see what the array looks like and what total is doing etc. Aug 31, 2014 at 3:01

1 Answer 1

2

In this line

total += scores[numPlayers];

you are adding a value from outside of the array. Change it to:

total += scores[i];
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.