Confidential Teaching Materials
Homework 3 Criteria, Page 1 of 10
Homework 3 Grading Criteria
Note: To receive any credit whatsoever, your answers must be legible and readily readable in
the judgment of the grader. Add brief explanatory comments as necessary to make
sure your answers are clear and unambiguous to the grader.
[p1] 15 points
Explain the three parameter types, and specify when each should be used.
[c1] 15 points
Sample Answers:
[p2] 12 points
What is the output of the following algorithm? Include a separate line for each print statement.
algorithm Tracer
function Confusion returnsa num (x, y isoftype in num)
if (x > y + 3) then
if (x > 5) then
Confusion returns 8
else
Confusion returns 4
endif
else
Confusion returns 3
endif
endfunction
procedure Ambiguity (a isoftype in num,
b isoftype in/out num,
c isoftype out num)
c <- 2 * (a + b)
a <- a + 1
b <- c – 1
print (a, b, c)
endprocedure
// main algorithm starts here
Confidential Teaching Materials
Homework 3 Criteria, Page 2 of 10
value1, value2, value3 isoftype num
value1 <- 1
value2 <- 2
value3 <- 3
Ambiguity (value1, value2, value3)
print (value1, value2, value3)
value3 <- Confusion (value1,value2)
Ambiguity (value2, value3, value1)
print (value2, value1)
value1 <- Confusion (value3,value2)
print (value1)
endalgorithm
[c2] 12 points
[p3] 12 points
Find and correct any errors in the code segment below. You may assume all documentation to be
correct. Note: it may be the case that there are no errors.
function CalcMinNum returnsa num (num1, num2, num3 isoftype in/out num
result isoftype out num)
// this function will receive three numbers and return the smallest
// number
if (num1 < num2) AND (num1 < num3) then
result <- num1
elseif (num2 < num3) AND (num2 < num1) then
result <- num2
else
result <- num3
endif
CalcMinNumber returns Result
endfunction
[c3] 12 points
Confidential Teaching Materials
ANSWER:
[p4] 15 points
Write a recursive module that, given a number as a parameter, will return the number of digits in
the number. For example:
10 has 2 digits
0 has 1 digit
123 has 3 digits
HINT – the built-in “integer division” operator DIV will come in handy when writing this module.
[c4] 15 points
GRADING:
-1 for not making the module a function (fun)
-1 for invalid parameter (npo)
-2 for not returning the right terminating value (ret)
-4 for wrong recursive call (log)
Confidential Teaching Materials
Homework 3 Criteria, Page 4 of 10
[p5] 15 points
The greatest common denominator (GCD) of two numbers is the largest integer that divides both
numbers evenly. For example, the GCD of 15 and 5 (written as gcd (15, 5)) is 5 because 5 is the
largest number that divides both 15 and 5. Some more examples:
gcd (24, 12) = 12
gcd (13, 2) = 1
gcd (110, 85) = 5
Finding the GCD of two small numbers can often be done by inspection, but large numbers are a
bit more difficult to deal with. One way of finding the GCD of any two numbers is called the
Euclidian method. With the Euclidian method, you set up three columns, labeled X, Y, and
Remainder. In “X”, the first number is placed; in “Y”, the second number. The remainder that
occurs when X is divided by Y is placed in the “Remainder” column. If the remainder is 0, then
whatever is in column Y is the greatest common denominator. If the remainder is not 0, a new row
in the table is created. The previous row’s Y value is placed in the X column, and the previous
row’s remainder goes in the Y column. The remainder is then computed again, and is placed in the
“Remainder” column. This process repeats until 0 is the remainder.
Consider this example of computing the GCD of 110 and 85
X Y Remainder
110 85 25
85 25 10
25 10 5
10 5 0
This process is rather tedious, and can be greatly aided by a computer. Your job is to write a
recursive module that is passed two numbers as parameters and returns their greatest common
denominator.
[c5] 1 points
Sample Solution:
Confidential Teaching Materials
GRADING:
-1 for not making the module a function (fun)
-1 for invalid parameters (npo)
-0.5 for not making the parameters nums (num)
-3 for not returning the right terminating value (ret)
-4 for wrong recursive call (log)
[p6] 10 points
Write a recursive module that will read in characters from the user until s/he enters a ’q’. When a
’q’ is encountered, the module should print out all of the characters that the user had entered in
reverse order. For example, if the user entered the following characters: a, d, f, t, e, q, then qetfda
would be printed.
[c6] 10 points
GRADING:
-1 for not making it a procedure (pro)
Confidential Teaching Materials
Homework 3 Criteria, Page 6 of 10
-2 for not handling termination condition (log)
-3 for not handling the recursive condition (log)
[p7] 25 points
A common task in computer programming is to format output so it is easy for a person to read.
For example, it is much easier to read 3,495,394,392 than it is 3495394392. However, when a
computer stores a number, it does not store any formatting information. This is the job of the
programmer: Write a module that takes in a number and prints the “comma-ized” version of the
number to the screen. Commas should appear every three digits FROM THE RIGHT.
To help you complete this task, you can assume that the following two modules have been written
for you:
Get_First_Digit – returns the first digit of a number.
Get_Rest_Of_Digits – returns all of the digits after the first. If the number is only one digit, then
it is returned.
You may also use any of the modules that you have written on previously on this homework
without re-copying them. Note: You need not handle cases where the number contains zeros.
[c7] 25 points
Sample Solution:
Confidential Teaching Materials
Homework 3 Criteria, Page 7 of 10
GRADING:
-1 for not making it a procedure (pro)
-1 for not having the right parameter (npo)
-0.5 for not making it a num (nup)
-3 for not terminating properly (wrt)
-9 for not handling the “print a comma” case (log)
-2 for not printing first digit (wro)
-3 for not making recursive call
-7 for not handling the “normal” case (log)
-2 for not printing first digit (wro)
Premise for Problems 8 through 11:
A cellular service company has hired you to keep track of information about their customers. Each
Confidential Teaching Materials
Homework 3 Criteria, Page 8 of 10
[p8] 7 points
[c8] 7 points
Sample Solution:
[p9] 7 points
Declare the necessary record data structure to hold information about one customer.
[c9] 7 points
Sample Solution:
GRADING:
-2 for not having “Call_rec definesa record” or equiv. (rec)
[p10] 20 points
Confidential Teaching Materials
Write a module that fills in a single customer record. The module should prompt the user to enter
the cusomer’s name, address, phone number, and all of the information about both of their cell
phones. Information entered should be sent back to the calling module via a parameter.
HINT: It is essential that you use good procedural abstraction on this problem to get full credit.
[c10] 20 points
procedure Get_Phone_Info (cell_phone isoftype out Cell_Rec)
print (“Enter cell phone number: “)
GRADING:
For each procedure:
Confidential Teaching Materials
Homework 3 Criteria, Page 10 of 10
-1 for not making it the proper record (rec)
[p11] 20 points
Write a module that prints out all of the information about a given customer, incuding his/her
name, address, home phone, and information about both cell phones.
[c11] 20 points
Sample Solution:
procedure Print_Phone_Info (cell_phone isoftype in Cell_Rec)
print (“Cell phone number:”, cell_phone.phone_num)
print (“Minute limit: “, cell_phone.minute_lim)
GRADING:
-1 for not making it a procedure (pro)
-0.5 for not making it an in (oup)