Hi Guys I have a homework assignment and I cannot find the average number of random numbers for example: User inputs
1
2
-4
-3
8
and it goes until user inputs zero, When the zero is input, The program shows minimum and maximum values and the average of input numbers. I have no problems with minimum and maximum values but I couldn't find the average of these given numbers, Can you please help me on that issue. I will really appreciate it, Thank you
My Code is Like this:
#include<iostream>
usingnamespace std;
int main()
{
int largest;
int smallest;
int num;
bool isValid=true;
cout<<"Enter a number (0 to end)."<<endl;
while(num!=0){
cin>>num;
if(num>largest&&num!=0)
largest=num;
elseif(num<smallest&&num!=0)
smallest=num;
if(num==0){
cout<<"Largest number entered was: "<<largest<<endl;
cout<<"Smallest number entered was: "<<smallest<<endl;
isValid=false;
}
}
system("pause");
return 0;
}
Now I have to put the average. But I don't have idea on that can you help me?
Why could you not find the average of these numbers? Keep a running count of the number of integers entered and a running total, simply divide them after the input loop ends.
#include<iostream>
usingnamespace std;
int main()
{
int largest;
int smallest;
int num;
bool isValid=true;
cout<<"Enter a number (0 to end)."<<endl;
while(num!=0){
cin>>num;
if(num>largest&&num!=0)
largest=num;
elseif(num<smallest&&num!=0)
smallest=num;
average=(largest+smallest)/num;
if(num==0){
cout<<"Largest number entered was: "<<largest<<endl;
cout<<"Smallest number entered was: "<<smallest<<endl;
isValid=false;
}
}
system("pause");
return 0;
}
Sorry, IDE = "Integrated Development Environment", it could be Code::Blocks, MS Visual C++, Eclipse or a number of others. It is the program that you would use to help you write programs, are you using any of the ones I listed? Or are you using the compiler straight up?
I use Bloodshed Dev C++ Program to run the Code and sometimes Visual Studio 2010. But it is very slow so i prefer to use Dev C++. It compiles fast. Can you help to find solution for the problem that I have mentioned before, it is very important for me, Please, Thanks in advance! :D
Change line 16 to int average = (largest + smallest) / num; the spaces aren't needed. It looks like you forgot to declare "average" before you used it.
EDIT: It's been my experiance that if Visual Studio is compiling slow it's because of the "debug" version. Set it not to compile a debug version of your application and it should speed up considerably. Also please move away from Dev-C++: http://www.cplusplus.com/forum/articles/36896/
#include<iostream>
usingnamespace std;
int main()
{
int largest;
int smallest;
int num;
cout<<"Enter a number (0 to end)."<<endl;
while(num!=0){
cin>>num;
if(num>largest&&num!=0)
largest=num;
elseif(num<smallest&&num!=0)
smallest=num;
int average=(smallest+largest)/num;
if(num==0){
cout<<"Largest number entered was: "<<largest<<endl;
cout<<"Smallest number entered was: "<<smallest<<endl;
cout<<"The average is: "<<average<<endl;
}
}
return 0;
}
I will really appreciate this if you help me with this code. :)
After I wrote the code int average=(smallest+largest)/num; it started to show error. But I need to find average. It is one of the important issues.
either in loop, count the number of input numbers, and inside loop increment that counter, also take all number's sum. And, after loop, just do sum/counter
2nd solution would be:
double avg = 0;
while (num != 0)
{
if (n == 0)
break;