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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
|
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
void initialize(/*inout*/int[], /*in*/int); // List of function prototypes.
void getTestScores(/*inout*/int[], /*in*/int);
void fileInput(/*inout*/int[], /*in*/int);
int showScores(/*in*/const int[], /*in*/int);
int highest(/*in*/const int[], /*in*/int);
int getLowest(/*in*/const int[], /*in*/int);
double average(/*in*/const int[], /*in*/int);
void getScore(/*in*/const int[], /*in*/int);
void selectionSortAs(int[], int);
void selectionSortDe(int[], int);
void binarySearch(const int[], int);
int main()
{
const int SIZE = 10; // Array size
int testScores[SIZE]; // Array of test scores
int choice = 0; // Needed for menu interface
initialize(testScores, SIZE); // Function call to initialize the array testScores()
// each of ten elements to zero.
do // Display menu as long as user doesn't type 11 to quit.
{
cout << endl;
cout << " 1. Read in 10 scores (integers) from the user." << endl;
cout << " 2. Read in 10 scores from a file, scores.txt." << endl;
cout << " 3. Print the 10 scores." << endl;
cout << " 4. Print the highest score." << endl;
cout << " 5. Print the lowest score." << endl;
cout << " 6. Print the mean (average) of the 10 scores." << endl;
cout << " 7. Print a score based on an entry or row# and show how many scores are higher." << endl;
cout << " 8. Sort scores (acsending sort)." << endl;
cout << " 9. Sort scores (decsending sort)." << endl;
cout << "10. Search for a score." << endl;
cout << "11. Exit the program." << endl << endl;
// Prompt user for an option
cout << "Enter an option such as 1 or 2 etc: "; // Get user's choice.
cin >> choice;
switch(choice) // Access the correct function to handle
{ // user's choice.
case 1: getTestScores(testScores, SIZE);
break;
case 2: fileInput(testScores, SIZE);
break;
case 3: showScores(testScores, SIZE);
break;
case 4: highest(testScores, SIZE);
cout << "The highest number is " << highest(testScores, SIZE) << endl;
break;
case 5: cout << "The lowest score is " << getLowest(testScores, SIZE) << endl;
break;
case 6: cout << "The average score is " << average(testScores, SIZE) << endl;
break;
case 7: getScore(testScores, SIZE); // Show test score from the array given a number.
break;
case 8: selectionSortAs(testScores, SIZE);
break;
case 9: selectionSortDe(testScores, SIZE);
break;
case 10: binarySearch(testScores, SIZE);
break;
case 11: cout << "Come back to access your data later.";
break;
default: cout << "INVALID INPUT." << endl;
}
} while (choice != 11);
return 0;
}
//***********************************************************************************
// Function will initialize all the elements of an aray to zero.
// Pre: Array testScores[] sent in from main with a size of ten.
// Post: Each element is initialized to zero and zent back to main.
void initialize(/*inout*/int scores[], /*in*/int size)
{
for(int count = 0; count <= size; count++) // Initialize array element to zero.
scores[count] = 0;
}
//***********************************************************************************
//*********************************************************************************************************
// The getTestScores function accepts an array and its size as arguments. It prompts the user to enter test
// scores, as arguments. It prompts the user to enter test scores, which are stored in the array.
// Pre: An integer array has been declared in the calling function with a known size.
// Post: Array's elements are filled with user input and sent back to main
void getTestScores(/*inout*/int scores[], /*in*/int size)
{
for(int count = 0; count <= size - 1; count++)
{
cout << "Enter test score number " << (count + 1) << ": "; // Get each test score.
cin >> scores[count];
}
}
//*********************************************************************************************************
//*****************************************************************************************
// Function will allow user to input a file consisting of numbers to be used in the program
// Pre: Array testScores[] is sent in from main with a size of ten elements.
// Post: Array elements are filled with numbers from file and sent back to main.
void fileInput(/*inout*/int testScores[], /*in*/int size)
{
string fileName;
ifstream inData;
cout << "Enter a filename: ";
cin >> fileName;
inData.open(fileName.c_str());
for (int count = 0; count < size; count++)
inData >> testScores[count];
}
//******************************************************************************************
//******************************************************************************************************
// Function Will display the values stored in each element of the array with a consequtive numbered list.
// Pre: An integer array has been declared in the calling function with a known size.
// Post: Algorithm will display contents, one per line.
int showScores(/*in*/const int scores[], /*in*/int size)
{
for(int count = 0; count <= size; count++)
{
if((count + 1) == size) // Prints one less whitespace for row #10. Keeps numbers aligned.
{
cout << "score #" << count + 1 << ": " << scores[count] << endl;
return 0;
}
cout << "score #" << count + 1 << ": " << scores[count] << endl; // Prints rows one through nine.
}
}
//****************************************************
// The getLowest function accepts a double array and *
// its size as arguments. The lowest value in the *
// array is returned as a double. *
//****************************************************
int getLowest(/*in*/int const scores[], /*in*/int size)
{
int lowest; // To hold the lowest value
// Get the scores first array element.
lowest = scores[0];
// Step through the rest of the array. When a
// value less than lowest is found, assign it
// to lowest.
for (int count = 1; count < size; count++)
{
if (scores[count] < lowest)
lowest = scores[count];
}
return lowest;
}
double average(/*in*/const int testScores[], /*in*/int size)
{
int sum = 0;
double average = 0;
for (int count = 0; count < size; count++)
{
sum = sum + testScores[count];
}
average = sum / 10.00;
return average;
}
void getScore(/*in*/const int scores[], /*in*/int size)
{
int row = 0, score = 0, higher = 0;
cout << "Please enter entry or row # of score you want: ";
cin >> row;
while(row < 0 || row > size - 1)
{
cout << "Invalid row, Please enter entry or row # of score you want: ";
cin >> row;
}
score = scores[row - 1];
cout << "Entry # " << row << " score: " << score << endl;
higher = score;
for (int count = 0; count < size; count++)
{
if(scores[count] > higher)
higher = scores[count];
}
cout << "Score statistics: " << higher << " higher" << endl;
}
|