### Can anyone help me figure out my function isn't finding the largest integer?

My code compiles fine but it doesn't seem to want to calculate the max integer. It calculates min and average fine but I'm not seeing what is wrong with my code. The max integer keeps coming out wrong.

If anyone could help me out I'd greatly appreciate it.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687`` ``````#include using std::cin; using std::cout; using std::endl; #include #include 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 scorex[50]; for (int z = 0; z < nScores; z++) scorex[z] = score[z]; double avg = 0; if (nScores == 0) { return 1; } else { for (int x = 0; x < nScores; x++) { avg = avg + scorex[x]; } avg = avg / nScores; for (int y = 0; y < nScores; y++) { if (scorex[y] > maxScore) maxScore = scorex[y]; if (scorex[y] < minScore) minScore = scorex[y]; } avgScore = avg; return 0; } }``````
You referenced the maxScore in your stat() function uninitialized and you will get a garbage value (default from C++) by that.

Your code just messed up here except for getting the avg. You don't need any other copy of scores[] there.

You should do it this way
 ``12345678910`` `````` maxScore = minScore = avgScore = 0; for (int i = 0;i < nScores; ++i) { if (maxScore < scores[i]) maxScore = scores[i]; if(minScore > scores[i]) minScore = scores[i]; } ``````

And also initialized first all the values of your scores[MAX_SCORE] to zero.

or just declared it globally. This will initialized all values of scores[] to 0.
Topic archived. No new replies allowed.