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

Homework Solutions

Chapter 4 Homework Solutions

4.1
	a) until (x != 5) 
	b) while (x >= 7)
	c) while ((x = 0) or (y <= 2))
4.2 A while loop signals to the reader that the condition it is testing is a continue condition. An until loop signals that the condition is an abort condition. Even though "until XXX" can be expressed as "while not XXX" and vice-versa, the cost of the extra syntax is paid by the benefit to the reader.
 
4.3
	[loop initialization]
	while [condition]
	  ...
	  [statement to do after every loop]
	end
4.4 Need to change line three to read: while(i >= 0)
 
4.5 The program will output the following numbers, each on a separate line:

11, 2, 4, 12, 4, 16, 14, 58, 64

 
4.6 This code calculates the factorial of the number inputted.
 
4.7
	1  5
	2  4
	3  
	4  8
	5  2
	6  
	7  17
	8  0
	9  
	10  divzero.rb:8:in `/': divided by 0 (ZeroDivisionError)
The program abnormally exits with a ZeroDivisionError, because the third time through the loop, c is 0.
 
4.8
	Go to restaurant
	Get seated
	Order
	Eat 
	Pay
	Leave

		Go to restaurant A
		See host/ess
			If host/ess is ready to seat you – sit
			Otherwise, wait for host/ess
		Order drink B
		Wait C mins
		Get drink B
		Order food D
		Wait E mins
		Get food D
		Eat food D
		Ask for check
		Receive check
		Pay amount F
		Leave tip G
		Leave restaurant A
4.9 The innermost loop executes 2 times, both other loops execute 5 times. 5 * 5 * 2 = 50 iterations of the inner loop.
 
4.10 Infinite loops are needed for things such as menu prompts and constant input reading. Nested loops are needed for things such as traversing a 2D array, calculating powers, and sorting algorithms. Non-executing loops are not needed. If it is truly a never executing loop, the code inside will never do anything, and thus there is no need for such a loop type.
 
4.11
   	1  balance = gets.chomp.to_i
   	2  interest_rate = gets.chomp.to_f / 100
   	3  periods = gets.chomp.to_i
   	4  while (periods > 0)
   	5    balance = balance * (1 + interest_rate)
   	6    periods = periods - 1
   	7  end
   	8  puts balance
4.12
   	1  balance = gets.chomp.to_i
    	2  deposit = gets.chomp.to_i
    	3  interest_rate = gets.chomp.to_f / 100
    	4  periods = gets.chomp.to_i
    	5  while (periods > 0)
    	6    balance = balance + deposit
    	7    balance = balance * (1 + interest_rate)
    	8    periods = periods - 1
    	9  end
   	10  puts balance
4.13
 	1  #!/usr/bin/ruby    
 	2  puts "Enter a value for m"
 	3  m = gets.to_i
 	4  puts "Enter a value for n (Must be higher than m)"
 	5  n = gets.to_i
 	6  while (m >= n)
 	7    puts "n must be higher than m--Try Again"
 	8    puts "Enter a value for m"
 	9    m = gets.to_i
	10    puts "Enter a value for n (Must be higher than m)"
	11    n = gets.to_i
	12  end
	13  while (m < n)
	14
	15    if (m%2 == 0 && m%3 == 0)
   	16      puts m.to_s + "is divisible by 2 and 3"
   	17    elsif (m%2 == 0)
   	18      puts m.to_s + "is divisible by 2 only"
   	19    elsif (m%3 == 0)
   	20      puts m.to_s + "is divisible by 3 only"
   	21    end
   	22    m=m+1
   	23  end
4.14
   	1  #!/usr/bin/ruby
    	2  puts “Enter numerator: “
    	3  num = gets.to_i
    	4  puts “Enter denominator: ”
    	5  den = gets.to_i
    	6  total = 0
    	7  if( den != 1 ) then
    	8    while ( total < num )
   	9      total = total + den
   	10    end
   	11    total = total – denom
   	12    ans = num – total
   	13    puts ans
   	14  else
   	15    puts 1
   	16  end
4.15
   	1  PI = 3.14159265
    	2  center_x = gets.to_i
    	3  center_y = gets.to_i
    	4  radius = gets.to_i
    	5  total_points = gets.to_i
    	6
    	7  point = 1
    	8  while (point <= total_points)
   	9    x = Math.sin((2 * PI * point) / total_points) * radius
   	10    y = Math.cos((2 * PI * point) / total_points) * radius
   	11    puts "(" + x.to_s + ", " + y.to_s + ")"
   	12    point = point + 1
   	13  end
4.16 The code converts a given amount of feet to a combination of feet, yards, and miles.
    	1  total_feet = gets.chomp.to_i
    	2  
    	3  puts total_feet.to_s + " feet is"
    	4  miles = total_feet % 5280
    	5  total_feet = (total_feet - (5280 * miles))
    	6  yards = total_feet % 3
    	7  total_feet = (total_feet - (3 * yards)
    	8  
    	9  puts miles.to_s + " miles"
   	10  puts yards.to_s + " yards"
   	11  puts "and " + total_feet.to_s + " feet."
4.17 Ruby has a for loop, used for iterating over a list of objects. for i in x...y i is the current iteration For the very first iteration i = x The loop ends when i = y
 
4.18
   	1  num = 1
   	2  while (num <= 10)
   	3    puts num
   	4    num = num + 1
   	5  end

   	1  num = 1
   	2  until (num > 10)
   	3    puts num
   	4    num = num + 1
   	5  end

   	1  for num in 1..10
   	2    puts num
   	3  end
4.19
   	1  sum = 0
    	2  i = 2
    	3  until (sum > 5000)
    	4    sum = sum + i
    	5    # debugging test
    	6    # puts "sum ==> " + sum.to_s
    	7    i = i + 2
    	8    # debugging test
    	9    # puts "i ==> " + i.to_s
   	10  end
   	11  sum = sum - i
   	12  # Going back to the value before 5000
   	13  puts sum
4.20 In both the outer loop and the inner loop the i and j values are never incremented so the loop will not end. By adding increment values such as j = j + 1 and i = i + 1 will fix the code.
    	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      j = j + 1
   	10   end
   	11   i = i + 1
   	12  end
4.21
  		1  want_to_quit = false
    	2  while not want_to_quit
    	3    input1 = gets.chomp
    	4    if input1 == "q"
    	5      want_to_quit = true
    	6    else
    	7      num1 = input1.to_i
    	8      num2 = gets.chomp.to_i
    	9      result = nil
   	10      until result
   	11        operator = gets.chomp
   	12        case
   	13        when "+" then result = num1 + num2
   	14        when "-" then result = num1 - num2
   	15        when "*" then result = num1 * num2
   	16        when "/" then result = num1 / num2
   	17        else
   	18          puts "invalid operator - try again"
   	19        end
   	20      end
   	21      puts "=" + result.to_s
   	22    end
   	23  end
When the user enters "q" for the first number, the calculator will stop running.
 
4.22
   	1  a = 1
    	2  puts a
    	3  b = 1
    	4  puts b
    	5  i = 1
    	6  while (i <= 20)
    	7    c = a + b
    	8    puts c
    	9    a = b
   	10    b = c
   	11    i = i + 1
   	12  end
4.23
   	1  avg = 0.0
    	2  sum = 0.0
    	3  score = 0.0
    	4  i = 1
    	5  while (i <= 5)
    	6    puts "Enter score #" + i.to_s
    	7    score = gets.to_f
    	8    sum= score + sum
    	9    i = i + 1
   	10  end
   	11  avg = sum/5
   	12  case
   	13    when (avg < 60) then puts "Grade=F"
   	14    when (avg >= 60 && avg < 70) then puts "Grade=D"
   	15    when (avg >= 70 && avg < 80) then puts "Grade=C"
   	16    when (avg >= 80 && avg < 90) then puts "Grade=B"
   	17    when (avg >= 90) then puts "Grade=A"
   	18  end

4.24 The loop is different because the code within the loop will always be executed. If the above loop was a while loop, it would never ask if you wanted to continue. This kind of loop is useful when you need to data and modify it before checking if it in the condition.