next up previous
Up: aps105F Home

APS105F:  Computer Fundamentals
Lab Quiz, October 30, 1997

Instructions

  1. Create two dummy text files called ``part1'' and ``part2''. The files can have anything or nothing in them for this test.

    Test out the two submission programs, ``SUBMITPART1'' and `` SUBMITPART2'', and make sure that they work. Ask for assistance if you have difficulty.

    You should submit versions of each program as you go along. Only your most recent submission will be kept. Submissions will be disabled when the end of the test is announced. Therefore, make sure that the submit programs work for you NOW!! and submit your programs well in advance of the end of the test.

  2. Write a recursive function that returns the value of the following function:
    displaymath129

What to do

Write a program that constantly prompts for values for x and y. The program should output the value of F for the function above. When the input is less than zero, then the program terminates.

Save your program in a file called ``part1'' and submit it using ``SUBMITPART1''.

If you are using C, you will have to call your program part1.c when you are compiling it. When you are done, you will have to rename it to part1 for submission.

Assumptions

Assume that only legal inputs are entered.

In the file ``/usr/copy/aps105/MAZEin'' you will find an N by N matrix of dots and P's. A smaller 6 by 6 sample is shown below. Note that the first line of the file gives the value of N.

6
.  .  .  .  .  .
.  .  .  P  P  .
P  P  .  P  .  .
.  P  .  P  .  .
.  P  P  P  P  .
.  .  .  .  P  .

What to do

  1. Copy the MAZEin file to your own directory so that your program can read it in the same directory that it is running in. Leave the name of the file as MAZEin.
  2. Write a procedure, called MAZEread, to read the MAZEin file and store each element into a two-dimensional MAZE array of characters called AMAZE. Each character in the file is separated by two spaces and you do not have to store the spaces. A better solution will allow dynamic sizing of the array, where the array indices would run from 1 to N. For example, the P in the left column of the sample, would be stored in the element MAZE(3,1).

    The MAZEread procedure should include as parameters the array to be filled and the name of the file to read or the file number for the file, depending on your solution.

  3. Write a procedure that takes a MAZE array like AMAZE as an argument and prints it on the screen in a format similar to the file.
  4. Write a procedure to solve the maze using the right-hand rule that states that if you enter the maze with your right hand on the right wall and keep it on the wall as you walk, you will exit the maze at the location that you started, or you will find another exit.

    As you go through the maze, you should put an `x' in each element that you traverse. When you exit, you should print out the AMAZE array showing the path through the maze, which should now be marked by the x's.

    Your procedure will have to find the entrance, which will be on the left side of the maze.

  5. Your complete program should read in the MAZEin file, solve it, and then print the resulting MAZE file showing the solution.

    An example output of your program for the sample maze is shown below. Note that the coordinates of the entrance and exit must also be printed.

    The entrance is at coordinate (3,1).
    
    .  .  .  .  .  .
    .  .  .  P  P  .
    x  x  .  P  .  .
    .  x  .  P  .  .
    .  x  x  x  x  .
    .  .  .  .  x  .
    
    The exit is at coordinate (6,5).

Save your program in a file called ``part2'' and submit it using ``SUBMITPART2''.

If you are using C, you will have to call your program part2.c when you are compiling it. When you are done, you will have to rename it to part1 for submission.

Assumptions

The input file is in the correct format, with an entrance on the west side. We will test your program with a different maze.

Restrictions

Do not use any fancy formating commands in your output, such as the Turing locate function.

  1. Give the complexities of the following functions using O notation and indicate which is the slowest and which is the fastest algorithm.
    displaymath135

  2. What are the complexities of the algorithms with the following pseudo code structures:

      for i : 1 .. N
    
          for j : 1 .. i
    
          end for
    
      end for

    Answer: -10pttex2html_wrap137(100,50) 

      for i : 1 .. N
    
          k := i;
          while(k > 1)
              for j : 1 .. k
    
    
              end for
              k := k/3
          end while
    
      end for

    Answer: -10pttex2html_wrap137(100,50) 

  3. What is the complexity of the function in Question 2. Use the back of this sheet for your work if needed.

    Answer: -10pttex2html_wrap137(100,50) 

Consider the following pseudo code:

var N : int initialize to 5

var A : 20 by 20 array of int

procedure t (var B : 20 by 20 array of int, N : int)

    for i = 1 to N
        declare variable A initialized to N
        loop
            B (i, A) = i * (A - 2)
            A = A - 1
            exit when A = 0
        end loop
    end for

end t

t (A,N)

for i = 1 to N
    for j = 1 to N
        put A (i, j), " " on the same line
    end for
    start a new line
end for

  1. Draw lines, as was done in class, to indicate the scope of all variables and the procedure. Use Turing-like scoping rules.
  2. Trace through several iterations of this program by hand showing how the variables are changed and explaining what the program is doing. Do this by building a table or use some other scheme that helps to clearly explain how the program works. For each array element, just show the values of the elements that are changing. Do not show the whole array each time one element changes! Indicate clearly if you use the back of the sheet.

    What is the final output?

A very interesting, but simple data structure is the binary tree. Without getting into the details of how to implement them in a program there is still a lot that can be gained by looking at pictures and understanding how things work at that level. One thing that you want to do is to traverse the tree, which means you have to visit or go to every node in the tree.

Consider the tree shown in the figure below:

 tex2html_wrap143

The arrows represent pointers to other nodes in the tree. When there is nothing to point to, then we say there is a NULL pointer as indicated by the electrical ground symbols in the figure.

One recursive algorithm to traverse the tree is shown below:

procedure traverse(root)  % root is a pointer to a tree/sub-tree
    if root = NULL then return

    traverse(left tree)   % follow the pointer to the left sub-tree
    traverse(right tree)  % follow the pointer to the right sub-tree
    print value of root   % print what is shown in the circle on the figure
end procedure

  1. What is the sequence of letters that would be output for the above tree when using this algorithm? Assume that the pointer to node A is the argument to the first call to traverse.

  2. Draw a tree that, when traversed with the above algorithm, will output the following string of characters:

    8 4 * 6 3 5 + * -


next up previous
Up: aps105F Home

Paul Chow
Thu Feb 12 12:20:44 EST 1998