Solution to Write a Program: Compute an average of integer values Ask the user to enter a … - Sikademy
Author Image

Archangel Macsika

Write a Program: Compute an average of integer values Ask the user to enter a number of values to average in the range from 2 to 10. Use a loop to make sure the entered number is within the range. Output an error message any time an invalid number is entered. Once a valid number of values is entered ask the user to input each value. Enumerate the values being asked for (see the output example). Your goal is to calculate their average and output it to the console. Your output should look similar to the following: First run Enter a number of values from 2 to 10: 1 Invalid input! Enter a number of values from 2 to 10: 11 Invalid input! Enter a number of values from 2 to 10: 6 Enter value 1: 5 Enter value 2: 8 Enter value 3: 45 Enter value 4: 11 Enter value 5: 6 Enter value 6: 1 The average is 12.667 Second run Enter a number of values from 2 to 10: 4 Enter value 1: 41 Enter value 2: 55 Enter value 3: 12 Enter value 4: 9 The average is 29.250

The Answer to the Question
is below this banner.

Can't find a solution anywhere?

NEED A FAST ANSWER TO ANY QUESTION OR ASSIGNMENT?

Get the Answers Now!

You will get a detailed answer to your question or assignment in the shortest time possible.

Here's the Solution to this Question


#include <iostream> #include <iomanip> using namespace std; int main(){ int numberValues=0; while(numberValues<2 || numberValues>10){ cout<<"Enter a number of values from 2 to 10: "; cin>>numberValues; if(numberValues<2 || numberValues>10){ cout<<"Invalid input!\n"; } } double average; double sum=0; double value=0; for(int i=0;i<numberValues;i++){ cout<<"Enter value 1: "; cin>>value; sum+=value; } average=sum/(double)numberValues; cout<<fixed<<setprecision(3)<<"The average is "<<average<<"\n"; system("pause"); return 0; }

Related Answers

Was this answer helpful?

Join our Community to stay in the know

Get updates for similar and other helpful Answers

Question ID: mtid-3-stid-44-sqid-1099-qpid-95