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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
|
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
void getNumberOfScores( int& );
int generateOneScore();
void generateScores( int[], int );
void displayScores(int[], int );
int findLowest(int[], int );
int findHighest(int[], int );
double calcAverage (int[], int, bool);
int countAboveAverage (int[], int, double);
char findLetterGrade(double);
void displayDuplicateScores( int[], int );
void displayHistogram( int[], int );
const int MIN_A = 90;
const int MIN_B = 80;
const int MIN_C = 70;
const int MIN_D = 60;
int main()
{
cout
<< "Welcome to the Grade Processor Application\n"
<< "This app will generate the number of scores desired by the user\n"
<< "and then perform and display several functions on this data.\n\n";
int no_of_scores = 0;
getNumberOfScores(no_of_scores);
int* scores = new int[no_of_scores];
srand(time_t(0));
generateScores(scores, no_of_scores);
displayScores(scores, no_of_scores);
double average = calcAverage(scores, no_of_scores, false);
cout
<< " Average of all scores: " << average << '\n'
<< " Number of scores above this average: "
<< countAboveAverage(scores, no_of_scores, average) << '\n'
<< " Letter grade for all scores: "
<< findLetterGrade(average) << '\n'
<< '\n';
average = calcAverage(scores, no_of_scores, true);
cout
<< "Average of all scores without lowest: " << average << '\n'
<< " Number of scores above this average: "
<< countAboveAverage(scores, no_of_scores, average) << '\n'
<< " Letter grade after dropping lowest: "
<< findLetterGrade(average) << '\n'
<< '\n';
cout
<< " Minimum of all scores: "
<< findLowest(scores, no_of_scores) << '\n'
<< " Maximum of all scores: "
<< findHighest(scores, no_of_scores) << '\n'
<< '\n';
cout
<< "Displaying histogram for " << no_of_scores << " scores:\n";
displayHistogram(scores, no_of_scores);
cout << "Score occurrence counts > 1:\n";
displayDuplicateScores(scores, no_of_scores);
cout << '\n';
cout << "Thanks for using the Grade Processor application.\n";
delete[] scores;
return 0;
}
void getNumberOfScores( int& number)
{
cout << "How many scores do you want to process: ";
cin >> number;
}
int generateOneScore()
{
return rand() % 65 + 35; // <--
}
void generateScores( int list[], int n )
{
for(int i = 0; i < n; ++i)
{
list[i] = generateOneScore();
}
return;
}
void displayScores(int list[], int n)
{
cout << "All scores:\n";
int col_count = 0;
for(int i = 0; i < n; i++)
{
cout << setw(6) << list[i];
++col_count;
if(col_count % 10 == 0)
cout << '\n';
}
cout << '\n';
return;
}
int findLowest(int list[], int n)
{
int smallest = list[0]; // <---
for (int i = 1; i < n; i++) // <--
{
if (list[i] < smallest)
smallest = list[i];
}
return smallest;
}
int findHighest(int list[], int n)
{
int largest = list[0];
for (int i = 1; i < n; i++)
{
if (list[i] > largest)
largest = list[i];
}
return largest;
}
double calcAverage (int list[], int n, bool drop)
{
double total = 0;
for(int i = 0; i < n; ++i)
total += list[i];
if (drop)
{
total -= findLowest(list,n);
n--;
}
return 1.0*total/n;
}
int countAboveAverage (int list[], int n, double average)
{
int count = 0;
for(int i = 0; i < n; ++i)
{
if(list[i] > average)
count++;
}
return count;
}
char findLetterGrade(double grade)
{
char letter_grade;
if(grade >= MIN_A)
letter_grade = 'A';
else if(grade >= MIN_B)
letter_grade = 'B';
else if(grade >= MIN_C)
letter_grade = 'C';
else if(grade >= MIN_D)
letter_grade = 'D';
else
letter_grade = 'F';
return letter_grade;
}
void displayDuplicateScores( int list[], int n )
{
int duplicates[101]{0};
for( int i = 0; i < n; i++)
++duplicates[ list[i] ];
int col_count = 0;
for (int i = 100; i >= 0; --i)
{
if (duplicates[i] > 1)
{
col_count++;
cout << setw(6) << i << setw(2) << duplicates[i];
if(col_count % 10 == 0)
cout << '\n';
}
}
cout << '\n';
}
void displayHistogram( int list[], int n )
{
string letter = {"ABCDEF"};
int group[5] = {0};
char grade;
for(int i = 0; i < n; ++i)
{
grade = findLetterGrade(list[i]);
switch(grade)
{
case 'A':
++group[0];
break;
case 'B':
++group[1];
break;
case 'C':
++group[2];
break;
case 'D':
++group[3];
break;
case 'F':
++group[4];
break;
default:
cout << "Error in histogram processing\n";
break;
}
}
string histogram = "";
for (int i = 0; i < 5; ++i)
{
histogram.assign(group[i], '*');
cout << letter[i] << ": " << histogram << '\n';
}
cout << '\n';
}
|