Running Average


This is a companion discussion topic for the original entry at https://toph.co/p/running-average

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,
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.
submission
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?

A much simpler solution in python:

n = int(input())

nums = input().split()
summ = 0
count = 0
for i in range(n):
summ += int(nums[i])
count += 1
avg = summ / count
if avg == int(avg):
print(int(avg))
else:
print(avg)

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
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.

//Please follow me on Instagram.
//instagram.com/saque_lain
//Thank you.

#include <stdio.h>
int main (void){
int nb, x;
double y;
int num;

scanf("%d", &nb);
for(x=0; x<nb; x++){
    scanf("%d", &num);
    y = y + num;
    printf("%lf\n", y/(x+1));
}
return 0;

}

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)