system
November 11, 2018, 9:05am
1
Given N numbers, read each one, calculate the running average and print it. For example, given the t…
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”.
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
hjr265
January 10, 2019, 7:54pm
4
@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?
hjr265
January 20, 2019, 4:31pm
6
Talha76:
printf(“\n”);
@Talha76 You don’t need this printf
.
Rabeya
March 29, 2019, 9:30am
7
#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?
hjr265
March 29, 2019, 9:33am
8
Rabeya:
scanf(“%d”,a[i]);
@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
hjr265
April 29, 2019, 4:44am
10
@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
hjr265
April 29, 2019, 9:54am
12
@Nilc_Zet If r
is your running average, then you cannot add the next number to it directly.
1 Like
moc_55
May 11, 2019, 6:30pm
13
#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
hjr265
May 13, 2019, 11:23am
15
Welcome to the community, @moc_55 !
You are initializing the array a
before knowing the value of x
.
hjr265
May 13, 2019, 11:24am
16
Welcome to the community, @RDA_Programmer . Can you please share your code?
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[1000],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
hjr265
September 14, 2019, 5:37am
19
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[1001],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;
}
hjr265
September 18, 2019, 3:44pm
21
Please check the constraint on N. The array you declared is too small.
1 Like