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
|
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
// FUNCTION PROTOTYPES GO HERE:
void read_student(int & students);
void read_exams(int students,int * scores1, int * scores2, int * scores3);
void compute_totals(int students,int*scores1,int * scores2, int* scores3,int* totals);
double compute_average(int students,int *totals);
int find_min(int students, int * totals);
int find_max(int students, int * totals);
int above_average(int * totals,int students,double avg);
void display_table(int *scores1, int *scores2, int *scores3,int *totals,int students, double avg,int max,int min,int above);
void print_top();
void call_student(int students,int *scores1,int *scores2,int *scores3,int *totals,double avg);
void print_bottom(int students,double avg,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
read_student(students);
// 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
read_exams(students,scores1,scores2,scores3);
// Compute exam totals for each student
compute_totals(students,scores1,scores2,scores3,totals);
// Compute the average of the total scores
avg = compute_average(students,totals);
// Find the minimum total score
min =find_min(students,totals);
// Find the maximum total score
max = find_max(students,totals);
// Compute the number of students with total scores at or average of the total scores
above = above_average(totals,students,avg);
// Display table
display_table(scores1,scores2,scores3,totals,students,avg,max,min,above);
// De-allocate arrays
delete [] scores1;
delete [] scores2;
delete [] scores3;
delete [] totals;
return 0;
}
// FUNCTION DEFINITIONS GO HERE:
void read_student(int & students )
{
cout<<"Enter number of student: "; //ask user input
while (cin>>students && students<=0) //check if user input is >0
{
cout<<"Sorry, you must enter positive value."<<endl;
cout<<"Enter number of student: ";
}
cout<<endl;
}
void read_exams(int students,int * scores1, int * scores2, int * scores3)
{
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 compute_totals(int students,int*scores1,int * scores2, int* scores3,int* totals)
{
for(int i =0;i<students; i++)
{
totals[i] = scores1[i] + scores2 [i] + scores3 [i];
}
}
double compute_average(int students,int *totals)
{
double average(0.0);
int total(0);
for (int i=0; i<students; i++)
{
total +=totals[i];
}
average = double(total)/students;
return(average);
}
int find_min(int students, int * totals)
{
int min= totals[0];
for (int i=1;i<students;i++)
{
if(totals[i] < totals[0])
{
min=totals[i];
}
}
return (min);
}
int find_max(int students, int * totals)
{
int max= totals[0];
for (int i=1;i<students;i++)
{
if(totals[i] > totals[0])
{
max=totals[i];
}
}
return (max);
}
int above_average(int * totals,int students,double avg)
{
int up(0);
for (int i=0;i<students;i++)
{
if(totals[i]>avg)
{
up++;
}
}
return (up);
}
void display_table(int *scores1, int *scores2, int *scores3,int *totals,int students, double avg,int max,int min,int above)
{
//call function 1
print_top;
//call function 2
for(int i=0;i<=students;i++)
{
call_student(students, scores1,scores2,scores3,totals,avg);
}
//call function 3
print_bottom(students,avg,min,max,above);
}
//function 1
void print_top()
{
cout<<"-----------------------------------------------"<<endl;
cout<<"Student"<<setw(3)<<"Score 1"<<setw(3)<<"Score 2"<<setw(3)<<"Score 3"<<setw(3)<<"total";
}
//function 2
void call_student(int students,int *scores1,int *scores2,int *scores3,int *totals,double avg)
{
int i;
cout<<i+1<<setw(3)<<scores1<<setw(3)<<scores2<<setw(3)<<scores3<<setw(3)<<totals;
if(totals[i]>avg)
{
cout<<"+"<<endl;
}else if(totals[i]<avg)
{
cout<<"-"<<endl;
}else
cout<<"="<<endl;
}
//function 3
void print_bottom(int students,double avg,int min,int max,int above)
{
cout<<"-----------------------------------------------"<<endl;
cout<<"-----------------------------------------------"<<endl;
cout<<"The number of Students is:"<<students;
avg = round(avg);
cout<<"The avg total score (rounded) is:"<<avg;
cout<<"The maximum total score is:"<<max;
cout<<"The minimum total score is:"<<min;
cout<<"Total scores at or above the avg is:"<<above;
cout<<"-----------------------------------------------"<<endl;
}
|