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
|
#include <fstream>
#include <iostream>
using namespace std;
#include <cstdlib>
// search for A grades
bool hasA(int* score, int SIZE, int i)
{
for (i = 0; i < SIZE; i++)
{
if (score[i] >= 90)
{
return true;
cout << "At least one 'A' grade entered.";
break;
} // if
} // for
return false;
}
int compare(const void* pa, const void* pb)
{
const int& a = *static_cast<const int*>(pa);
const int& b = *static_cast<const int*>(pb);
if (a < b) return -1; // negative if a<b
if (a > b) return 1; // positive if a>b
return 0; // 0 for tie
} // compare
double getAverage(int* score, int n)
{
int sum = 0;
double average = 0.0;
for (int i = 0; i < n; i++)
sum += score[i];
average = double(sum) / n;
return average;
}
int main()
{
cout << "Description: This program prompts the user to enter a variable" << endl;
cout << " number of scores, displays the input, calculates average, displays" << endl;
cout << " the minimum and maximum values, and notifies the user if any 'A'" << endl;
cout << " scores were entered." << endl;
cout << endl;
int SIZE;
// prompt user for size
cout << "How many scores? ";
cin >> SIZE;
cin.ignore(1000, 10);
int* score = new int[SIZE];
// prompt user for scores
int i;
for (i = 0; i < SIZE; i++)
{
cout << "Enter score " << (i + 1) << ": ";
cin >> score[i];
cin.ignore(1000, 10);
} // for
// sort scores
qsort(score, SIZE, sizeof(int), compare);
// display scores
for (i = 0; i < SIZE; i++)
cout << score[i] << ' ';
cout << endl;
// display min, max, and average
cout << "Minimum: " << score[0] << endl;
cout << "Maximum: " << score[SIZE-1] << endl;
cout << "Average: " << getAverage(score, SIZE) << endl;
cout << hasA(int* score, int SIZE, int i) << endl;
cout << endl;
delete [] score;
return 0;
} // main
|