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
|
/* This Program takes the average of a user's test
Sorts the entered scores in ascending order
You can enter as many scores as you like.
*/
#include <iostream>
#include<iomanip>
using namespace std;
void getScore(int&, int&, double *); // stores the testscores
void calcAverage (float, int, int &); // used to find the average test scores
void selectionSort (double *, int); // sorts the test scores in ascending order
void PrintTest(double *, int); // Prints testscores in ascending order
int main ()
{
int Total; // holds the sum of all scores
int numTest; // holds the amount of scores the user will enter
double average; // holds the average of all scores
double *score; // dynamically holds all scores
// Gets the number of test scores
cout << "How many test scores are you entering? ";
cin >> numTest;
// Dynamically allocates an array large enough
// to hold the user-defined number of scored
score = new double[numTest];
cout << fixed << showpoint << setprecision(2);
getScore(numTest, Total, score); //asks user to input their test data
selectionSort(score, numTest); //sorts the testscores
calcAverage(average,numTest,Total); // finds the average
PrintTest(score, numTest); // Prints Test scores in ascending order
return 0;
}
void getScore(int &numTest, int &Total, double *score)
{
Total=0;
int test;
for (test =1; test <= numTest; test++)
{
//Gets the tests scores using the number entered
cout << "Enter test " << test<< " score now: ";
cin >> *score;
// validates score entries
if ( *score < 0 || *score > 100)
{
cout << "\nYou have entered an invalid number. \n\n"
<<"Re-Enter: ";
cin >> *score;
}
//Gets the Total of all the scores
Total+=*score;
}
}
//**************************************************************
// Definition of function selectionSort. *
// This function performs an ascending order selection sort on *
// array. size is the number of elements in the array. *
//**************************************************************
void selectionSort(double *score, int numTest)
{
int startScan, minIndex;
double minValue;
for (startScan = 0; startScan < (numTest - 1); startScan++)
{
minIndex = startScan;
minValue = *(score + startScan);
for(int index = startScan + 1; index < numTest; index++)
{
if (*(score + index) < minValue)
{
minValue = *(score + index);
minIndex = index;
}
}
*(score + minIndex) = *(score + startScan);
*(score + startScan) = minValue;
}
}
//Prints the test scores in ascending order
void PrintTest(double *score, int numTest)
{
cout<<"The test scores in ascending order are. \n\n" <<"Score \n" <<"----- \n\n";
for (int count = 0; count < numTest; count++)
{
cout << *(score + count)<<endl; // displays test scores in ascending order
}
cout << fixed << showpoint << setprecision(2);
}
//Calculates the average test score
void calcAverage (float average,int numTest, int& Total)
{
void getScore(int numTest, int Total, int *score);
void calcAverage (double average);
//finds the average testscore
average = (Total) / (numTest);
cout << "\nThe average score is: "<<average<<". \n";
}
|