I need help finding the average of user inputs BUT, The user does not need to know how many numbers s/he has to average. This my code so far, what do I need to do to make it right?
#include<iostream>
usingnamespace std;
int main(){
int i = 0;
double x = 0;
int howmany = 0;
int input = 0;
cout<<"how many numbers do you have" << endl;
cin >> howmany;
while(i < howmany){
cout << " enter numbers" << endl;
cin >> input;
x = x + input;
i++;
}
x = x/howmany;
cout << "The average is " << x << endl;
}
In other words find the average without the user having to input how many numbers they input-ed
Write a program that allows the user to average as many numbers (double floating point) as the user desires and repeat as many times as the user wants to calculate averages.
The program needs to ask the user to enter the numbers to be averaged. The user DOES NOT need to know how many numbers s/he has to average.
The program output must echo the user’s inputs and then show the user the average. The numerical output must always show the decimal point and provide two digits to the right of the decimal point, even if they are zeros.
I see... I didn't consider the part on the user not knowing.
I guess the best whay is to use a vector<int> to store the numbers (you don't have to worry about running out of memory) and a centinel to finish the input (say... input the numbers and type -1 to finish).
You don't have to worry about taking count of numbers, cause vectors have a method:
your_vector.length will give you the amount of numbers.
#include<iostream>
usingnamespace std;
int main(){
double x = 0;
int howmany = 0;
int anotherNumber;
do
{
cout<<"Enter a number: "<<endl;
cin>>x;
howmany++;
cout<<"Are you finished? 2 = finished. "<<endl;
cin>>anotherNumber;
}
while (anotherNumber!=2);
x = x/howmany;
cout << "The average is " << x << endl;
}
I can't compile it right now but I think that would work.
#include<iostream>
usingnamespace std;
int main(){
double x = 0, input;
int howmany = 0;
int anotherNumber;
do
{
cout<<"Enter a number: "<<endl;
cin>>input;
howmany++;
cout<<"Are you finished? 2 = finished. "<<endl;
cin>>anotherNumber;
}
while (anotherNumber!=2);
x += input/howmany;
cout << "The average is " << x << endl;
}