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

Homework Solutions

Chapter 3 Homework Solutions

3.1
puts 'enter an integer'
x=gets
x=x.to_i
xnew=x/2
xnew=2*xnew
if x==xnew
	puts 'you entered an even integer'
else
	puts 'you entered an odd integer'
end
3.2a
puts"enter a value between five and ten "
x=gets
x=x.to_i
if (x>=5) and (x<=10)
 	puts "Great you entered a proper value!"
else
 	puts "You have entered an improper value"
end
3.2b
puts "Enter an integer between five and ten"
x=gets.to_i
case
 	when (x<5) then puts "x is smaller then the interval [5,10]"
  	when ((x>=5) and (x<=10)) then puts "x is within the interval [5,10]"
  	else puts "x is greater then the interval [5,10]"
end
3.3
puts "Enter an integer"
x=gets.to_i
if (x==0) then puts "The value of the unit pulse function at this input is 1"
else puts "The value of the unit pulse function at this input is 0"
end
3.4
puts "plese enter three integers"
x=gets.to_i
y=gets.to_i
z=gets.to_i
if x>0 then 
  	if y>0 then
   		if z>0 then
     			puts "three of the integers are greater then 0"
    		else
      			puts "two of the integers are greater then 0"
    		end
  	else
    		if z>0 then
     			puts "two of the integers are greater then 0"  
    		else
      			puts "one of the integers is greater then 0"  
    		end
  	end
	else 
  	if y>0 then
    		if z>0 then
      			puts "two of the integers are greater then 0"
		else
    			puts "one of the integers is greater then 0"  
    		end
  	else
    		if z>0 then
      			puts "one of the integers is greater then 0"  
    		else
      			puts "none of the integers are greater then 0"  
    		end
  	end
end
3.5
puts "plese enter three integers"
x=gets.to_i
y=gets.to_i
z=gets.to_i
case
  	when (x>0 and y>0 and z>0) then puts "three of the integers are greater then 0"
  	when (x>0 and y>0 and z<0) then puts "two of the integers are greater then 0"
  	when (x>0 and y<0 and z>0) then puts "two of the integers are greater then 0"
  	when (x<0 and y>0 and z>0) then puts "two of the integers are greater then 0"
  	when (x>0 and y<0 and z<0) then puts "one of the integers is greater then 0"
  	when (x<0 and y>0 and z<0) then puts "one of the integers is greater then 0"
  	when (x<0 and y<0 and z>0) then puts "one of the integers is greater then 0"
  	when (x<0 and y<0 and z<0) then puts "none of the integers are greater then 0"
end
3.6
puts "please enter the x and y coordinates (integers between -4 and 4 inclusive)"
x=gets.to_i
y=gets.to_i
case
  	when (((x<-2) or ((x<2) and (y<0)))and((x<5) and (y<5) and (x>-5) and (y>-5))) 
		then puts "the altitude is 0m"
  	when (((((x<2) and (y>-1)) or ((x>1)and(y<0))) and((x<5) and (y<5) and (x>-5) 
		and (y>-5)))) then puts "the altitude is 1m"
 	when ((((x>1)and(y>-1))and((x<5) and (y<5) and (x>-5) and (y>-5)))) then puts
	 	"the altitude is 2m"
 	else puts "the input coordinates were invalid"
end
3.7
puts "please enter in a four digit number one digit at a time starting from the 
	thousands"
d1, d2, d3, d4 = gets.to_i, gets.to_i, gets.to_i, gets.to_i

puts "please enter in an one digit number to divide the above by"
div=gets.to_i

case
 	 when (div==1) then puts "1 divides all integers"
 	 when (div==2) then 
   		if d4 % 2 == 0
   			puts "2 divides the number" 
    		else 
      			puts "2 does not divide the number" 
    		end
  	when (div==3) then
		if ((d1+d2+d3+d4) % 3) == 0
      			puts "3 divides the number" 
    		else 
      			puts "3 does not divide the number"
    		end
  	when (div==4) then 
    		if ((10*d3+d4) % 4) == 0
      			puts "4 divides the number" 
    		else 
      			puts "4 does not divide the number"
    		end
  	when (div==5) then     
		if (d4==5) or (d4==0)
      			puts "5 divides the number" 
    		else 
      			puts "5 does not divide the number"
  		end
  	when (div==6) then 
    		if ((d4 % 2) == 0) and ((d1+d2+d3+d4) % 3) == 0
      			puts "6 divides the number" 
    		else 
      			puts "6 does not divide the number"
    		end
  	when (div==7) then 
    		if ((d1*1000 + d2*100 + d3*10) - (d4 * 2)) % 7 == 0
      			puts "7 divides the number" 
    		else 
      			puts "7 does not divide the number"
    		end
   	when (div==8) then 
     		if (d3+d2+d1) % 8 == 0
       			puts "8 divides the number" 
     		else 
       			puts "8 does not divide the number"
     		end
   	when (div==9) then 
     		if (d1+d2+d3+d4) % 9 == 0
       			puts "9 divides the number" 
     		else 
       			puts "9 does not divide the number"
   		end
   	else puts "the dividend entered was not valid"
