Make a program that will search through scores of an array and display the score
ID: 3858196 • 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. The program should look like the following:Explanation / Answer
#include<iostream>
using namespace std;
#define SIZE 10
void sortScores(int (&a)[SIZE], int size){
int i, j, temp;
for (i = 0; i < size; ++i)
{
for (j = i+1; j < size; ++j)
{
if (a[i] < a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
}
void displayScores(int (&a)[SIZE], int size){
int i, j, temp;
for (i = 0; i < size; ++i)
{
cout << a[i] << endl;
}
}
int main(){
int scores[SIZE];
cout << "Enter 5 scores" << endl;
for (int i = 0; i<5; i++){
cin >> scores[i];
}
sortScores(scores,5);
displayScores(scores,5);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.