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.
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
+ 3
@devil
I meant that's a better code..("your's")
+ 3
your welcome!
+ 2
thanks @Rockey but I want to give a random input to the function and numbers of input is also random.
+ 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;
+ 2
you can ignore the term "endl" it's just my habit to use.
+ 2
that's better now
+ 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.
+ 1
and also try to take---
float sum,n,mean;
+ 1
@rocky what you mean?
+ 1
@rocky thank you brother.....
+ 1
Thanks a lot @Devil ,really it's a better idea to use an array
+ 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.