Question: Hi, I have a question about my C++ code. I just want to verify if everything is fixed and I do not understand the steps
Hi, I have a question about my C++ code. I just want to verify if everything is fixed and I do not understand the steps that say "To avoid run-time errors as a result of dividing by zero, check the number of numbers to be averaged -- if it is zero, return 0 for the average." and "If the number of numbers in the array is zero, do not set the min, max, and average (which are specified as the 3rd, 4th, and 5th parameters.)" Thanks.






This is the code:
//Allows the program to perform input and output
#include
using std::cout;
using std::cin;
using std::endl;
#include
//function templates
int readArray(int, int[]);
int stat(int, const int[], int&, int&, int&);
int histogram(int, const int[], int[]);
int main()
{
//program starts here:
const int MAX_SCORE = 50; //the maximum # of scores that a user can enter
int score[MAX_SCORE]; //create storage in the array, up to 50 scores
int nScores = readArray(MAX_SCORE, score); /ecessary to count the number of scores entered
//cout statements to guide the user
cout
/ecessary to call the scores in the array from the function
int minScore, maxScore, avgScore;
if (stat(nScores, score, minScore, maxScore, avgScore) == 0)
{
cout
int grade[5] = {};
histogram(nScores, score, grade);
cout
}
else
cout
system("pause");
}
int stat(int nScores, const int arr[], int& min, int& max, int& avg) {
if (nScores == 0)
{
return 1;
}
else {
int sum = 0;
min = arr[0];
max = arr[0];
for (int i = 0; i
if (arr[i]
if (arr[i] > max) max = arr[i];
sum += arr[i];
}
if (nScores == 0)
avg = 0;
else
avg = sum / nScores;
return 0;
}
}
int readArray(int MAX_SIZE, int arrayToBeFilled[])
{
int nEntries = 0;
for (int i = 0; i
{
//read the user input, stored in bufer, and passed to the array 'arrayToBeFilled'
char buf[100];
cin >> buf;
arrayToBeFilled[i] = atoi(buf);
//If a user enters a negative numebr or a number greater than 100, skip i
if (arrayToBeFilled[i] 100)
{
i -= 1; //way to "rewind" i before continue; to avoid "holes" such as leave a position unused.
continue; //continue and skip the negative numbers
}
else if (buf[0] == 'q' || buf[0] == 'Q')
break; //break the loop when the user enters 'q' or 'Q'
else
nEntries++; //count the score if it is non-negative
}
return nEntries; //return the # of entries to the main function
}
int histogram(int nScores, const int arr[], int grade[]) {
if (nScores == 0)
{
return 1;
}
else {
for (int i = 0; i
{
//A
if (arr[i] >= 90)
grade[0] += 1;
//B
if (arr[i] >= 80 && arr[i]
grade[1] += 1;
//C
if (arr[i] >= 70 && arr[i]
grade[2] += 1;
//D
if (arr[i] >= 60 && arr[i]
grade[3] += 1;
//F
if (arr[i]
grade[4] += 1;
}
return 0;
}
}
STEP 3 of 5: Compute Averages Using Arrays Modifyfrom step 2, by adding a function to compute the average of the scores in the keyboard- entered array. 1. Use this prototype for the added function: int avglint, const int). The meaning of the return value and each of the parameters is outlined below in instructions 2-4. 2. The function is to return the simple integer average of the numbers read into the array. 3. The first function parameter is the number of numbers in the array to be averaged together. 4. The second function parameter is the array that was filled. It is passed as an array without the size specified, because the array can be of any size, depending on the number of values to be averaged. (The actual size is passed in the first parameter.) 5. To avoid run-time errors as a result of dividing by zero, check the number of numbers to be averaged - if it is zero, return O for the average Use the following code for the int main) function. Put all other code in the functions, to be written by you int main) const int MAXSCORE-50; // the maximum #of scores that a user can enter int score [MAX ScORE]; 11 create storage for up to 50 scores int nscores readArray (MAX_ SCORE, score); // read array and return count cout
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
