Write a program that records high-score data for a fictitious game. The program
ID: 3670658 • Letter: W
Question
Write a program that records high-score data for a fictitious game. The program will ask the user to enter five names, and five scores. It will store the data in memory, and print it back out sorted by score. The output from your program should look exactly like this: Additional Requirements The data must be stored in two arrays: an array of strings named names, and an array of ints named scores. These arrays must be declared in the main function. All of the user input should be done in a function named initializeArrays(). It should have the following signature: You must also write two more functions: one to sort both arrays, and one to display the final list of names and scores. They should have the following signatures. The main function should be very short. It should just declare the arrays and then invoke these three functions.Explanation / Answer
#include<iostream>
using namespace std;
void initializeArray(string names[], int scores[], int size){
for(int i=0;i<size;i++){
cout<<"Enter the name for score #"<<i+1<<" : ";
cin>>names[i];
cout<<"Enter the score for score #"<<i+1<<" : ";
cin>>scores[i];
}
}
void sortData(string names[], int scores[], int size){
for(int i = size - 1; i > 0; i--){
int currentMin = scores[0];
int currentMinIndex = 0;
for (int k = 1; k <= i; k++){
if (currentMin > scores[k]){
currentMin = scores[k];
currentMinIndex = k;
}
}
string tempname = names[currentMinIndex] ;
names[currentMinIndex] = names[i];
names[i] = tempname;
scores[currentMinIndex] = scores[i];
scores[i] = currentMin;
}
}
void displayData(const string names[],const int scores[], int size){
for(int i=0;i<size;i++)
cout<<names[i]<<": "<<scores[i]<<endl;
}
int main(){
int size;
cout<<"Enter number of students: ";
cin>>size;
int scores[size];
string names[size];
initializeArray(names,scores,size);
cout<<" Entered Values: ";
displayData(names,scores,size);
sortData(names,scores,size);
cout<<" Top Scorers: ";
displayData(names,scores,size);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.