0

How to give many input at a time?

I want to create a function to calculate mean of n numbers , where n=1,2....... like mean of 4,48,53,47,84 here n=5 but I don't know how to give desired numbers of input at a time plzz help me.

3rd Jul 2017, 8:59 AM
Raushan Kumar
Raushan Kumar - avatar
13 Answers
+ 3
use an array....for example:- for n int values.. int n,sum=0; int a[ ]; cout<<"enter the number of values="; cin>>n; for(int i=0;i<n;i++) { cout<<"enter "<<i+1<<" value="; cin>>a[i]; sum+=a[i]; } int mean; mean=sum/n; cout<<"mean is="<<mean; hope you got it
3rd Jul 2017, 9:27 AM
DEVIL kingg
DEVIL kingg - avatar
+ 3
@devil I meant that's a better code..("your's")
3rd Jul 2017, 9:31 AM
Rocky
Rocky - avatar
+ 3
your welcome!
3rd Jul 2017, 9:32 AM
Rocky
Rocky - avatar
+ 2
thanks @Rockey but I want to give a random input to the function and numbers of input is also random.
3rd Jul 2017, 9:11 AM
Raushan Kumar
Raushan Kumar - avatar
+ 2
ok I'm giving u random mean for 3 numbers int x,y,z,sum; float mean; cout<<"Enter the first number"<<endl; cin>>x; cout<<"Enter the second number"<<endl; cin>>y; cout<<"Enter the third number"<<endl; cin>>z; sum=x+y+z; mean=sum/3; cout<<"The mean value="<<mean<<endl;
3rd Jul 2017, 9:22 AM
Rocky
Rocky - avatar
+ 2
you can ignore the term "endl" it's just my habit to use.
3rd Jul 2017, 9:22 AM
Rocky
Rocky - avatar
+ 2
that's better now
3rd Jul 2017, 9:29 AM
Rocky
Rocky - avatar
+ 1
if it's n numbers then sum=1+2+...+n mean=sum/n I guess ..sorry if it's not correct not started c++ yet just the basic i know.
3rd Jul 2017, 9:04 AM
Rocky
Rocky - avatar
+ 1
and also try to take--- float sum,n,mean;
3rd Jul 2017, 9:07 AM
Rocky
Rocky - avatar
+ 1
@rocky what you mean?
3rd Jul 2017, 9:29 AM
DEVIL kingg
DEVIL kingg - avatar
+ 1
@rocky thank you brother.....
3rd Jul 2017, 9:32 AM
DEVIL kingg
DEVIL kingg - avatar
+ 1
Thanks a lot @Devil ,really it's a better idea to use an array
3rd Jul 2017, 9:38 AM
Raushan Kumar
Raushan Kumar - avatar
+ 1
@raushan....your welcome brother....yes indeed if you use my program you can enter at least 1 and atmost thousands of value depending on users choice.
3rd Jul 2017, 9:44 AM
DEVIL kingg
DEVIL kingg - avatar