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

Write a function whose job is to input #\'s, non-# to quit, recover from the inp

ID: 3557515 • Letter: W

Question

Write a function whose job is to input #'s, non-# to quit, recover from the input failure by calling recover(), and tell main some statistics about the numbers read.

A. unsigned readA();

Read unsigned #'s, non-# to quit, recover, return the sum of all #'s read.

For example, if main says

            cout <<"The total is " <<readA() <<endl;

and if the user types

            2 4 6 8 blah

then readA will read the numbers 2, 4, 6, 8, keeping track of the total of the numbers read, readA will then choke on the "blah", readA will then, call recover() so that cin recovers from the input failure and moves past the "blah", readA will then return the 20, then main will resume the cout statement so that it outputs

            The total is 20

B. void readB( double & sum );

Same as above, except that readB informs main of the answer by setting a reference arg instead of by giving a return value.

main should call readB and main should output the answer.

C. double readC();

This time readC returns the largest #. readC should c&d if there aren't any #'s before the input failure.

D. void readD( double & max );

readD, like above, informs main of the largest #, again complaining and dying if there aren't any numbers, but this time readD informs main of the answer through a reference arg instead of a return value.

E. void readE( double & max, double & min, double & average );

Like above, but this time there are 3 values that the function informs the main function of instead of just 1. (As readE moves through the input data, you'll need to keep track of 4 quantities as you go:

            the largest # seen so far

            the smallest # seen so far

            the number of #'s seen so far

            the sum of the #'s seen so far

)

Explanation / Answer

unsigned readA(){
unsigned total = 0, a;
while(true){
try{
cin >> a;
if(cin.fail()) throw 0;
total += a;
}
catch(int x){
cout << "The total is " << total << " ";
break;
}
}
}

void readB(double &sum){
sum = 0;
unsigned a;
while(true){
try{
cin >> a;
if(cin.fail()) throw 0;
sum += a;
}
catch(int x){
break;
}
}
}

double readC(){
double max = -1;
unsigned a;
while(true){
try{
cin >> a;
if(cin.fail()) throw 0;
if(max < a) max = a;
}
catch(int x){
if(max == -1) return 0;
break;
}
}
return max;
}

void readD(double &max){
max = -1;
double a;
while(true){
try{
cin >> a;
if(cin.fail()) throw 0;
if(max < a) max = a;
}
catch(int x){
if(max == -1) return;
break;
}
}
}

void readE(double &max, double &min, double &average){
double a, sum = 0;
int count = 0;
try{
cin >> a;
if(cin.fail()) throw 0;
max = a;
min = a;
sum = a;
count++;
average = sum / count;
}
catch(int x){
}
while(true){
try{
cin >> a;
if(cin.fail()) throw 0;
count++;
if(max < a) max = a;
if(min > a) min = a;
sum += a;
average = sum / count;
}
catch(int x){
if(max == -1) return;
break;
}
}
}

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