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

Homework Problems

Chapter 2 Homework Problems

2.1 Floating point numbers are not perfectly accurate. For example:
	>> 1.001 - 1
	=> 0.00099999999999989
Try to represent 1.1875 and 1.1 as a binary numbers. Use negative powers of 2 to represent the fractional part. Explain why Ruby cannot do the above calculation exactly.
 
2.2 Floats can have two special values: Infinity and NaN (Not a Number). Generate both.
 
2.3You saw that Ruby does not allow addition of floats and strings. For example:
	>> 1.1 + "string"
	TypeError: String can't be coerced into Float
	        from /Users/leland/.irbrc:73:in `+'
	        from (irb):14
What type combinations does Ruby allow to be added?
 
2.4Earlier in this chapter, we saw a method call for integers called upTo that incremented an int by 1 until it reached a certain maximum value (Refer to page xx for further details). There is another method call for ints called the step function that follows the following syntax:
	anInt.step( endNum, step ) {| i | puts i} 
Where anInt, endNum, and step are all ints. This method increments anInt by the value of step (or decrements anInt if the value of step is negative) and stops when anInt is larger than or equal to endNum if step is positive or when anInt is less than or equal to endNum if step is negative.
	a)	irb(main):001:0> 1.step(10,2){|i| puts i}
	b)	irb(main):002:0> 9.step(0,-3){|i| puts i}
Write down the expected output of both parts.
 
2.5You saw ".class" used to see the type of a variable. There is another method, "superclass" that allows you to see the class above. For example:
		>> 1.class
		=> Fixnum
		>> 1.class.superclass
		=> Integer
Draw a hierarchy of numbers, marking types as mathematically distinct or implementation distinct.
 
2.6 Using IRB, initialize three variables, x y z, each to some number less than 10. Design an equation with these variables utilizing at least one multiplication, one division and one addition or subtraction element. Have the computer do it once without parenthesis, and then add parenthesis to the equation and try it again. Are the answers the same? If not, why?
 
2.7In a program, a certain variable called changeTemp is created to store the magnitude of difference in temperature between 2 different cities. The temperature values for each city are stored in separate variables named tempc1 and tempc2, respectively. As we are looking for the magnitude of temperature difference, we do not wish to have a negative value for changeTemp. A student believes the following will always give the desired value for changeTemp
		changeTemp= tempc1-tempc2
Is the student correct? Why or why not? If not, explain what you would modify to correct it.
 
2.8The “==” operator is used to compare whether two strings are equal. It returns “true” when two strings are identical and “false” when they are not.
	a)	“foo” == “foot”
	b)	“hello” == “Hello”
	c)	“face” == “FACE”
Write down the expected result for each part.
 
2.9 Earlier in the chapter, we saw the round method call for floats which rounds a float value to the nearest integer. Another method for floats called floor (based on the floor function in math) returns the largest int that is less than or equal to the float value.
	a)	irb(main):001:0>x= -4.0.floor
	b)	irb(main):002:0> x=3.98.floor
	c)	irb(main):003:0> x=-1.9.floor
Write down the expected result of x for each part.
 
2.10 Earlier in the chapter, we saw the round method call for floats which rounds a float value to the nearest integer. Another method for floats called ceil (based on the ceiling function in math) returns the smallest int that is larger than or equal to the float value.
	a)	irb(main):001:0> x= 2.0.ceil
	b)	irb(main):002:0> x=3.47.ceil
	c)	irb(main):003:0> x=-1.9.ceil
Write down the expected result of x for each part.
 
2.11
	
	irb(main):001:0> aString = “hey ”*3 + “yo “*2
What is the expected value of aString?
 
2.12
	irb(main):001:0> x= 3
	irb(main):002:0> x= x+4.5
	irb(main):003:0> x= x-2
Write down the expected value of x. What data type is this value?
 
2.13
	
	a) irb(main):001:0> x= 9
       	   irb(main):002:0> x= x/2


	b) irb(main):003:0> x= 9
	   irb(main):004:0> x= x/2.5
For both parts, Write down the expected value of x.
 
2.14
	irb(main):001:0> x= 5
	irb(main):002:0> y= 1.42*10^3
	irb(main):003:0> z= x+y
What is the expected result of z?
 
2.15 Suppose a program is written that finds the average temperature for a given year. A user of the program is prompted to enter the average temperature values for each season of the year: Winter, Spring, Summer, and Fall. The program stores these values as floats in variables tempW, tempS, temps, and tempF, respectively. The final result is stored in the variable avgTemp. The program calculates the average temperature with the following line:
	avgTemp=tempW+tempS+tempS+tempF/4
However, when the program runs, the value of avgTemp is always incorrect. Briefly describe what is wrong with this line and what changes you would make to correct this error.
 
2.16
	irb(main):001:0> x= "hello"
	irb(main):002:0> y= "goodbye"
	irb(main):003:0> z= x+y
What is the expected result of z?
 
2.17
	irb(main):001:0> x= "five"
	irb(main):002:0> y= 5
	irb(main):003:0> z=x+y
What result do we expect for z?
 
2.18 Ruby also has Rational numbers. Create a rational number like this:
	>> puts Rational(3,10) 
	3/10
	=> nil
Do the above calculation using Rationals. Does it compute exactly? Explain why you think it does or does not.
 
2.19 Some languages, such as Java, are more strict than Ruby do not allow mixing of types, as in "1 + 1.1". Other languages, like Perl, allow types to be mixed even more freely than Ruby. For example, "1 + "2."" in Perl returns 3.5. What do you think are the disadvantages and advantages of each approach?
 
2.20 A new programmer has written down the following lines of code to test whether two strings are equal.
	
	1  #! /usr/bin/ruby
	2  a = "silver"
	3  b = "gold"
	4  if (a = b)   #if the two strings are equal, go to 
              		#line 5. Otherwise go to line 6
	5  puts "Strings a and b are equal"
	6  end  #end of program	  
The code compiles but whenever this code is run, it always prints out “Strings a and b are equal” even when this is not the case. What is the problem with the current code? What can be done to correct this?
 
2.21 What is the difference between logic and syntax errors? Give an example of each.
 
2.22 What needs to be changed in this code to produce the output
	
	=> "hellohellohellohello" ?

	irb(main):001:0> hello = 2
	irb(main):002:0> message = hello*"hello"
	irb(main):003:0> message+hello*"hello"
2.23 Set y in this code so that it outputs
	2
	2.0

	irb(main):001:0> x = 2
	irb(main):002:0> y = ?
	irb(main):003:0> x + y
	 => ?
2.24 Fix the following code so that no errors come up.
	irb(main):001:0> x = 5
	irb(main):002:0> y = 3.0
	irb(main):003:0> z = 1,000
	irb(main):004:0> k = x + y * z
2.25 What is the expected result?
	irb(main):001:0> y =  -4.499999999999999
	=> -4.5
	irb(main):002:0> z = y
	=> -4.5
	irb(main):003:0> z.abs.round
	=>?
2.26 What are the expected results of lines 4 and 5?
	irb(main):001:0> a = 5
	irb(main):002:0> b = 2.5
	irb(main):003:0> c = 1.25
	irb(main):004:0> (a+b+c).round
	=> ?
	irb(main):005:0> a+b+c.round
	=> ?
2.27 The .length method returns how many characters are in a string. For example,
	irb(main):001:0> string = "hello"
	=> "hello"
	irb(main):002:0> string.length
	=> 5
Since the word “hello” has five letters, the .length method returns the integer 5. What happens when you try to run the following commands?
	a)irb(main):003:0> string.abs

	b)irb(main):003:0> x = 5
	  => 5
	  irb(main):004:0> x.length

	c)irb(main):003:0> string.abs.length

	d)irb(main):003:0> string.length.abs