 # Running Average Again

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

What’s wrong in this code???

``````#include <iostream>
#include <cstdio>

using namespace std;

int main() {

int n;
cin >> n;
if (n < 100000) {
int arr[n];
int count = 0;
double data = 0;

for (int i = 0; i < n; i++) {
count++;
cin >> arr[i];
if(arr[i] < 1 || arr[i] > 1000)
return 0;

data += (double) arr[i];
double result = (data / count);
printf("%.10f\n", result);
}
}

return 0;
}
``````
1 Like

@tariqul The problem statement has been updated. `N` can be equal to 100000.

``````#include <stdio.h>
float main()
{
int n,num[n];
scanf("%d",&n);
printf("\n");
if(n<=100000)
{
float s=0;
float av;
for(int i=0;i<n;)
{
scanf("%d",&num[i]);
s+=num[i];
i+=1;
av=s/i;
printf("%.5f\n",av);
}
}
}
``````

what’s the problem with this code?

1 Like

@Talha76 You don’t need this `printf`.

``````#include<stdio.h>
int main()
{
int N,i,sum=0;
float average;
scanf("%d",&N);
int a[N];
for(i=0;i<N;i++){
scanf("%d",a[i]);
}
for(i=0;i<N;i++){
sum=sum+(float)a[i];
average=sum/(i+1);
printf("%f\n",average);
}
return 0;
}
``````

what is the problem?

@Rabeya You missed an ampersand here. It should be `&a[i]`.

``````#include <stdio.h>
float main()
{
int n,num[n];
scanf("%d",&n);
float s=0;
float av;
for(int i=0; i<n;)
{
scanf("%d",&num[i]);
s+=num[i];
i+=1;
av=s/i;
printf("%f\n",av);
}

}
``````

whats the problem

@MD_Mynuddin Could you please try using `double` instead of `float`?

``````#include <stdio.h>
int main()
{
int n,i;
double r = 0;
scanf("%d",&n);
int N[n];
for(i = 0;i < n; i++){
scanf("%d",&N[i]);
r = (r+N[i])/(i+1);
printf("%.4lf\n",r);
}
return 0;
}
``````

what is wrong in my code?
I can’t submit

@Nilc_Zet If `r` is your running average, then you cannot add the next number to it directly.

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

Still showing error. It is showing wrong answer

Welcome to the community, @moc_55!

You are initializing the array `a` before knowing the value of `x`.

Runtime error I debugged it and everything okk but when i submit it says runtime error

#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,ar,i;
double total=0,avg=0;
scanf("%d",&n);
if(n<=100000)
for(i=0;i<n;i++)
{
scanf("%d",&ar[i]);
}
for(i=0;i<n;i++)
{
total+=ar[i];
avg=total/(i+1);
printf("%.10lf\n",avg);
}
return 0;
}

``````n=int(input())
num=0
for i in range(1, n+1):
z=int(input())
if (z<0 or z>1000):
exit()
else:
num+=z
ava=num/i
print(format(ava, ".10f"))
``````

Sir,I do not know why I’m facing runtime error

In your code, this won’t work:

``````z=int(input())
``````

Because, the second line will have `n` integers. Calling `input()` reads the entire line. To read `n` integers from a line, you can do this:

``````Z = map(int, input().split())
``````

Here, Z will now be a list with all of the integers in that line.

This is what happens in that line of code:

• `input()` reads the entire line as string.
• `split()` causes the string to become a list of strings where each space-separated word becomes an element of that list.
• `map(int, ...)` takes that list and converts each element to an int.
• The resulting list is assigned to Z.
1 Like

how can i pass the runtime problem?

``````#include <stdio.h>
#include<math.h>
int main() {
double ara,sum=0.0,ravg,dif;
int i,t;
scanf("%d", &t);
for(i=1; i<=t; i++){
scanf("%lf", &ara[i]);
sum += ara[i];
ravg = sum / i;
dif = ravg - (int) (ravg) ;
//dif = ceil(ravg) - floor(ravg);
if(dif<0.000001) printf("%.0lf\n", ravg);
else{
printf("%0.5lf\n", ravg);
}
}
return 0;
}
``````

Please check the constraint on N. The array you declared is too small.

1 Like