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 252 253
|
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
// FUNCTION PROTOTYPES GO HERE:
int num_students (string prompt);
void score_input (int *scores1, int scores2[], int scores3[], const int students);
void sum_score (const int scores1[], const int scores2[], const int scores3[], int totals[], const int students);
double average_score (int totals[], const int students);
int find_min (const int totals[], const int students);
int find_max (const int totals[], const int students);
int above_avg (const int totals[], const int students, double & average);
void display_table (const int scores1[], const int scores2[],const int scores3[], const int totals[], const int students, double & average, int min, int max, int above);
void display_top ();
void display_scores(int i, const int scores1[], const int scores2[], const int scores3[], const int totals[], double & average);
void display_bottom (const int students, double average, int min, int max, int above);
int main()
{
int * scores1;
int * scores2;
int * scores3;
int * totals;
int students(0);
double avg(0.0);
int min, max, above;
// Prompt for the number of students
students = num_students("Enter number of students: ");
cout << endl;
// Allocate arrays to hold exam scores and totals
scores1 = new int[students];
scores2 = new int[students];
scores3 = new int[students];
totals = new int[students];
// Prompt and read exams for each student
score_input (scores1, scores2, scores3, students);
// Compute exam totals for each student
sum_score (scores1, scores2, scores3, totals, students);
// Compute the average of the total scores
avg = average_score (totals, students);
// Find the minimum total score
min = find_min (totals, students);
// Find the maximum total score
max = find_max (totals, students);
// Compute the number of students with total scores at or average of the total scores
above = above_avg (totals, students, avg);
// Display table
cout << endl;
display_table(scores1, scores2, scores3, totals, students, avg, min, max, above);
// De-allocate arrays
delete [] scores1;
delete [] scores2;
delete [] scores3;
delete [] totals;
return 0;
}
// FUNCTION DEFINITIONS GO HERE:
int num_students(string prompt)
{
int x(0);
cout << prompt;
cin >> x;
while (x <= 0)
{
cout << "Sorry, you must enter a positive value." << endl;
cout << prompt;
cin >> x;
}
int students = x;
return(students);
}
void score_input (int *scores1, int scores2[], int scores3[], const int students)
{
for (int i(0); i < students; i++)
{
cout << "Enter the three exam scores for student #" << i+1 << ": ";
cin >> scores1[i] >> scores2[i] >> scores3[i];
}
}
void sum_score (const int scores1[], const int scores2[], const int scores3[], int totals[], const int students)
{
for (int i(0); i < students; i++)
{
totals[i] = scores1[i] + scores2[i] + scores3[i];
}
}
double average_score (int totals[], const int students)
{
double average(0.0);
int total(0);
for (int i(0); i < students; i++)
{
total = total + totals[i];
}
average = double(total)/students;
return(average);
}
int find_min (const int totals[], const int students)
{
int min(0);
min = totals[0];
for (int i(1); i < students; i++)
{
if (totals[i] < min)
{
min = totals[i];
}
}
return(min);
}
int find_max (const int totals[], const int students)
{
int max = totals[0];
for (int i(1); i < students; i++)
{
if (totals [i] > max)
{
max = totals[i];
}
}
return(max);
}
int above_avg (const int totals[], const int students, double & average)
{
int above(0);
for (int i(0); i < students; i++)
{
if ( totals [i] >= average)
{
above++;
}
}
return(above);
}
void display_table (const int scores1[], const int scores2[], const int scores3[], const int totals[], const int students, double & average, int min, int max, int above)
{
display_top();
for (int i(0); i < students; i++)
{
display_scores(i, scores1, scores2, scores3, totals, average);
}
display_bottom(students, average, min, max, above);
}
void display_top()
{
cout << "-----------------------------------------------" << endl;
cout << "Student Score 1 Score 2 Score 3 Total " << endl;
cout << "-----------------------------------------------" << endl;
}
void display_scores(int i, const int scores1[], const int scores2[], const int scores3[], const int totals[], double & average)
{
cout << right << setw(7) << i+1 << " " << left << fixed << setw(10) << scores1[i] << setw(10) << scores2[i] << setw(10) << scores3[i] << setw(6) << totals[i] << setw(1);
if ( totals[i] > average )
{
cout << "+" << endl;
}
else if (totals [i] < average)
{
cout << "-" << endl;
}
else
{
cout << "=" << endl;
}
}
void display_bottom (const int students, double average, int min, int max, int above)
{
cout << "-----------------------------------------------" << endl;
cout << setw(40) << "The number of students is: " << students << endl;
cout << setw(40) << "The avg total score (rounded) is: " << int(round(average)) << endl;
cout << setw(40) << "The maximum total score is: " << max << endl;
cout << setw(40) << "The minimum total score is: " << min << endl;
cout << setw(40) << "Total scores at or above the avg is: " << above << endl;
cout << "-----------------------------------------------" << endl;
}
|