0

I am writing a function to search through an array that stores the score of some players for the highest scoring player(s). The problem is that the I can search and record the highest score easily but it is when there are multiple of a score that my logic falls appart.

My code is attached below:

/* Function display_highest_score() searches the player score array for the highest value and
   any repetitions before printing the results to the screen.
   Parameters: The player array, score array, and number of players.
   Returns: Nothing.*/
void display_highest_score(char player_array[][STRING_LENGTH], int score_array[], int num_players) {

    int high_num = 0; /* Stores the highest player score. */
    int position[MAX_PLAYERS] = { 0 }; /* Stores the position of the player(s) with the highest score. */
    int players = 1; /* Stores the number of players that have that score. */
    int j = 0; /* A looping variable. */

    /* Loop to find and store the highest scoring player(s). */
    for (int i = 0; i < num_players; i++) {

        if (high_num < score_array[i]) {

            high_num = score_array[i];
            position[j] = i;
            j++;

        }
        else if (high_num == score_array[i]) {

            players = players++;

        }

    }

    /* Print to screen code. */
    printf("\nPlayers with the highest score of %d are:\n\n", high_num);

    for (j = 0; j < players; j++) {

        printf("--> %s\n", player_array[position[j]]);

    }

}

If the function is searching through an array, say score_array[10] = { 3, 1, 0, 10, 4, 8, 10, 0, 3, 16 } then it will change the maximum score to 16 but it will print the players that have score 10 as having 16 instead.

I've tried to swap the position[j] = i; j++; segment to the else if statement but it still doesnt work

I'm still relativley new to c coding so any help would be appreciated.

Nath
  • 3
  • 2
  • Why do you need an array of positions? First find highest score, then loop again and print players with that score. – stark Jun 08 '23 at 13:36
  • `players = players++;` invokes [Undefined Behaviour](https://en.cppreference.com/w/c/language/behavior). See [here](https://stackoverflow.com/a/3575375/2505965). You probably meant just `players++;` or `players += 1;` (this will not solve your problem completely, though). – Oka Jun 08 '23 at 13:40

3 Answers3

0

What you are currently doing is adding a position index for every new high score that you see. The players variable will only increment when it sees a repeat high score but will not record that players index.

What you need to do is if a new high score is seen (high_num < score_array[i]) then you should clear the positions array and reset number of players back to 0, in order to clear out previously detected high scores and start fresh with the new value. If you set j = 0 and players =1 in the if before assigning position[j] = i; it should only show the highest score values. In the else statement, you need to add

position[j] = i;
j++

In order to track all high scores that are equal. At that point you don't need players as a separate variable since players will always equal j.

0

You need to add the current position in all cases, but clear the previously stored positions when a new high score is found. Also, you should initialize players to 0 to handle the corner case of an empty set of players (in any case, you should print an error message in case players==0 at the end):

    int players = 0;

...
    if (high_num <= score_array[i]) {
        if (high_num < score_array[i]) {
             high_num = score_array[i];
             players = 0;
        }
        position[players++] = i;
    }
...
nielsen
  • 5,641
  • 10
  • 27
0

I would strongly recommend approaching this with different logic. The easiest solution would be using one loop to find the highest score, and then another loop to print out every player with that score. Something a bit like this pseudocode:

for (list of scores):
     if score is new high score:
          store new high score

for (list of players):
     if player has high score:
          print player

If you still really want to stick with the code you have, the first thing I'd look at is your players value that stores the number of players with a high score. I think you should be setting it to 1 whenever you find a new high score, and then incrementing it whenever there's a tie, but you appear to be skipping that first part and only increasing the value when there is a tie for the high score.

Hope this helps.

Matt
  • 1
  • 1