Home
Preface
Chapter Selection
Programming Examples
Homework Problems
Homework Solutions MEA's
Search Useful Links

Homework Problems

Chapter 4 Homework Problems

4.1 For each of the following subproblems convert the given loop type (while, until) to its opposite.
	a)  while (x = 5)
	b)  until (x < 7)
	c)  until ((x != 0) and (y > 2))
4.2 Why do you think Ruby has a while and until loop? Do you think Ruby should have both?
 
4.3 Other languages, such as C, have a "for" loop. It looks like this:
	for ([loop initialization]; [condition]; [statement to do after every 
	loop])
Implement this loop in Ruby.
4.4 A student has written the following code, why does the code in the loop not execute?
 
	1  #!/usr/bin/ruby
 	2  i = 0
 	3  while (i > 0)
 	4    # Do stuff...
 	5    i = i + 1
 	6  end
4.5 What is the output of the following program? Answer without actually running the program.
	1  #!/usr/bin/ruby
   	2  a = 4
    	3  b = 1
    	4  c = 10
    	5  d = 0
    	6  while (b < 8)
    	7    a = c + b
    	8    puts a
    	9    b = b * 2
   	10    puts b
   	11    d = b * b
   	12    puts d
   	13  end
4.6 Run through the following code in your head and explain what it does.
 	1  #!/usr/bin/ruby
 	2  puts “Enter a number >= 0: ”
 	3  n = gets.to_i
 	4  a = 1
 	5  while( n > 1 )
 	6    a = (n * (n – 1)) * a)
 	7    n = n – 2
 	8  end
 	9  puts a
4.7 Execute the following program. Record the output and explain why the program abnormally exits.
	
	1  #!/usr/bin/ruby
    	2  a = 2
    	3  b = 36
    	4  c = 6
    	5  d = 0
    	6  e = 2
    	7  while (a < 8)
    	8    d = (b / c) - 1
    	9    puts d
   	10    c = c - e
   	11    puts c
   	12    a = a + 1
   	13    puts
   	14  end
4.8 Imagine the process of eating at a restaurant. (Solutions may vary)
	
	a)  Give an algorithm for this process.
	b)  Now include the implementation details
4.9 In the following code, how many times will the inner loop execute?
  	1  #!/usr/bin/ruby
  	2  a = 7
  	3  b = 0
  	4  c = 11
  	5  until ( a < 3 )
  	6    while ( b <> 5 )
  	7      until ( c > 17 )
  	8        # Do stuff...
  	9        c = c + 3
 	10       end
 	11     b = b + 1
 	12    end
 	13    a = a – 1
 	14  end
4.10 Come up with an example for which the following loop types are needed.
	
	a)  Infinite loops
	b)  Nested loops	
	c)  Non-executing loops
4.11 Write a program to calculate compounded interest using a while loop. The user inputs the amount deposited, the interest rate (as a percentage) per period, and the number of periods the deposit accumulates interest. Compound interest means that every period, your new balance is calculated using the last period's balance times the interest rate.
 
4.12 Modify the program you wrote in 4.xx to also accept a deposit amount to add each period, before calculating interest.
 
4.13 Create a program that asks the user for two numbers, n and m. If m is greater than or equal to n, display an error and ask for the two numbers again. The program will find all numbers between n and m that are divisible by 2 and 3. The program output should list all numbers between n and m and will state which numbers are divisible by 2 and 3, which numbers are only divisible by 2 and which numbers are only divisible by 3. Those numbers that aren't divisible by 2 or 3 need no description. The value m must be smaller than the value n.
 
4.14 Implement the mod operator without using the mod operator and using a loop. (Assume the numerator is always greater than the denominator)
 
4.15 Write code that gives any number of points on a circle, with a given radius, centered at the a given point. Hint: Use Math.sin() and Math.cos()
 
4.16 Read the following code:
	1  total_feet = gets.chomp.to_i
	2  
    	3  puts total_feet.to_s + " feet is"
    	4  miles = 0
    	5  while total_feet - 5280 > 0
    	6    total_feet = total_feet - 5280
    	7    miles = miles + 1
    	8  end
   	9  
 	10  yards = 0
   	11  while total_feet - 3 > 0
   	12    total_feet = total_feet - 3
   	13    yards = yards + 1
   	14  end
   	15  
   	16  puts miles.to_s + " miles"
   	17  puts yards.to_s + " yards"
   	18  puts "and " + total_feet.to_s + " feet."
Write an explanation of what the code does, and refactor (i.e. Rewrite) the code to not use loops.
 
4.17 Investigate and describe another Ruby loop construct.
 
4.18 Count from 1 to 10 using a while, until, and for loop.
 
4.19 Using an until loop, write a program that will sum up consecutive even integers from 0 stopping when the sum exceeds 5000 and displaying the result before the sum exceeds 5000.
 
4.20 The following code is supposed to perform mathematical operations using the variables given but is not working correctly. Debug the code so it works.
	
	1  i = 1
    	2  j = 3
    	3  while (i < 5)
    	4    a = i * 2
    	5    puts a
    	6    while (j < 10)
    	7      b = j + a
    	8      puts b
    	9    end
   	10  end
4.21 Make a simple calculator. It should read in two numbers, and an operator (+, -, *, /), and display the result. It should continue to do this until a condition of your choosing stops it.
 
4.22 Write a program that outputs the first 20 numbers in the Fibonacci sequence. In the Fibonacci sequence, the current number is the sum of the previous two numbers. The first two numbers in the sequence are 1 and 1.
 
4.23 Write a program that uses a loop and a case statement that takes 5 test scores computes the average and issues a letter grade.
 
4.24 Explain how the following code is different from a while loop. When might this
	
	style of loop be useful?
  	1  yn = ""
  	2  begin
  	3    puts "Do you want to continue?"
   	4    yn = gets.chomp
   	5  end while (yn == "y")