#include <iostream.h> using namespace std; double computeAverage(double inputs[]
ID: 3622357 • Letter: #
Question
#include <iostream.h>
using namespace std;
double computeAverage(double inputs[]);
bool isValidInput(double input);
int main()
{
double arrInput[10];
double average;
int trigger = 1;
cout << endl;
cout << "This program gives you the average of 10 positive numbers.";
cout << endl;
cout << endl;
while (trigger == 1) {
for (int i=1; i<=10; i++) {
cout << "Please enter in number " << i << ": ";
cin >> arrInput[i-1];
bool valid = isValidInput(arrInput[i-1]);
if (!valid) {
cout << endl;
cout << "Please enter in a positive number.";
cout << endl;
i--;
}
}
average = computeAverage(arrInput);
cout << endl;
cout << "The average of those numbers is: ";
cout << average;
cout << endl;
cout << endl;
cout << "Enter 1 to continue, 0 to quit. ";
cin >> trigger;
cout << endl;
}
return (0) ;
}
// ********************************************************
double computeAverage(double inputs[])
/* Computes the average of 10 positive numbers */
/* */
/* Inputs: */
/* array of 10 numbers (doubles) */
/* */
/* Output: */
/* average of those 10 numbers (double) */
{
double sum = 0.0;
double avg;
for (int i=0; i<10; i++) {
sum = sum + inputs[i];
}
avg = sum/10;
return (avg);
}
// ********************************************************
bool isValidInput(double input)
/* Determine if the input is a positive number */
/* */
/* Inputs: */
/* a double value */
/* */
/* Output: */
/* true if it's greater than 0, false otherwise */
{
if (input > 0.0) {
return true;
} else {
return false;
}
}
Explanation / Answer
The above program is used to compute the average of 10 positive decimal numbers. The first line is the header declaration used for cin , cout. Then the prototypes have been defined for the function computeAverage and isValid input. Then in main function array has been declared to get 10 input numbers. The trigger is used to know if the user wants to continue the program after completing calculation of average once again for different inputs. Then the input is got from user and checked to see if input is greater than 0 only then it is valid or the user has to enter another input again. After 10 valid inputs the average is calculated by first calculating the sum of 10 numbers and dividing by 10. User is prompted if program is to be run once again and based on value of trigger the program continues (while loop uses variable trigger for decision)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.