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

Write a program that records high-score data for a fictitious game. The program

ID: 3671116 • 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: 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 mrtiahzeArrays() 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 I of names and scores They should have the following signatures. The mam function should be very short It should |ust declare the arrays and then invoke these three functions.

Explanation / Answer

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

void initializeArray(string names[], int scores[], int n){
   for(int i=0;i<n;i++){
       cout<<"Enter name ans scores for "<<(i+1)<<" record"<<endl;
       cin>>names[i];
       cin>>scores[i];
       }
   }
void sortData(string names[], int scores[], int n){
   //pos_min is short for position of min
   int pos_min,temp;
   string tmp;
   for (int i=0; i < n-1; i++)
   {
   pos_min = i;//set pos_min to the current index of array
       for (int j=i+1; j < n; j++)
       {
           if (scores[j] < scores[pos_min])
pos_min=j;
   //pos_min will keep track of the index that min is in, this is needed when a swap happens
       }  
   //if pos_min no longer equals i than a smaller value must have been found, so a swap must occur
if (pos_min != i)
{   // swaping scores
temp = scores[i];
scores[i] = scores[pos_min];
scores[pos_min] = temp;
//swaping names
tmp = names[i];
names[i] = names[pos_min];
names[pos_min] = temp;
}
   }
}

void display(const string names[], const int scores[], int n){
   cout<<"Name"<<"                   "<<"Score"<<endl;
   for(int i=0;i<n;i++){
       cout<<names[i]<<"                "<<scores[i]<<endl;
       }
   }

int main (){

   int n;
   cout<<"Enter number of records: ";
   cin>>n;
   string names[n];
   int scores[n];
  
   initializeArray(names, scores, n);
   sortData(names, scores, n);
   display(names, scores, n);
  
return 0;
}

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