Average of a stream of numbers
PROBLEM :
Given a stream of numbers, print average or mean of the stream at every point.
Input:
The first line of input contains an integer T denoting the number of test cases.
The first line of each test case is N,N is the size of array.
The second line of each test case contains N input C[i].
Output:
Print the average of the stream at every point (in integer).
Constraints:
1 = T = 20
1 = N = 50
1 = C[i] = 100
Example:
Input
2
5
10 20 30 40 50
2
12 2
Output
10 15 20 25 30
12 7
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
SIMPLE SOLUTION O(n^2)
#include<iostream>
using namespace std;
int main()
{
int t,no,a[100],i,sum,j ;
cin>>t ;
while(t--)
{
cin>>no ;
for(i=0;i<no;i++)
cin>>a[i] ;
for(i=0;i<no;i++)
{
sum=0 ;
for(j=0;j<=i;j++)
{
sum=sum+a[j] ;
}
cout<<sum/(i+1)<<" " ;
}
cout<<endl ;
}
return 0;
}
--------------------------------------------------------------------------------
BETTER SOLUTION O(n)
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#include<math.h>
void print_avarage(int ,float* ,int ) ;
int main()
{
int t,no,a[50],i ;
float avg ;
cin>>t ;
while(t--)
{
cin>>no ;
for(i=0;i<no;i++)
cin>>a[i] ;
avg=0 ;
for(i=0;i<no;i++)
{
print_avarage(a[i],&avg,i) ;
}
cout<<endl ;
}
return 0;
}
void print_avarage(int ele,float *avg,int no)
{
float r=(ele+(*avg)*no)/(no+1) ;
cout<<floor(r)<<" " ;
(*avg)=r ;
}
---------------------------------------------------------------------------------
Given a stream of numbers, print average or mean of the stream at every point.
Input:
The first line of input contains an integer T denoting the number of test cases.
The first line of each test case is N,N is the size of array.
The second line of each test case contains N input C[i].
Output:
Print the average of the stream at every point (in integer).
Constraints:
1 = T = 20
1 = N = 50
1 = C[i] = 100
Example:
Input
2
5
10 20 30 40 50
2
12 2
Output
10 15 20 25 30
12 7
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
SIMPLE SOLUTION O(n^2)
#include<iostream>
using namespace std;
int main()
{
int t,no,a[100],i,sum,j ;
cin>>t ;
while(t--)
{
cin>>no ;
for(i=0;i<no;i++)
cin>>a[i] ;
for(i=0;i<no;i++)
{
sum=0 ;
for(j=0;j<=i;j++)
{
sum=sum+a[j] ;
}
cout<<sum/(i+1)<<" " ;
}
cout<<endl ;
}
return 0;
}
--------------------------------------------------------------------------------
BETTER SOLUTION O(n)
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#include<math.h>
void print_avarage(int ,float* ,int ) ;
int main()
{
int t,no,a[50],i ;
float avg ;
cin>>t ;
while(t--)
{
cin>>no ;
for(i=0;i<no;i++)
cin>>a[i] ;
avg=0 ;
for(i=0;i<no;i++)
{
print_avarage(a[i],&avg,i) ;
}
cout<<endl ;
}
return 0;
}
void print_avarage(int ele,float *avg,int no)
{
float r=(ele+(*avg)*no)/(no+1) ;
cout<<floor(r)<<" " ;
(*avg)=r ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment