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;
}
}