Place these files in a lab5 folder. Before leaving lab, zip up the lab5 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 has 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 corrected function as indicated.
The area of a triangle is the half of the product of its height h and its base b as expressed by the formula
\[area = \frac{1}{2}hb.\]
Identify and correct the error in the Ruby function area_triangle(h,b) listed below. It should take a two parameters named h and b and is intended to return the corresponding value of area according to the formula above.
def area_triangle(h,b)
return 1/2 * h * b
end
Save the corrected version as area.rb
Identify and correct the error in the following Ruby function, which is meant to return true if m is a multiple of n , and return false otherwise.
def multipleof?(m, n)
if m % n == 0 do
return true
else
return false
end
end
Save the corrected version of the function as multiple.rb.
Identify and correct the error in the following Ruby function, which is meant to calculate the sum of the lengths of the strings in string_list.
def sum_length(string_list)
for s in string_list do
sum = sum + s.length
end
return sum
end
Save the corrected version as sum_length.rb.
Identify and correct the error in the following Ruby function, which is meant to print out a table of factorials from 0 to n:
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
end
Save the corrected version as n_factorials.rb.
The Ruby function ordered?(k, m, n) is intended to take three integers as parameters and check if k < m < n . Identify and correct the error in the Ruby function listed below.
def ordered?(k, m, n)
if k < m < n then
return true
else
return false
end
end
Store the corrected version of the function in ordered.rb.
The following function is intended to print the number of occurrences of a given key in a list by counting backwards in the list, starting from the end of the list.
def backward_count(list, key)
count = 0
len = list.length
for i in (len-1)..0 do
if list[i] == key then
count = count + 1;
end
return count
end
Save the corrected version of this function as bcount.rb
The following Ruby function attempts to calculate and return the product of the numbers in list. Identify and correct the error in this function.
def product(list)
prod = 1
i = 0
while i <= list.length do
prod = prod * list[i]
i = i + 1
end
return prod
end
Store the corrected version of the function in product.rb
Consider the following function, which uses recursion to do linear search to find out whether key appears in the array list.
def linsearch?(list, key) return false if list.length - 1 == 0 return true if list.first == key return search?(list[1..list.length-1], key) end
Put the corrected function in linsearch.rb.
The following Ruby functon is intended to compute 2n recursively, for positive integers n.
def double(n) return 2 + double(n-1) return 0 if n == 0 end
Save the corrected version of your function as double.rb.