1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
#include <iostream>
#include <string>
using namespace std;
void displayScores(string names[], int score[], const int amountOfPlayers);
void sort(string names[], int score[], const int amountOfPlayers);
int main(void)
{
// create variables
const int PLAYERS = 5;
string names[PLAYERS];
int scores[PLAYERS];
for(int i = 0; i < PLAYERS; i++)
{
cout << "Name " << i+1 << ": ";
getline(cin, names[i]);
cout << "Score for " << names[i] << ": ";
cin >> scores[i];
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
// Display
cout << "Before Sorting:\n";
displayScores(names, scores, PLAYERS);
sort(names, scores, PLAYERS);
cout << "\nAfter Sorting:\n";
displayScores(names, scores, PLAYERS);
system("pause");
return 0;
}
void displayScores(string names[], int scores[], const int amountOfPlayers)
{
for (int i = 0; i < amountOfPlayers; i++)
{
cout << names[i] << ": " << scores[i] << endl;
}
}
void sort(string names[], int scores[], const int amountOfPlayers)
{
for (int i = 0; i < amountOfPlayers - 1; i++)
{
for (int j = i + 1; j < amountOfPlayers; j++)
{
if (scores[i] < scores[j])
{
string storeName = names[i];
int storeScore = scores[i];
names[i] = names[j];
scores[i] = scores[j];
names[j] = storeName;
scores[j] = storeScore;
}
}
}
}
|