Running Average

Given $N$ numbers, read each one, calculate the running average and print it. For example, given the…

Click here to read the complete problem statement.


If you need help solving this problem, mention your approach and ask specific questions. Please avoid sharing your code and asking the Community to figure out “what’s wrong”.

N=int(input())
li=[0]*N
for i in range(N):
temp=int(input())
li[i]=temp
for i in range(N):
print(sum(li[:i+1])/(i+1))
where is the problem in my code???

#include<bits/stdc++.h>
using namespace std;

int main()
{
ios_base::sync_with_stdio(false);
cin.tie();

long double n, num=0;
cin >> n;
if(n<100) {
    for(long double i=1; i<=n; i++) {
        long double x;
        cin >> x;
        num += x;
        cout << num / i << "\n";
    }
}

return 0;

}

Where is the problem in my code???

//Bismillahir Rahmanir Rahim
//Tanvir Ahmmad IU CSE
#include <bits/stdc++.h>
using namespace std;
int main()
{
long long t,cnt=0;
double num, sum=0.0;
cin>>t;
while(t–)
{
cin>>num;
sum+=num;
cnt++;
printf(“%.7lf\n”,sum/cnt);
}
return 0;
}

@XeroCoder I have tried to run your code. The problem is that you have used cout without telling it how precisely it will print a number. In other words, you need to specify that after the point, how many digits it should print.

In line 16, just replace,

cout  << num / i << "\n";

with,

cout  << setprecision(9) << num / i << "\n";

and it should work out.

Also, something like,

if (n<100)

in line 10 is not necessary.

@Big_Pappa
In my case just using,

 printf("%.4f\n",(sum / count));

was enough. I don’t think you do need to print 7 digits after the point. Cause in the problem statement only printing it with 10-4 precision was specified. I think this will have solved it.

You should have given it some thought before asking it. The problem in your code is rather silly. I am not telling you to stop asking in the forum but try to solve it yourself before you ask someone else. Or else, you cannot learn.

Now, let’s give a look at your code.

1. N = int(input())
2. li = [0]*N
3. for i in range(N):
4. 	temp = int(input())
5. 	li[i] = temp
6. for i in range(N):
7. 	print(sum(li[:i+1])/(i+1))

Actually, to begin with, there is almost nothing right in your code. The actual problem is taking the input. The for loop that you have started in line 3 is not necessary. the input() function takes a line as an input as a string and you can convert it into an integer with int(). int() can only convert a string to integer if it contains numerical characters only. Cause, anything without numerical characters cannot.So, when you call The input() function each time, it is basically taking the whole line as an input() which can no longer be converted into an integer. For example,

As you can see, even if int() can convert “12345” to an integer, it cannot do it with “12 345”. Cause “12 345” is not a number.

Sample Input

3
4 2 7

So, in sample input line 2, your code cannot convert “4 2 7” into an integer and as a result shows a Runtime Error. The correct way to do this is to use map().

N = int(input())
li = list(map(int, input().split()))

and then the next part of your code,

for i in range(N):
	print(sum(li[:i+1])/(i+1))

should work.

Try to google it, to learn more about it.

By the way, I personally tried to run it. I will add a screenshot of it.

However, your code is very ineffeicient, try to improve it.

`

why it is saying wrong answer some one please help me

In the last line, it will be %lf since there was double in case of A.That’s all i can find it but i cant say it accurately since i am a beginner.

1 Like
#include<stdio.h>
int main()
{
	int ara[100];
	int a,b,c;
	float d;
	scanf("%d",&a);
	for(b=0;b<a;b++){
		scanf("%d",&ara[b]);
		}
	for(b=0;b<a;b++){
		c+=ara[b];
		d=c/(b+1);
		printf("%.2f\n",d);
		}
	return 0;
}

whats wrong here?

The errors in your code are:

  1. You have to specify that the result you are looking for is a double before d like
    d= (double) c/(b+1);
    2.The output information in the question specifies that the output has an accuracy of 10^-4 .
    Thus you have change the %.2f to %.4f.
    printf("%.4f\n",d);
#include<stdio.h>
#include<string.h>
int main()
{
    int l;
    scanf("%d",&l);
    float ara[l];
    float i,j,average,sum=0.0;
    for(i=0;i<l;i+=1){
        scanf("%f",&ara[i]);
    }
    for(i=0;i<l;i+=1){
        for(j=0;j<=i;j+=1){
            sum += ara[j];
        }
        average = sum / (i+1);
        printf("%f",average);
    }
    return 0;
}

What’s the problem here?

s=int(input())
a=0
for i in range(1,s+1):
	n=int(input())
	a=(n+a)/i
	print(a)

what’s wrong in this code?
it’s showing runtime error!

#include<iostream>
using namespace std;
int main()
{
    int N,i;
    cin>>N;
    double ara[N];
    double sum=0;
    for(i=0;i<N;i++){
        cin>>ara[i];
        sum+=ara[i]+ara[i+1];
        double avg=sum/(i+1);
        cout<<avg<<endl;
    }
    return 0;
}

Why the code is showing wrong answer in one test case.

why am i getting runtime error?

user = int(input())
while True:
    attempt = input()
    num = [int(val) for val in attempt.split()]
    if len(num) == user:
        result = 0
        for i in range(0, len(num)):
            result += num[i]
            i += 1
            avg = result / i
            print(avg)

Is it possible to solve this problem in C#?
I mean how can i take user input (int) in a loop, where each input separated by whitespace?

#include <stdio.h>
int main()
{
int num[30];
int n,i;

			double av,sum;
			av=sum=0;
			scanf("%d",&n);
			for(i=0;i<n;i++)
			{
							scanf("%d",&num[i]);
			}
			for(i=0;i<n;i++)
			{
							sum=sum+num[i];
							av=sum/(i+1);
							printf("%lf\n",av);
			}
			return 0;

}

//What is the problem in this code that it’s facing runtime error?//Screenshot_20201206-233529

help me please
#include
#include

using namespace std;

int main() {

int n;
cin >> n;
if (n < 100000) {
int arry[n];
int i,j=0;
double sum;
for(i=0;i<n;i++){
cin>>arry[i];
}
for(i=0;i<n;i++){
j++;
sum+=arry[i];
sum/=j;
printf("%.10f\n",sum);
}

}

return 0;

}