To Find the Average of submitted numbers

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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>
using namespace 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;
else if(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?
Last edited on
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.
What do you have so far? Post it here with code tags if you would be so kind, it's the button to the lower right that looks like this "<>".
I submitted my code. Now I have to put the code inside this program but I don't have exact idea how to do this.
You don't know how to use your IDE?
I am newbie and what is the relationship between my question and IDE? I don't understand the gravity of your Question Computergeek01 Here is my COde:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<iostream>
using namespace 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;
else if(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/
Last edited on
I did as you said but program didn't work it Shows error. The Error Message Box Appeared saying Just-In-Time Debugger. :(
Can't you turn that off? Sorry I don't have that IDE installed on this PC anymore. I'll google it.
Guys I am really sorry but it didn't work. Now I am using Visual C++ 2010 and it shows error and the error is that the
num
is not initialized can you give me another solution my code is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>
using namespace 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;
else if(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.
you have two options:

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;

//input number into n variable
avg = (avg + n)/2;

}


Try this.
Gorav
http://www.kgsepg.com
The second one is not working. I didn't test the first one. As I understood in the first it should be:
1
2
3
4
int avg;
int sum;
for(int counter=0;counter<=sum; counter++)
avg=sum/counter;

Like this?
Can you help me to implement this code into a code, the exact place it should be staying.
Thanx in Advance.
Another way to do this:
1. get input from user: check the input for error (e.g when user enters a letter or characters or floating point numbers) ref: http://www.cplusplus.com/forum/beginner/4566/#msg20182
2. store input in dynamic array i.e vector: ref: http://www.cplusplus.com/reference/stl/vector/
3. sort the vector/array using sort algorithm: http://www.cplusplus.com/reference/algorithm/sort/
- you'll get the smallest number in the first and largest in last element
4. add up all the vector elements and divide it by its size to get your average -use for loop
ref: http://www.cplusplus.com/doc/tutorial/control/
Well for one, you're comparing num to largest when largest hasn't even been initialized.
Thank you guys, I have solved this problem. Thanks for your ideas and hints :D
Topic archived. No new replies allowed.