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
|
#include <iostream>
#include <iomanip>
using namespace std;
// Function prototypes
void mainMenu();
float calcAverage(float, float, int);
void sortArray(float *, int);
void showArray(float *, int);
int main()
{
int numScores = 0;
float *testScores = new float[numScores]; //allocate storage space for the array
float average = 0.0,
total = 0.0;
mainMenu();
cout << "Please enter how many tests you would like to score: ";
cin >> numScores;
while (numScores < 0)
{
cout << "\n\nError! Please enter a positive number: ";
cin >> numScores;
}
cout << "\n\nPlease enter the scores for each test: \n";
for (int i = 0; i < numScores; i++)
{
cout << "\nTest Score: ";
cin >> testScores[i];
while (testScores[i] < 0.0 || testScores[i] > 100.0)
{
cout << "\nYou cannot enter a score higher than 100 or lower than 0!";
cin.clear();
cin.ignore();
cin >> testScores[i];
}
total += testScores[i];
} //end for loop
// Get the average
calcAverage(total, average, numScores);
system("cls");
cout << "The average of all the scores is: " << average
<< "\n\nThe sorted test scores are as follows: \n\n";
// Sort the values
sortArray(testScores, numScores);
// Display the scores
cout << "The sorted values are:\n";
showArray(testScores, numScores);
delete [] testScores; //de-allocate storage space for the array
cout << "\n\n\n\t\tThanks for using this Test Scores program!";
system("pause");
return 0;
}
//scratch paper:
//how many test scores are you entering?
//cin >> test scores to array
//assign all negatives a value of 0
//sort them in ascending order
//calculate the average score
//display the sorted list and the average
//use pointer notation, not array notation
//////////////////////////////////////////////////////////////////////////////////////////////
/*********************************************************
* Main Menu *
* this is the main menu function. as a void function, *
* it does not return any value *
*********************************************************/
void mainMenu()
{
system("cls");
cout << "\n\n..................................................\n"
<< "Welcome to the Test Scores #1 Program!\n"
<< "..................................................\n\n";
}
/*************************************************************
* calcAverage *
* This function displays calculates the average of all the *
* scores inputted by the user *
*************************************************************/
float calcAverage(float total, float average, int numScores )
{
average = total / numScores;
return average;
}
/************************************************************
* sortArray *
* This function performs an ascending-order bubble sort on *
* array. The parameter size holds the number of elements *
* in the array. *
************************************************************/
void sortArray(float *testScores[], int numScores)
{
float *temp;
bool swap;
do
{ swap = false;
for (int count = 0; count < (numScores - 1); count++)
{
if (*testScores[count] > *testScores[count + 1])
{
temp = testScores[count];
testScores[count] = testScores[count + 1];
testScores[count + 1] = temp;
swap = true;
}
}
} while (swap);
}
/*************************************************************
* showArray *
* This function displays the contents of array. The *
* parameter size holds the number of elements in the array. *
*************************************************************/
void showArray(float *testScores[], int numScores)
{
for (int count = 0; count < numScores; count++)
{
cout << testScores[count] << " ";
cout << endl;
}
}
|