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
|
// Lab 4
// Programmer: Naushil Mehta
// Editor(s) used: win 7 notepad
// Compiler(s) used: Visual Studio 2008
//
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int readArray(int, int[]);//implement step 2 of 5
int avg(int , const int[]); //implement step 3 of 5
int stat(int, const int[], int&, int&, int&); //implement step 4 of 5
int histogram(int,const int[], int[]);//implement step 5 of 5
int main()
{
const int MAX_SCORES = 100;
int score[MAX_SCORES];
//cout<< readArray(MAX_SCORES,score)<<endl;
int nScore = readArray(MAX_SCORES,score);
cout<<"Average of the array: "<<avg(nScore, score) <<endl;
//step 4 of 5
int avgScore, minScore, maxScore;
int grade[5]={0};
if (stat( nScore, score, avgScore, minScore, maxScore) == 0)
{
cout<<"Average = "<< avgScore << endl
<<"Max = "<<maxScore <<endl
<<"Min = "<<minScore <<endl;
//step 5 of 5
histogram(nScore,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;
}
cin.get();
cin.get();
return 0;
}
int readArray(int max_score, int score[])
{
int nScores =0;
for(int i =0; i< max_score; i++)
{
cout<<"Enter element(-1 to end input): ";
cin >> score[i];
cout<<endl;
if (score[i] == -1)
{
score[i] = NULL;
break;
}
else
{
nScores++;
}
if(nScores>=max_score)
break;
cin.ignore();
}
return nScores;
}
int avg(int n, const int score[])
{
int total = 0;
if (n == 0)
return 0;
for(int i = 0; i < n; i++)
{
total += score[i];
}
return total/n;
}
int stat(int n, const int score[], int& avg, int& mn, int& mx)
{
if (n == 0)
return 1;
int total = 0;
for(int i = 0; i < n; i++)
{
total =total + score[i];
if (( i == 0) || (mn > score[i]))
mn = score[i];
if ((i == 0) || (mx < score[i]) )
mx = score[i];
}
avg = total / n;
return 0;
}
int histogram(int nScore,const int score[], int grade[])
{
if(sizeof(score)/sizeof(int)==0)
return 1;
for(int i = 0; i < nScore; i++)
{
if (score[i] >= 90)
{
grade[0]++;
}
else if(score[i] >= 80)
{
grade[1] ++;
}
else if(score[i] >= 70)
{
grade[2] ++;
}
else if(score[i] >= 60)
{
grade[3] ++;
}
else
{
grade[4] ++;
}
}
return 0;
}
|