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
|
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
#include <cstdlib>
int readArray(int, int[]);
int stat(int, const int[], int&, int&, int&);
int histogram(int, const int[], int[]);
int main()
{
const int MAX_SCORE = 50;
int score[MAX_SCORE];
int nscores = readArray(MAX_SCORE, score);
int minscore, maxscore, avgscore;
if (stat(nscores, score, minscore, maxscore, avgscore) == 0)
{
cout << "Min=" << minscore << endl;
cout << "Max=" << maxscore << endl;
cout << "Average=" << avgscore << endl;
int grade[5] = {};
histogram(nscores, score, grade);
cout << "A's: " << grade[0] << endl;
cout << "B's: " << grade[1] << endl;
cout << "C's: " << grade[2] << endl;
cout << "D's: " << grade[3] << endl;
cout << "F's: " << grade[4] << endl;
}
else
{
cout << "no data" << endl;
}
}
int readArray(int MAX_SCORE, int score[])
{
int nscores = 0;
char buf[100];
cout << "Enter up to 50 scores, separated by spaces\n";
cout << "Enter a Q after the last score is entered\n";
for(int i = 0; i < MAX_SCORE; i++)
{
cin >> buf;
score[i] = atoi(buf);
if (score[i] <= 0 || score[i] >= 100)
break;
if (score[i] == 'Q' || score[i] == 'q')
{
continue;
}
else
nscores++;
}
return nscores;
}
int stat(int nscores, const int score[], int& minscore, int& maxscore, int& avgscore)
{
minscore = score[0];
maxscore = score[0];
avgscore = 0;
if(nscores != 0)
{
for (int J = 0; J < nscores; J++)
avgscore += score[J];
avgscore /= nscores;
for (int i = 0; i < nscores; i++ )
{
if (minscore > score[i])
{
minscore = score[i];
}
if (maxscore < score[i])
{
maxscore = score[i];
}
}
return 0;
}
else return 1;
}
int histogram(int nscores, const int score[], int grade[])
{
for (int i = 0; i < nscores; i++)
{
if (score[i] >= 90)
{
grade[0] += 1;
}
if (score[i] > 80 && score[i] < 90)
{
grade[1] += 1;
}
if (score[i] > 70 && score[i] < 80)
{
grade[2] += 1;
}
if (score[i] > 60 && score[i] < 70)
{
grade[3] += 1;
}
if (score[i] > 0 && score[i] < 60)
{
grade[4] += 1;
}
}
return 0;
}
|