CSE 15: Introduction to Computer Science
Lab work and Assignment #2 (Chapter 2)

After going through the multimedia for Problem solving before programming, you will start solving problems in Knobby's World. In lab, do the following exercises from the book, given again below. Write down notes answering the questions. Show a TA your written answers and demonstrate your solution to exercise 2.70, before you leave.

After completing your lab assignment above, you can go on to the homework exercises below (following the lab exercises).
Or work on your homework during the week. Hand in your homework via Blackboard, here.

Note: Knobby's World has been installed on the LAN. From Windows Start > Run knobby.
The CD for The Universal Computer also installs it on your own PC, from
Windows Start > Programs > Universal Computer > Knobby, or from the multimedia, Tools > Knobby.

Knobby's World laboratory exercises (from the textbook, The Universal Computer):

Exercise 2.58: Start up Knobby's World. By default, it should load the situation file mountain.sit. By using move and left, navigate Knobby to the other side of the mountain.

Exercise 2.59: How can you instruct Knobby to take three steps backward? When or why might you want Knobby to do this?

Exercise 2.60: How can you instruct Knobby to take three steps forward, pick up a character, and return to his original position?

Exercise 2.61: What happens if Knobby attempts to perform a write at the beginning of a task? Why? How can a Knobby program avoid this behavior?

Exercise 2.62: What happens when Knobby performs a read from a corner that appears not to have a character on it? How can you use this behavior to erase other characters?

Exercise 2.63: Now for some action! Press the reset button, so that Knobby is back where he started, ready to start a task. Knobby's goal is to pick up a copy of the flag, F', climb up to the top of the mountain (where it should find '^'), plant the new flag there, then climb down to the other side of the mountain. What problem solving strategy might you well use to solve this problem?

Exercise 2.64: There is no single command to copy a character from a corner one at 1st Street and 2nd Avenue, the other at 1st Street and 3rd Avenue.) How can he do that?

Exercise 2.66: Without actually doing it, what do you suppose would happen if you sent Knobby to the flag, had him read it, and then copied b2 into b1? Why? Now perform the experiment to see if your analysis was correct. (Don't forget to reset the world first.)

Exercise 2.67: Reset again. This time plant a copy of the flag on top of the mountain, then come back down and erase the (original) flag you've left at the bottom. How can Knobby "empty" board b1 in order to erase the flag? Hint: see exercise 2.61.

Exercise 2.69: Using macros, define instructions right and reverse. (Enter d to define a macro-instruction; enter e to execute it.)  Then send Knobby through the task of planting a flag on a mountain, using your new instructions. (Remember, you perform a left or a move as before; you perform a right or a reverse by pressing e, then telling Knobby which new instruction to execute.)

Exercise 2.70: Now that you have defined macro-instructions right and reverse, define a single new instruction for the complete flag-moving task. (Test it to make sure that it works correctly.) In what sort of circumstances might this new instruction be part of a solution to a still larger problem?

Exercise 2.76: Situation maze.sit shows Knobby sitting at the entrance of a maze. Navigate him through the maze by hand. Introspect a bit: what problem solving strategy are you using?

(Show a lab instructor that you have done and understand the above laboratory exercises, before going on to do the homework.)

Homework exercises, in the textbook, The Universal Computer:

a) Do the following written exercises: 2.1, 2.5, 2.6, 2.7 (describe your solution when you hand it in), 2.19, 2.20, 2.36, 2.37, 2.47and 11.53 from the C++ book, reproduced below. Use a text editor or word processor to enter your answers into one file.

Exercise 11.53 (social/ethical): Here is an excerpt from our department’s Homework Regulations:

The activity of learning course material involves the effort of each student. Without this effort it is usually not possible to absorb the material to a desirable level. In addition to student/instructor interaction, we also encourage active student/student interaction. Unfortunately, in a very small number of cases this is abused when a student does not made the effort to add a contribution to the cooperative effort, but simply uses the work of others to obtain a grade....

In the chapter on problem-solving, we mentioned that some programmers use cut-and-paste as a method of problem solving. When, if ever, would cut-and-paste from another student’s program be ethically appropriate? Why or why not? Would cut-and-paste from programs supplied with this book be appropriate? Why or why not?

Write Knobby's World programs for exercises 2.75 (the morning newspaper problem) and 2.82 (only instead of 3+4, show how Knobby solves 2+4). Instructions for handing in these programs are below.

Extra credit: 2.78 (Knobby's World will let you let create and edit your own situation files--see the World menu) and/or 3.30.

Due: Tuesday, September 10, anytime before midnight.

Hand in: Enter all your answers into a single file, using an text editor such as notepad or Word. (You can copy and paste the content of your Knobby's World programs into this one file; just use comments to demarcate each program file within your one big file.)  Submit your homework as an attachment here, in the Blackboard Assignment area.

Hints:

Exercise 2.75 assumes that you use the initial world situation in file morning.sit. (Under the World menu option, select Load, then double click on file morning.sit.)  To create your program, you could copy one of the other Knobby's World programs (ending with extension .kno) to another name, e.g., in DOS (or you may use Windows Explorer to copy the file):

      C:\umach>copy square.kno morning.kno

Then load it into Knobby's World (under the Program menu, select Load, then double click on morning.kno), then edit it (under the Program menu, select Edit, then mouse click on the editor window on the right). Use decomposition to break this problem down into smaller pieces that can be defined as instructions, which get called by the top level main instruction or other instructions. Try creating a few utility instructions, such as turning right, or moving a particular number of squares. Comment your code!

Exercise 2.82 only requires that you write a program that adds 2+4, not any two numbers. (Any two numbers is covered in an exercise in chapter 3.) Here is adder23.sit, which sets up the 2+3 problem (two X's, space, then four X's) and adder24.sit, which sets up the 2+4 problem (two X's, space, then four X's).    (You'll also find these situation files on the LAN and CD-ROM.)

Regarding Knobby's World programming, a student asks: > How can you break an infinite loop? My program was stuck in > an infinite loop and the only way I could stop it was to quit > the program. Thanks.
Apparently Knobby's World doesn't respond to ctrl-c or ctrl-break.
Here's what works: press ctrl-alt-del (hold down the Ctrl key, then hold down the Alt key, then press the Del key).
Windows will bring up a list of tasks.
Click on Knobby's World, then click on end task.
This may not work right away if you're in a "tight" (fast) loop, so you may have to wait or do this more than once.
BTW, this is how you can kill any task that's not behaving well, under Windows.

Prof. Blank