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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
#include <fstream>
#include <iostream>
using namespace std;
#include <cstdlib>
// search for A grades
bool hasA(int* score, int MAX_SCORES)
{
int i;
for (i = 0; i < MAX_SCORES; i++)
{
if (score[i] >= 90)
{
return true;
break;
} // if
} // for
return false;
}
// search for B grades
bool hasB(int* score, int MAX_SCORES)
{
int i;
for (i = 0; i < MAX_SCORES; i++)
{
if (score[i] >= 80 && score[i] < 90)
{
return true;
break;
} // if
} // for
return false;
}
// search for C grades
bool hasC(int* score, int MAX_SCORES)
{
int i;
for (i = 0; i < MAX_SCORES; i++)
{
if (score[i] >= 70 && score[i] < 80)
{
return true;
break;
} // if
} // for
return false;
}
// search for passing grades
bool hasPass(int* score, int MAX_SCORES)
{
int i;
for (i = 0; i < MAX_SCORES; i++)
{
if (score[i] >= 70)
{
return true;
break;
} // if
} // for
return false;
}
int numberGradesGreater(int* score, int n, double x)
{
int nGreater = 0;
for (int i = 0; i < n; i++)
if (score[i] >= x) nGreater++;
return nGreater;
}
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 << "Editor(s) used: XP Notepad" << endl;
cout << "Compiler(s) used: VC++ 2010 Express" << endl;
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 << " 'B', 'C', and 'passing' scores were entered." << endl;
cout << endl;
int aScore = 0;
int SIZE = 0;
const int MAX_SCORES = 6;
int score[MAX_SCORES];
// prompt user for size
cout << "How many scores? ";
cin >> SIZE;
cin.ignore(1000, 10);
// prompt user for scores
int i;
for (i = 0; i < SIZE; i++)
{
cout << "Enter score " << (i + 1) << ": ";
cin >> score[i];
cin.ignore(1000, 10);
if (SIZE < MAX_SCORES)
score[SIZE++] = aScore;
} // for
if (SIZE != 0)
{
// sort scores
qsort(score, MAX_SCORES, sizeof(int), compare);
// display scores
for (i = 0; i < MAX_SCORES; i++)
cout << score[i] << ' ';
cout << endl;
// display min, max, and average
cout << "Minimum: " << score[0] << endl;
cout << "Maximum: " << score[MAX_SCORES-1] << endl;
cout << "Average: " << getAverage(score, MAX_SCORES) << endl;
// display whether or not each letter grade entered
if(hasA(score, MAX_SCORES))
cout << "At least one 'A' grade entered." << endl;
else
cout << "No 'A' grades entered." << endl;
if(hasB(score, MAX_SCORES))
cout << "At least one 'B' grade entered." << endl;
else
cout << "No 'B' grades entered." << endl;
if(hasC(score, MAX_SCORES))
cout << "At least one 'C' grade entered." << endl;
else
cout << "No 'C' grades entered." << endl;
if(hasPass(score, MAX_SCORES))
cout << "At least one 'passing' grade entered." << endl;
else
cout << "No 'passing' grades entered." << endl;
}
else if (SIZE == 0)
cout << "Nothing to display" << endl;
cout << endl;
return 0;
} // main
|