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
Following Program
Once you get a valid number of values ask for each value individually. Enumerate each value you are asking for.
Define 3 named constants.
Ask the user to enter a number of values and read it from the console.
Calculate the average and output it to the console. 3 digits after decimal point.
Create your variables, use the appropriate type, name them appropriately (camelback notation), and remember to not leave them uninitialized.
#include<iostream>
using namespace std;
int main(){
int a, i;
float avg = 0, y;
cout<<"How many numbers do do you want to add (Enter between 1 and 10)?";
cin>>a;
if(a>=2 && a<=10)
{
cout << "Enter " << a << " elements one after another\n";
for(i = 0; i < a; i++) {
cin >> y;
avg += y;
}
avg /= a;
cout << "\nThe average of the entered input numbers is = " << avg;
}
else{
cout<<"An error. Enter any number between 2 and 10 and try again";
}
}
Comments
Leave a comment