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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
#include <iostream>
#include <cstring>
using namespace std;
struct winRecord
{
int wins;
char* name;
};
void initializeArrays(winRecord* standings, int SIZE);
void sortData(winRecord* standings, int SIZE);
void displayData(winRecord* standings, int SIZE);
int largestI(winRecord* standings, int startingI, int SIZE);
char* getLine()
{
const int BUFFER_SIZE = 1000;
char buffer[BUFFER_SIZE];
cin.getline(buffer, BUFFER_SIZE);
int length = strlen(buffer);
char *rValue = new char[length + 1];
strncpy(rValue, buffer, length);
return rValue;
}
int main()
{
int SIZE;
char* name;
cout << "How many teams will you enter?" << endl;
cin >> SIZE;
winRecord *standings = new winRecord[SIZE];
initializeArrays(standings, SIZE);
sortData(standings, SIZE);
displayData(standings, SIZE);
delete [] name;
standings = NULL;
}
void initializeArrays(winRecord* standings, int SIZE) //This function asks the user to enter in the data that needs to be stored.
{
for(int i = 0; i < SIZE; i++)
{
cout << "Enter team #" << (i + 1) << ": ";
cin >> standings[i].name;
standings[i].name = getLine();
cout << "Enter the wins for team #" << (i + 1) << ": ";
cin >> standings[i].wins;
}
cout << endl;
}
void sortData(winRecord* standings, int SIZE)
{
int bigI;
for(int count = 0; count < SIZE - 1; count++)
{
bigI = largestI(standings, count, SIZE);
swap(standings[bigI].name, standings[count].name);
swap(standings[bigI].wins, standings[count].wins);
}
}
int largestI(winRecord* standings, int startingI, int SIZE)
{
int wantedI = startingI;
for(int count = startingI + 1; count < SIZE; count++)
{
if(standings[count].wins > standings[wantedI].wins)
{
wantedI = count;
}
}
return wantedI;
}
void displayData(winRecord* standings, int SIZE) //This function displays the sorted list of names and wins.
{
cout << "League Standings: " << endl;
for(int i = 0; i < SIZE; i++)
{
cout << standings[i].name << ": " << standings[i].wins << endl;
}
}
|