I am trying to find the number of zero crossing in a list. The code I am using is:
JavaScript
x
8
1
for i in range(1, len(value)):
2
zerocrossing = 0
3
if ((value[i-1]) > 0 and value[i] < 0):
4
zerocrossing += 1
5
if ((value[i-1]) < 0 and value[i] > 0):
6
zerocrossing += 1
7
stdio.writeln('The Number of Zero Crossings is ' + str(zerocrossing))
8
This code doesn’t give me any errors, but it’s not giving me the right answer. If I give it the input [1.0, -1.0, 1.0]
it gives me 1
, but it should give 2
. What am I doing wrong?
Advertisement
Answer
You’re setting zerocrossing
to zero at every loop iteration. Move zerocrossing = 0
outside the for-loop.