Place these files in a lab4 folder. Before leaving lab, zip up the lab4 folder and hand the zip file in.
def count(key, list) count = 0 i = 1 while i < list.length do if list[i] == key then count = count + 1; end i = i + 1 end return count end
Each of the following programs have a bug in it. For each program, first, identify an input for which it gives an incorrect output. In answers.txt, record:
In addition, correct each function and save a copy of that function as indicated.
The distance d in meters that an object dropped in a vacuum near the surface of the earth will fall in t seconds can be described by formula, \(d = \frac{1}{2}9.8t^2.\)
Identify and correct the error in the Ruby function drop_distance(t) listed below. It should take a single parameter named t and is intended to return the corresponding value of d according to the formula above.
def drop_distance(t) return 1/2 * 9.8 * t**2 end
Save the corrected version as drop_distance.rb
The following function is meant to take the absolute value of a number.
def abs_val(n) if n < 0 do return -n else return n end end
Save the corrected version of the function as abs_val.rb.
Identify and correct the error in the following ruby function, which is meant to print out a table of the first n factorials:
def n_factorials(n) for i in 0..n do x = 1 for j in 1..i do x = x * j print i, "! \tis\t", x, "\n" end end
Save the corrected version as n_factorials.rb.
After \(y\) years, the future value \(F\) of a bank account with an initial deposit \(P\) that is subsequently increased at i % each year and compounded yearly is given by the formula:
$$F = P(1 + i/100)^y,$$Identify and correct the error in the Ruby function future_value listed below.
def future_val(present_value, interest_percentage, years) return present_value * (1 + interest_percentage / 100)**years end
Store the corrected version of the function in future_val.rb.
The following function is intended to print out a table of the cubes of the numbers 0 through n.
def cubes(n) i = 0 while i <= n do i = i + 1 end print i, "\t", i**3, "\n" end
Save the corrected version of this function as cubes.rb
Suppose you need a function that will print out an empty square made of "*" characters of a given size. Such as this one:
irb(main):003:0> make_square(8) ******** * * * * * * * * * * * * ******** => nil
Identify and correct the error in the following Ruby function:
def make_square(n) for i in 1..n do print "*" end print "\n" for i in 1..n do print "*" for j in 1..n do print " " end print "*\n" end for i in 1..n do print "*" end end
Hint: you may need to make corrections at more than one location in the body of make_square.
Store the corrected version of the function in make_square.rb
The arithmetic mean of a list of numbers is computed by finding the sum of those numbers and dividing that sum by the number of items in the list.
Identify and correct the error in the following Ruby function:
def arithmetic_mean(list) list.each{|x| sum = sum + x } return 1.0 * sum / list.length end
Put the corrected function in arithmetic_mean.rb.
The following function is intended to return the value of that would result from squaring each number in the array, list, and adding up all of those squares to obtain a result.
def sum_prod(list) i = 0 x = 0 while i < list.length do x = list[i] * list[i] x = x + x end return x end
Save the corrected version of this function as sum_prod.rb.