Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi, I have a question about my C++ code. I just want to verify if everything is

ID: 3591184 • Letter: H

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 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 <iostream>

using std::cout;

using std::cin;

using std::endl;

#include <cstdlib>

//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); //necessary to count the number of scores entered

//cout statements to guide the user

cout << "Enter up to 50 scores (0-100), enter a 'q' or 'Q' after the last score is entered." << endl;

//necessary to call the scores in the array from the function

int minScore, maxScore, avgScore;

if (stat(nScores, score, minScore, maxScore, avgScore) == 0)

{

cout << "Min= " << minScore << endl

<< "Max= " << maxScore << endl

<< "Avg= " << avgScore << endl;

int grade[5] = {};

histogram(nScores, score, grade);

cout << "As: " << grade[0] << endl

<< "Bs: " << grade[1] << endl

<< "Cs: " << grade[2] << endl

<< "Ds: " << grade[3] << endl

<< "Fs: " << grade[4] << endl;

}

else

cout << "no data, exiting..." << endl;

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 < nScores; i++) {

if (arr[i] < min) min = 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 < MAX_SIZE; i++) //read the scores from the keyboard, space and/or newline delimited

{

//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] < 0 || 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 < nScores; i++)

{

//A

if (arr[i] >= 90)

grade[0] += 1;

//B

if (arr[i] >= 80 && arr[i] < 90)

grade[1] += 1;

//C

if (arr[i] >= 70 && arr[i] < 80)

grade[2] += 1;

//D

if (arr[i] >= 60 && arr[i] < 70)

grade[3] += 1;

//F

if (arr[i] <= 50)

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

Explanation / Answer

Yes , everything's working fine . There were some unnecessary if-else which i have removed.

the steps "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" states-

number of numbers to be averaged means the total number of scores of which you want to find average. Now, if total numbers=0 (means nScores=0), then when you attempt to find average (sum/nScores, here nScores=0), it will cause divide by zero exception because you cant divide a number by 0.Therefore, to avoid run time error return 0 for average if   total number of scores=0. .

And also, if number of numbers in array is 0 means array has no elements, then min, max and avg ofcourse can't be set to any value. So, don't set these three parameters.

#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <cstdlib>
//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
//cout statements to guide the user
cout << "Enter up to 50 scores (0-100), enter a 'q' or 'Q' after the last score is entered." << endl;
int nScores = readArray(MAX_SCORE, score); //necessary to count the number of scores entered

//necessary to call the scores in the array from the function
int minScore, maxScore, avgScore;
if (stat(nScores, score, minScore, maxScore, avgScore) == 0)
{
cout << "Min= " << minScore << endl
<< "Max= " << maxScore << endl
<< "Avg= " << avgScore << endl;
int grade[5] = {};
histogram(nScores, 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;
system("pause");
}
int stat(int nScores, const int arr[], int& min, int& max, int& avg) {
if (nScores == 0)
{
avg = 0;
return 1;
}
else {
int sum = 0;
min = arr[0];
max = arr[0];
for (int i = 0; i < nScores; i++) {
if (arr[i] < min) min = arr[i];
if (arr[i] > max) max = arr[i];
sum += arr[i];
}
avg = sum / nScores;
return 0;
}
}
int readArray(int MAX_SIZE, int arrayToBeFilled[])
{
int nEntries = 0;
for (int i = 0; i < MAX_SIZE; i++) //read the scores from the keyboard, space and/or newline delimited
{
//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 number or a number greater than 100, skip i
if (arrayToBeFilled[i] < 0 || 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[]) {
for (int i = 0; i < nScores; i++)
{
//A
if (arr[i] >= 90)
grade[0] += 1;
//B
if (arr[i] >= 80 && arr[i] < 90)
grade[1] += 1;
//C
if (arr[i] >= 70 && arr[i] < 80)
grade[2] += 1;
//D
if (arr[i] >= 60 && arr[i] < 70)
grade[3] += 1;
//F
if (arr[i] <= 50)
grade[4] += 1;
}
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote