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
|
#include<iostream>
#include<string>
using namespace std;
int main()
{
static const int SIZE = 10;
static int testScores[SIZE] = { 80,90,85,65,55,70,75,50,80,95 };
int count;
int highest, lowest;
//static int average; // <-- delete not used
// Function prototypes
void SortTest(int[], int);
double FindMean(int[], int); // <-- double see below
// The code below will display the unsorted list of test scores.
cout << "The unsorted scores for each test are: " << endl;
cout << testScores[0];
cout << " " << testScores[1];
cout << " " << testScores[2];
cout << " " << testScores[3];
cout << " " << testScores[4];
cout << " " << testScores[5];
cout << " " << testScores[6];
cout << " " << testScores[7];
cout << " " << testScores[8];
cout << " " << testScores[9] << endl;
// Now we will call a function to sort our array in ascending order.
SortTest(testScores, SIZE);
// Now we will find the highest and lowest numbers.
highest = testScores[0];
for (count = 1; count < SIZE; count++)
{
if (testScores[count] > highest)
highest = testScores[count];
}
cout << "\n"; // Used solely for spacing.
cout << "\nThe highest test score is: " << highest << endl;
// When the above code is done processing this line will
// display the highest number.
lowest = testScores[0];
for (count = 1; count < SIZE; count++)
{
if (testScores[count] < lowest)
lowest = testScores[count];
}
cout << "\nThe lowest test score is: " << lowest << endl;
// When the above code is done processing this line will
// display the lowest number.
// Now we will call a function to find the mean average of all
// the test scores.
cout << "Average = " << FindMean(testScores, SIZE) << endl; // <-- one way
double average = FindMean(testScores, SIZE); // <-- another way
cout << "Alternative average = " << average << endl;
return 0;
}
// This function will use a technique called a bubble sort to sort our array.
void SortTest(int testScores[], int SIZE)
{
int size = 10;
int i, j, temp;
for (i = 0; i < size; i++);
for (i = 1; i < size; i++)
{
for (j = 0; j < (size - i); j++)
if (testScores[j] > testScores[j + 1])
{
temp = testScores[j];
testScores[j] = testScores[j + 1];
testScores[j + 1] = temp;
}
}
cout << "\nThe scores for each exam after sorting in ascending order are:"
<< endl;
for (i = 0; i < size; i++)
cout << " " << testScores[i];
return;
}
// This function will calculate the mean average of all test scores for us.
double FindMean(int testScores[], int SIZE) // <--- should be double
//if you return a double
{
double average = 0.0; // <-- should initialize variables
double sum = 0.0;
for (int x = 0; x < SIZE; x++)
{
sum += testScores[x];
// average = sum / SIZE; // <-- delete this line
}
return average; // <-- no brackets
}
|