end
3.8
3.9
puts "Please enter a year"
year = gets.to_i
if year % 4 == 0 && year % 100 != 0 
	puts "This is a leap year"
else if  year % 400 == 0 
	puts "This is a leap year"
else
	puts "This is not a leap year"
end
3.10
time = Time.now
t = time.strftime("%H").to_i
case
	when ((t<=11)&&(t>=4)) then puts "Good morning world"
	when ((t>=12)&&(t<=17)) then puts "Good afternoon world"
	when ((t>=18)&&(t<=21)) then puts "Good evening world"
	when ((t>=22)&&(t<=3)) then puts "Good night world"
end
3.11a
puts "enter the first integer ==>"
x=gets
x = x.to_i
puts "enter the second integer ==>"
y = gets
y = y.to_i
if(xy)
	puts"Most recent integer is smaller."
else
	puts"The last two integers are equal."
end

x = y
y = gets
y = y.to_i
if(xy)
	puts"Most recent integer is smaller."
else
	puts"The last two integers are equal."
end
3.11b
puts "enter the first integer ==>"
x=gets
x = x.to_i
puts "enter the second integer ==>"
y = gets
y = y.to_i
case
	when (x < y) then puts"Most recent integer is larger."
	when (x > y) then puts"Most recent integer is smaller."
	else
		puts"Last two integers are equal."
end
	
x = y
y = gets
y = y.to_i
case
	when (x < y) then puts"Most recent integer is larger."
	when (x > y) then puts"Most recent integer is smaller."
	else
		puts"Last two integers are equal."
end
3.12a
puts "enter a value ==>"
x = gets
x = x.to_i
largest = x
smallest = x
puts "enter a value ==>"
x = gets
x = x.to_i
if(x > largest)
	largest = x
else if(x < smallest)
	smallest = x
end
x = gets
x = x.to_i
if(x > largest)
	largest = x
else if(x < smallest)
	smallest = x
end
puts "The largest input was: " + largest
puts "The smallest input was: " + smallest
3.12b
3.13 The code outputs a sorted list of numbers.
3.14
number = gets.to_i
if (number % 2) == 0
	puts "It's even!"
else
  	puts "It's odd."
end
3.16a
input = gets.chomp
puts input if input = 'a'
3.16b
input = gets.chomp
if input = 'awesome'
  	puts input
else
  	puts 'Cheese'
end
3.16c
input = gets.chomp
puts input if input = '42'
3.17
puts "Please enter how many slices of pizza you need"
p=gets.to_i
if (p % 8 == 0) then
  	puts "You will need " + (p/8).to_s +" pies of pizza."
else 
  	puts "You will need " + (((p-(p%8))/8 + 1)).to_s +" pies of pizza."
end
3.18
puts "Enter the first x coordinate==>"
xOne=gets
puts "Enter the first y coordinate==>"
yOne=gets
puts "Enter the second x coordinate==>"
xTwo=gets
puts "Enter the second y coordinate==>"
xTwo=gets
if(xOne == xTwo)
        puts "the line is vertical"
elseif( yOne == yTwo)
        puts "The line is horizontal"
else
	slope = (yOne - yTwo) / (xOne - xTwo)
        puts "The slope of the line is " + slope
end
3.19
puts "enter the first number==>"
x = gets.to_i
puts "enter the second number==>"
y = gets.to_i
if( x % y == 0)
        puts x + " is divisible by " + y
end
if( y % x == 0)
        puts y + " is divisible by " + x
end
3.20
puts 'Hello!  Who is this?'
input = gets.chomp
if input == 'Frank'
  	puts 'Hey Frank.  How's it going?'
elsif input == 'Mary'
  	puts 'Hi Mary.  Where have you been lately?'
elsif input == 'Susan'
  	puts 'Heya Susan, how's it hangin?'
end