Home > Software design >  C - How to cout the person with the highest score in a program
C - How to cout the person with the highest score in a program

Time:12-03

I'm working on a program that allows the user to input some names and integers for a soccer game, ie input the player's name, jersey number, & points scored, and then prints it all at the end.

How would I go about finding the player's name who scored the most points, and print that?

This is my incomplete code below:

void showHighest(Player p[], int size)
{
    int high = 0;

    for (int counter = 0; counter < size; counter  )
    {
        if (high < p[counter].points)
        {
            high = p[counter].points;
        }

        if (p[counter].points   )
        {

        }
    }
    cout << "The player with the most points was: " << p[high].name << "with " << high << "amount of points." << endl;
}

CodePudding user response:

You need to track the index of the highest player in the array, not just the highest points. In your cout statement, you are using the highest points as if it were an index, which it is not.

Try this instead:

void showHighest(Player p[], int size)
{
    int highest_points = 0;
    int highest_index = -1;

    for (int counter = 0; counter < size;   counter)
    {
        if (highest_points < p[counter].points)
        {
            highest_index = counter;
            highest_points = p[counter].points;
        }
    }

    if (highest_index != -1)
        cout << "The player with the most points was: " << p[highest_index].name << " with " << highest_points << " amount of points." << endl;
}

Alternatively, you can initialize the tracking variables to the info of the 1st player, and start the loop at the 2nd player, eg:

void showHighest(Player p[], int size)
{
    if (size < 1) return;

    int highest_points = p[0].points;
    int highest_index = 0;

    for (int counter = 1; counter < size;   counter)
    {
        if (highest_points < p[counter].points)
        {
            highest_index = counter;
            highest_points = p[counter].points;
        }
    }

    cout << "The player with the most points was: " << p[highest_index].name << " with " << highest_points << " amount of points." << endl;
}

Alternatively, use a pointer to track the Player with the highest points, eg:

void showHighest(Player p[], int size)
{
    if (size < 1) return;

    Player* highest_player = &p[0];

    for (int counter = 1; counter < size;   counter)
    {
        if (highest_player->points < p[counter].points)
        {
            highest_player = &p[counter];
        }
    }

    cout << "The player with the most points was: " << highest_player->name << " with " << highest_player->points << " amount of points." << endl;
}

Alternatively, you can use the standard std::max_element() algorithm to find the Player with the highest points without using a manual loop, eg:

#include <algorithm>

void showHighest(Player p[], int size)
{
    if (size < 1) return;

    auto p_end = p   size;
    auto it = std::max_element(p, p_end,
        [](const Player &a, const Player &b){
            return a.points < b.points;
        }
    );

    cout << "The player with the most points was: " << it->name << " with " << it->points << " amount of points." << endl;
}

CodePudding user response:

A good solution will be to set the change the 'high' integer into a player object. Then instead of holding only the highest score you will hold the player object.

void showHighest(Player p[], int size)
{
    Player* high = new Player;

    for (int counter = 0; counter < size; counter  )
    {
        if (high.points < p[counter].points)
        {
            high = p[counter];
        }

    }
    cout << "The player with the most points was: " << high.name << "with " << high.points << "amount of points." << endl;
}

Obviously there are many other ways as suggested above. Another way which is less effective is to "remember" the highest score value (as you do) and then check which player has this amount of points:

    void showHighest(Player p[], int size)
    {
        int high = 0;
    
        for (int counter = 0; counter < size; counter  )
        {
            if (high < p[counter].points)
            {
                high = p[counter].points;
            }
        }
        string name = "";
        for (int counter = 0; counter < size; counter   )
        {
          if(player[counter].points == high)
          {
             name = p[counter].name;
          }
        }
        cout << "The player with the most points was: " << name << "with" << high << "amount of points." << endl;
    }

you can find many other ways. I would recomend the first one due the use of the OOP advanteges you have at C .

  •  Tags:  
  • c
  • Related