197k views
1 vote
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 approximately like this: enter the name for score #1: suzy enter the score for score #1: 600 enter the name for score #2: kim enter the score for score #2: 9900 enter the name for score #3: bob enter the score for score #3: 1012 enter the name for score #4: armando enter the score for score #4: 8000 enter the name for score #5: tim enter the score for score #5: 514

1 Answer

0 votes

Scores.cpp

#include <iostream>

#include <string>

using namespace std;

void initializeArrays(string names[], int scores[], int size);

void sortData(string names[], int scores[], int size);

void displayData(const string names[], const int scores[], int size);

int main()

{

string names[5];

int scores[5];

//reading names and scores

initializeArrays(names, scores, 5);

//sorting the lists based on score.

sortData(names, scores, 5);

//displaying the contents of both arrays

displayData(names, scores, 5);

return 0;

}

void initializeArrays(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){

int temp = 0;

string tempStr = "";

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

for(int j=1; j < (size-i); j++){

//checking max score and sorting based on it.

if(scores[j-1]< scores[j]){

//swap the elements!

//swapping scores

temp = scores[j-1];

scores[j-1] = scores[j];

scores[j]=temp;

//swapping names

tempStr = names[j-1];

names[j-1] = names[j];

names[j]=tempStr;

}

}

}

}

void displayData(const string names[], const int scores[], int size){

cout<<"Top Scorers:"<<endl;

//printing the elements up to the size of both arrays.

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

cout<<names[i]<<": "<<scores[i]<<endl;

}

}

User Hradecek
by
5.2k points