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
|
#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std;
int buildQuizArray(int);
void printArray( string, int, int);
double calcQuizAverage( int, int);
void sortArray( int, int);
/**********************************************************************
Function: int buildQuizArray( int quizArray[] )
Use: This function builds the array.
Arguments: The to be built array
Returns: The number of quiz scores
**********************************************************************/
int buildQuizArray( int quizArray[] )
{
int numSlots;
int val;
int i = 0;
cout << "Enter a quiz score: ";
cin >> val;
while (val != -1)
{
quizArray[i] = val;
i++;
cout<< "Enter a quiz score";
cin >> val;
}
return i+1;
}
/**********************************************************************
Function: void printArray( string reportTitle, int quizArray[], int numberOfQuizzes )
Use: This function prints out the array
Arguments: The report title, the array, and the number of quizzes
Returns: Void
**********************************************************************/
void printArray( char reportTitle[], int quizArray[], int numberOfQuizzes )
{
int i;
cout<< reportTitle << "\n-----------\n";
for (i=0; i<numberOfQuizzes; i++);
cout<< "Quiz " << i <<": " << setw(2) <<quizArray[i] <<"/10\n";
}
/**********************************************************************
Function: double calcQuizAverage( int quizArray[], int numberOfQuizzes )
Use: This function calculates the quiz average
Arguments: The array and the number of quizzes
Returns: The average score
**********************************************************************/
double calcQuizAverage( int quizArray[], int numberOfQuizzes )
{
int sumLow, sum = 0, i=0;
double avg;
sumLow = quizArray[0] + quizArray[1];
for (i=0; i< numberOfQuizzes; i++)
sum += quizArray[i];
if (numberOfQuizzes <= 2)
avg = sum / (10 * numberOfQuizzes) * 100;
else
(sum - sumLow) / (10 * (numberOfQuizzes - 2)) * 100;
return avg;
}
/**********************************************************************
Function: void sortArray( int quizArray[], int numberOfQuizzes )
Use: This function sorts the array by number
Arguments: The array and the number of quizzes
Returns: The average score
**********************************************************************/
void sortArray( int quizArray[], int numberOfQuizzes )
{
int i, j, min;
int temp;
for (i=0; i<numberOfQuizzes; i++)
min = i;
for(j=i+1; j<numberOfQuizzes; i++)
{
if (quizArray[i] < quizArray[min])
min = j;
}
temp = quizArray[i];
quizArray[i]=quizArray[min];
quizArray[min]=temp;
}
int main ()
{
char str[20] = "Quiz Scores";
int numberOfQuizzes, quizArray[12];
double value;
numberOfQuizzes = buildQuizArray(quizArray[numberOfQuizzes]);
sortArray(quizArray[12], numberOfQuizzes);
value = calcQuizAverage(quizArray[12], numberOfQuizzes);
cout << value;
printArray(str, quizArray[12], numberOfQuizzes);
return 0;
}
|