Answer to Question #301898 in C++ for Hera

Question #301898

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 


1
Expert's answer
2022-02-24T04:19:52-0500


#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog