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
|
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
#include <cstdlib>
#include <algorithm>
using std::swap;
// function prototype
int readArray(int, int[]);
// int avg(int, const int[]); // delete it for step 4
int stat(int, const int[], int&, 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 cout
// cout << "Average of the scores: " << avg(nScores, score) << endl; // say the average of the scores
int avgScore, minScore, maxScore;
if (stat(nScores, score, avgScore, minScore, maxScore) == 0)
cout << "Average=" << avgScore << endl
<< "Max=" << maxScore << endl
<< "Min=" << minScore << endl;
else
cout << "no data" << endl;
}
int readArray(const int MAX_SCORE, int score[])
{
cout << "Enter up to 50 numbers (enter a -1 after the last score is entered): " << endl;
int nScores = 0; // count the number of scores entered
//read the scores from the keyboard, space and/or newline deliminated
for (int i = 0; i < MAX_SCORE; i++)
{
char buf[100];
cin >> buf;
score[i] = atoi(buf);
if (score[i] < 0)
break; // enter no more scores after the 1st negative is found
else
nScores++; // count the score if it is non-negative
}
cout << "The number of scores entered is: " << nScores << endl; // output the number of scores entered
return nScores;
}
int stat(int nScores, const int score[], int &avgScore, int &minScore, int &maxScore)
{
int total = 0;
if (nScores != 0)
{
for (int i = 0; i < nScores; ++i)
total = total + score[i];
/*
for (int j = i + 1; j < nScores, ++j)
{
if( score[j] < score [i])
minScore= score[j]
}
*/
avgScore = total / nScores;
return 0;
}
else
return 1;
}
|