Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Programming in C need help/correction with topTen function (will be called from

ID: 3900524 • Letter: P

Question

Programming in C

need help/correction with topTen function (will be called from menu in main) (score range is 17-100, lower the better) (battleship)

function will need to be able to:

Display top ten scores

A playCount will be needed to keep track of how many games

get score from a finish game, place and or compare it to scores in array and place it in the array if it makes the top ten (topTen array will need to be saved in bin file so it will be able to be compared each time),

must account for if < 10 games (just place in array) have been played and if 10 games have been played (games cap will be 10)

my code is below but only need help with topTen function please reference it. will have to prototype at top of main and call function in main.

void topTen(int score[], int newScore) { //Show the top ten scores

                        int i, count, temp, pass, n = 0, j;

                        for (i = 0; i < GAMES; i++) {

                                    if (score[i] == 0) {

                                                score[i] = newScore;

                                                i = 10;

                                    }

                        }//End loop

                        //Checks the newest score against all the other scores stored in the array.

                        if (newScore < score[GAMES - 1])

                                    score[GAMES - 1] = newScore;

                        // sort scores in array using insertion method

                        for (i = 1; i < n; i++) {

                        }

                        for (i = 0; i <= n - 1; i++) {

                                    j = i;

                        }

                        CLS;

                        printf(" Top Ten Scores ");

                        for (i = 0; i < (GAMES); i++) {

                                    if (score[i] == 0) {

                                                printf(" %02d: Empty Score ", i + 1);

                                    }

                                    else {

                                                printf(" %02d: %d Moves ", i + 1, score[i]);

                                    }

                        }

                        PAUSE;

            }// end topTen

Explanation / Answer

So from what I could understand, you want to create a leaderboard of top 10 scores from a game (Battleship I suppose?) and want to keep adding new scores from each game to the scoreboard such that it’s sorted overtime. And you plan to sort it using insertion sort, and when number of games exceeds 10, it just checks if the new score fits in on the existing scoreboard, and if it does, it updates it else nothing happens.

I have tried to code as general a solution as possible (though keeping certain constraints in mind, such as range of score) and I have taken the following assumptions:

Now, all said and done, here’s the code:

#include <stdio.h>

#include <assert.h>

#define SIZE 10

#define GAMES 10

#define INF 100000

void topTen(int score[], int newScore);

void printScores(int score[]);

int main(int argc, char const *argv[]) {

  int scores[SIZE], i = 0;

  for(i = 0; i < SIZE; i++)

    scores[i] = INF;

  printScores(scores);

  int input;

  for(i = 0; i < GAMES; i++) {

    printf("Enter score:");

    scanf("%d", &input);

    //assert(input >= 17 && input <= 100); // check range [17, 100]

    topTen(scores, input); // update scoreboard

    printScores(scores); // print scoreboard

  }

  return 0;

}

void topTen(int score[], int newScore) {

  int key, i, j;

  for(i = 0; i < SIZE; i++) {

    // if there exists an empty field in the array, just fill it

    if(score[i] == INF) {

      score[i] = newScore;

      break;

    }

  }

  // if the array is full, check if the new score has a place on the scoreboard

  if(i >= SIZE && newScore < score[SIZE - 1])

    score[SIZE - 1] = newScore; //if yes, replace the last value

  // now sort using insertion sort

  for(i = 1; i < SIZE; i++) {

    key = score[i];

    j = i - 1;

    while(j >= 0 && score[j] > key) {

      score[j + 1] = score[j];

      j = j - 1;

    }

    score[j + 1] = key;

  }

}

void printScores(int score[]) {

  printf("Top %d scores ", SIZE);

  for(int i = 0; i < SIZE; i++)  {

    if(score[i] == INF) {

      printf("%d : _ ", i + 1);

    } else {

      printf("%d : %d moves ", i + 1, score[i]);

    }

  }

}