### Help with pulling integer sizes within a function

Hi,

I'm struggling with this lab assignment and I hope someone could give me some tips. I'm trying to find the MAX and MIN integer sizes that are entered in by the users and cannot figure out a good way to go about doing this. I know that we need to sort [i] and [j] in my function to figure out which is the highest and lowest score correct? I've just been stuck on this for a long time and would greatly appreciate any help I can get.

Thanks

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778`` ``````#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 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; }``````
If you want to print the values of min, total and average score in main, then you might want to make your stat function a void.

Since you are allowed to use the sort function, it makes this even easier to determine smallest and largest. Sort the array by calling sort and putting in your array. To determine min, print array[0], for largest print array[array_size-1]
Hi, thanks for the tip. However, our professor requires us to use this function.

` int stat(int, const int[], int&, int&, int&)`
None of the stats you are calculating require sorting the array -- they can all be calculated in a single pass through an unsorted array.

Set min and max to the first value in the array. If you run into an element that is less than min, update min. If you run into an element that is greater than max, update max.
Would it be possible to provide an example on how I could go about doing that? I've been working on something like this within the int stat function.

This of course does not compile but could anyone let me know if I'm at least moving in the right direction?

 ``1234567891011121314151617181920212223242526272829303132333435363738`` `````` 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; for (int j = 1; j < total; j++) { if (mn > score[j]) { mn = score[j]; } else if (mx < score [j]) { mx = score [j]; } } return 0; } else return 1; }``````
Topic archived. No new replies allowed.