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
|
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
#include <cstdlib>
int readArray(int,int[]);
int avg(int, const int[]);
int stat(int, const int[], int&,int&,int&);
int histogram(int,const int[],int[]);
int main()
{
const int MAX_SCORE = 50; //the maximum # of scores that a user can enter
int score[MAX_SCORE]; //create storage for up to 50 scores
int nScores = readArray(MAX_SCORE, score); //read array and return count
int avgScore;
int minScore;
int maxScore;
if (stat(nScores, score, avgScore, minScore, maxScore) == 0)
{
cout << "The number of scores entered is: " << nScores << endl;
cout << "The average of the data entered is: " << avgScore << endl
<< "The maximum score entered is: " << maxScore << endl
<< "The minimum score entered is: " << minScore << endl;
int grade[5] = {0}; //={} is for newer compilers only
histogram(nScores, score, grade);
cout << "As: " << grade[0] << endl;
cout << "Bs: " << grade[1] << endl;
cout << "Cs: " << grade[2] << endl;
cout << "Ds: " << grade[3] << endl;
cout << "Fs: " << grade[4] << endl;
}
else
cout << "no data" << endl;
}
int readArray(int n,int accepted[])
{
char scores[100];
cout << endl;
//prompt user for input
cout << "Enter up to 50 test scores (0-100) after last score enter -1 to end: " << endl;
int scoresEntered = 0;
//read the scores from the keyboard, space and/or newline delimited
for (int i = 0; i < n; i++)
{
cin >> scores;
cin.ignore(0, ' ');
int entered = atoi(scores);
accepted[i] = entered;
if(accepted[i] < 0)
break;
else
scoresEntered++;
}
return scoresEntered;
}
int avg(int n,const int accepted[])
{
int total = 0;
if(n == 0)
{
return 0;
}
for(int i = 0;i < n; i++)
{
total = total + accepted[i];
}
return total / n;
}
int stat(int n,const int accepted[],int& avgs,int& min,int& max)
{
if(accepted[0] < 0)
{
return 1;
}
if(n == 0)
{
return 0;
}
avgs = avg(n, accepted);
min = accepted[0];
max = accepted[0];
for(int i = 0;i < n;i++)
{
if(accepted[i] < min)
{
min = accepted[i];
}
if(accepted[i] > max)
{
max = accepted[i];
}
}
return 0;
}
int histogram(int nScores, const int accepted[], int letterGrade[])
{
if(accepted[0] < 0)
{
return 0;
}
if(nScores == 0)
{
return 1;
}
for(int i = 0; i < nScores; i++)
{
if(accepted[i] >= 90)
{
letterGrade[0]++;
}
else if(accepted[i] >= 80 && accepted[i] <= 89)
{
letterGrade[1]++;
}
else if(accepted[i] >= 70 && accepted[i] <= 79)
{
letterGrade[2]++;
}
else if(accepted[i] >= 60 && accepted[i] <= 69)
{
letterGrade[3]++;
}
else if(accepted[i] < 50 && accepted[i] > 0)
{
letterGrade[4]++;
}
}
}
|