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

Make a program that will search through scores of an array and display the score

ID: 3858893 • Letter: M

Question

Make a program that will search through scores of an array and display the scores in descending order to simulate a high score leaderboard. The program should contain the following: main() - In main, declare an array called scores[]. Ask the user for 5 scores. Store the 5 scores in scores[]. Call a function called sortScores() and pass the array by reference. Call a function called displayScores(). Pass the array by value into that function. sortScores() - This function will accept an array passed by reference and the size of the array. The function will use the selection sort algorithm to sort through array. You will need to modify the algorithm to sort the scores from greatest to least. This function returns no data. displayScores() - This function accepts an array passed and will cout the contents of the array.

Explanation / Answer

Code :

#include <iostream>

#include <stdio.h>
using namespace std;


// Fuction for sorting the scores
void sortScores(int sc[], int n)
{
int i, j, max,temp;

  
for (i = 0; i < n-1; i++)
{
// loop for finding the maximmum array element
max = i;
for (j = i+1; j < n; j++)
if (sc[j] > sc[max])
max= j;

// Swaping the numbers
temp = sc[max];
sc[max] = sc[i];
sc[i] = temp;
}
}

/* displayScores function to display sorted array */
void displayScores(int arr[], int size)
{
int i;

printf("THE TOP SCORES: ");
for (i=0; i < size; i++)
printf("%d : %d ", i+1, arr[i]);
printf(" ");
}

int main() {
int scores[5] ;
do
{
cout << "Enter 5 scores: "<< endl;
// Takeing array elements from the user input
for (int i = 0; i < 5; ++i)
{
cin >> scores[i];
}
  
sortScores(scores, 5);
  
displayScores(scores, 5);
cout << ' ' << "Press any key to continue...";
} while (cin.get() != ' ');
}

Output :

Enter 5 scores:

12
32
100
36
89
THE TOP SCORES:
1 : 100
2 : 89
3 : 36
4 : 32
5 : 12

Press any key to continue...

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote