Need Help with another Simple Program

M

Max Norman

As an exercise out of Chris Pine's 'Learning to Program,' I've attempted
to build a program that calculates the number of leap years between two
given years. Here is my code:

puts 'Use this calculator to determine the number of leap years in a
specific period.'
total = 0 # total number of leap years
puts 'Enter the first year below:'
year_one = gets.chomp.to_i
puts 'Enter the second year below:'
year_two = gets.chomp.to_i

while year_one.to_i < year_two.to_i
if year_one%4 == 0
if year_one%100 != 0 || year_one%400 == 0
total = total + 1
year_one = year_one+ 1
else
year_one = year_one+ 1
end
end
end
puts 'Between ' + year_one.to_s + ' and ' + year_two.to_s + ', there
were '
puts total.to_s + ' leap years.'

-

This is a different approach than Pine takes (most of my attempts are
formulated differently, but based around the right concept). He does
this:
puts 'Pick a starting year (like 1973 or something):'
starting = gets.chomp.to_i
puts 'Now pick an ending year:'
ending = gets.chomp.to_i
puts 'Check it out... these years are leap years:'
year = starting
while year <= ending
if year%4 == 0
if year%100 != 0 || year%400 == 0
puts year
end
end
year = year + 1
end

-

What's going wrong?
 
B

brabuhr

As an exercise out of Chris Pine's 'Learning to Program,' I've attempted
to build a program that calculates the number of leap years between two
given years....

while year_one.to_i < year_two.to_i
=A0if year_one%4 =3D=3D 0
=A0 =A0if year_one%100 !=3D 0 || year_one%400 =3D=3D 0
=A0 =A0 =A0total =3D total + 1
=A0 =A0 =A0year_one =3D year_one+ 1
=A0 =A0else
=A0 =A0 =A0year_one =3D year_one+ 1
=A0end
end
end

What's going wrong?

Consider this snippet:

i =3D 0
while i < 1
if false
i +=3D 1 # oops, we never get here
end
end
 
M

Max Norman

Consider this snippet:

i = 0
while i < 1
if false
i += 1 # oops, we never get here
end
end

I'm not sure I understand this. What does that snippet exemplify?
 
7

7stud --

Max said:
I'm not sure I understand this. What does that snippet exemplify?

Look at this:

while year_one.to_i < year_two.to_i
if false
if year_one%100 != 0 || year_one%400 == 0
total = total + 1
year_one = year_one+ 1
else
year_one = year_one+ 1
end

Also, what value is stored in the variable year_one right before the
while loop starts?
 
M

Max Norman

Look at this:

while year_one.to_i < year_two.to_i
if false
if year_one%100 != 0 || year_one%400 == 0
total = total + 1
year_one = year_one+ 1
else
year_one = year_one+ 1
end

Also, what value is stored in the variable year_one right before the
while loop starts?

I think I've determined my problem; it relates to the variable
'year_one.' year_one is supposed to be the first year entered when the
program starts.

Check out my revision, which appears to work:

puts 'Use this calculator to determine the leap years in a specific
period.'
puts 'Enter the starting point below:'
start = gets.chomp.to_i
puts 'Enter the ending point below:'
ending = gets.chomp.to_i

year = start
total = 0

while year <= ending
if year%4 == 0
if year%100 != 0 || year%400 == 0
puts year
total = total + 1
end
end

year = year + 1

end

if total == 1
puts 'There is 1 leap year between '
puts start.to_s + ' and ' + ending.to_s + '.'
else
puts 'There are ' + total.to_s + ' leap years between '
puts start.to_s + ' and ' + ending.to_s + '.'
end
 
7

7stud --

Max said:
I think I've determined my problem; it relates to the variable
'year_one.' year_one is supposed to be the first year entered when the
program starts.

And what was year_one in your first post?
 
B

brabuhr

I'm not sure I understand this. What does that snippet exemplify?

In this snippet the loop exit condition will never be occur because i
will not be incremented. Your original version had a similar error:

while
if # when this is false
if
year_one =3D year_one+ 1 # we never get to here
else
year_one =3D year_one+ 1 # or here, so we keep looping
end#inner if
end#outer if
end#while

I wasn't sure how much you would want to be given away, so I tried to
keep my example abstract. :)
 
M

Max Norman

I wasn't sure how much you would want to be given away, so I tried to
keep my example abstract. :)

I appreciate your courtesy. I figured out my the problem a little while
ago, but your explanation is still useful.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top