Rice University - Instructor: Dung Nguyen PRINT NAME: ______

Total Page:16

File Type:pdf, Size:1020Kb

Rice University - Instructor: Dung Nguyen PRINT NAME: ______

Comp200 – Elements of Computer Science EXAM #2 November 16, 2005 Rice University - Instructor: Dung Nguyen PRINT NAME: ______

Instructions 1. This is an open-notes, open-book (except “Turing”) exam. 2. You are free to use any materials that were given to you in the lectures. 3. You are free to use DrJava and EXCEL and any on-line help materials that these software provide. 4. When you are done, upload all of your work to the “Exam2” assignment on the course WEBCT.

Please write and sign the Rice Honor Pledge here:

1 2 3 4 Total

1. In Java, the method Math.ceil(double x) that returns the smallest integer that is greater or equal to the input argument x(ceil stands for ceiling). Use the Interactions windows of DrJava to answer the following.  Math.ceil(.5) = 1  Math.ceil(1.35) = 2  Math.ceil(2) = 2

4/30/2018 1 of 5 Comp200 – Elements of Computer Science EXAM #2 November 16, 2005 Rice University - Instructor: Dung Nguyen PRINT NAME: ______

2. The following is the postage rate for first class mail in the USA.  $0.37 for the first ounce.

 $0.23 for each additional ounce. Write a Java program that produces the postage cost in US $ given the weight in ounces of the mail item. Name the class Postage and the method cost. Hint: Use the method Math.ceil(double x). Write the code that you would write in the Interactions pane of DrJava to compute the postage cost of the following.  a .75 oz letter.  a 1 oz letter.  a 1.25 oz letter.  a 2 oz letter.  a 2.5 oz letter.  a 3 oz letter.  a 3.75 oz letter. class Postage { double cost (double weight) { return (37 + Math.ceil(weight - 1) * 23) / 100; } }

> Postage p = new Postage(); > p.cost(.75) 0.37 > p.cost(1) 0.37 > p.cost(1.25) 0.6 > p.cost(2) 0.6 > p.cost(2.5) 0.83 > p.cost(3) 0.83 > p.cost(3.75) 1.06 >

4/30/2018 2 of 5 Comp200 – Elements of Computer Science EXAM #2 November 16, 2005 Rice University - Instructor: Dung Nguyen PRINT NAME: ______

3. Consider a Turing machine with  an alphabet consisting of ‘1’, ‘+’ and ‘b’ where b stands for the blank character,  three states q0, q1 and q2 where q0 is the start state and q2 is the final (stop) state, and  the following state transition table

Current State Current Symbol Next State Next Symbol Direction q0 1 q0 1 R q0 + q0 + R q0 b q1 b L q1 1 q2 b L q2 1 q2 1 L q2 + q3 1 Stop

a/ Draw the equivalent state transition diagram for the above.

Turing State Transition Diagram

Start Move right until seeing q0 R a blank. When seeing a blank move left and b change state b L q1 Erase 1, move left and change 1 state. b L Move left until seeing q2 L +. Replace + with 1 and + stop. 1 Stop q3 Halt

4/30/2018 3 of 5 Comp200 – Elements of Computer Science EXAM #2 November 16, 2005 Rice University - Instructor: Dung Nguyen PRINT NAME: ______b/ Assume the input/output tape looks like: 1 1 + 1 1 and the read/write head is positioned at the leftmost 1 of the input/output tape before starting the above Turing machine. Display a snapshot of the input/output tape and the read/write head of each step in the complete computation of the Turing machine.

State Input/Ouput Tape q0 1 1 + 1 1 ^

q0 1 1 + 1 1 ^

q0 1 1 + 1 1 ^

q0 1 1 + 1 1 ^

q0 1 1 + 1 1 ^

q0 1 1 + 1 1 ^

q1 1 1 + 1 1 ^

q2 1 1 + 1 ^

q2 1 1 + 1 ^

q2 1 1 + 1 ^

q3 1 1 1 1 ^ Halt

4/30/2018 4 of 5 Comp200 – Elements of Computer Science EXAM #2 November 16, 2005 Rice University - Instructor: Dung Nguyen PRINT NAME: ______

c/ What is this Turing machine computing?

It performs the addition of two unary numbers.

d/ Describe in a few sentences, the algorithm this Turing machine is programmed to compute.

 Move right until seeing the first blank.  Move left and erase the leftmost 1.  Move left until seeing +.  Replace + with 1 and stop.

e/ Made appropriate changes to the state transition table to make the read/write head go all the way to the right most end when the Turing machine finishes its computation. Add new states and label them q4, q5, etc if necessary.

Instead of making q3 a final state add state q4 and make q4 final and change the transition table to the following.

Current State Current Symbol Next State Next Symbol Direction q0 1 q0 1 R q0 + q0 + R q0 b q1 b L q1 1 q2 b L q2 1 q2 1 L q2 + q3 1 R q3 1 q3 1 R q3 b q4 b Stop

4/30/2018 5 of 5

Recommended publications