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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
/*
This program will take the names
and times of three runners and display
them by the fastest time.
Last Modified 09.19.2013
*/
#include <iostream>
#include <string>
using namespace std;
//This is the main module.
int main()
{
//Declare the variables.
const int SIZE = 3;
string names[SIZE];
int minutes[SIZE],
seconds[SIZE],
milliseconds[SIZE],
flag = 1;
unsigned long order[SIZE];
//Display a welcome message.
cout << " \n";
cout << "This program will take the name of three runners and their times and\n";
cout << "display the results by the fastest times in first, second, and third\n";
cout << "place.\n";
cout << " \n";
cout << "Last Modified 09.19.2013\n";
//Ask the user for the three runner's names and track times.
//Create a counter variable.
int index = 0;
//Get the names and track times.
for (index = 0; index <= SIZE-1; index++)
{
cout<< " \n";
cout << "Enter the name of runner # " << index+1 << ".\n";
cout << " \n";
getline(cin, names[index]);
//Set a message about input to display only once.
while (flag)
{ cout << " \n";
cout << "The next three inputs will be the MINUTES, SECONDS, and\n";
cout << "MILLISECONDS of the total track time.\n";
cout << " \n";
flag = 0;
}
//Get the MINUTES, SECONDS, and MILLISECONDS of the track time.
do
{ cout << " \n";
cout << "Enter runner number " << index + 1 << "'s track time MINUTES.\n";
cout << "Only numbers are valid.\n";
cin >> minutes[index];
}while (minutes[index] < 0);
order[index] = order[index] +(600 * minutes[index]);
do
{ cout << " \n";
cout << "Enter runner number " << index + 1 << "'s track time SECONDS.\n";
cout << "Only numbers are valid.\n";
cin >> seconds[index];
}while (seconds[index] < 0 || seconds[index] >= 60);
order[index] = order[index] + (seconds[index] * 10);
do
{ cout << " \n";
cout << "Enter runner number " << index + 1 << "'s track time MILLISECONDS.\n";
cout << "Only numbers are valid.\n";
cin >> milliseconds[index];
cin.ignore();
}while (milliseconds[index] < 0 || milliseconds[index] >= 100);
order[index] = order[index] + milliseconds[index];
}
//Sort the first, second, and third place runners by fastest time.
//Declare variable to hold last element of array.
for (int maxElement = 1; maxElement < SIZE; maxElement++)
{
for (index = 0; index < SIZE - 1; index++)
{
if (order[index] > order[index+1])
{
//Swap current and next elements in arrays.
//Declare temp variables.
int minTemp = 0,
secTemp = 0,
millTemp = 0,
orderTemp = 0;
string runTemp = "";
//switch variables with their next element in the array.
minTemp = minutes[index];
minutes[index] = minutes[index+1];
minutes[index+1] = minTemp;
secTemp = seconds[index];
seconds[index] = seconds[index+1];
seconds[index+1] = secTemp;
millTemp = milliseconds[index];
milliseconds[index] = milliseconds[index+1];
milliseconds[index+1] = millTemp;
runTemp = names[index];
names[index] = names[index+1];
names[index+1]= runTemp;
orderTemp = order[index];
order[index] = order[index+1];
order[index+1] = orderTemp;
}
}
}
//Display the results.
for (index = 0; index <= SIZE - 1; index++)
{ cout << " \n";
cout << index + 1 << " Place : " << names[index] << endl;
cout << "TIME : " << minutes[index] << ":" << seconds[index] << "." << milliseconds[index] << endl;
}
return 0;
}
|