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???
@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.
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.
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);
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)