CS 313 - Homework 3 - C

Due: Wednesday 3/4 in class

This homework consists of two written problems, two programming problems in C, and a brief warm-up exercise in Python. Hand in your solutions and printouts of your code. Please include sample output produced by your programs! On the first page, write your own name as well as the names of the students with whom you collaborated. Don't forget to answer question 6! Also submit your programs electronically by typing "submit313" in the directory containing your program files.

Note: make sure you write "pure C", i.e., your C programs should compile with the options "-ansi -pedantic".

  1. Read Sethi Chapters 4, 5, and 15.2 (on C).

  2. Sethi p. 198, problems 5.1 and 5.2. (For problem 5.1, of course include the output produced by your program in the 4 cases.)

  3. Sethi p. 145, problem 4.9: Implement a binary search tree again, this time in C. Please call your program bst.c - this will make testing easier. Look at last week's solutions if you had trouble. To make it more interesting, implement a function "prettyprint" in addition to the functions "member", "insert", and "print". Prettyprint should print the tree sideways the following way: For each node, print its right child on the same line, and its left child below it (you will have to keep track of the indentation level to do this). Here is an example: Given the trees
           5                                8
         /   \                            /   \
        /     \                         /       \
       3       10                     4          12
        \     /  \                  /  \        /  \
         4   9    12               2    6     10    14
            /     /               /\    /\    /\    /\
           8     11              1  3  5  7  9 11 13 15
    
    your program should print
      5 - 10 - 12                   8 - 12 - 14 - 15
                  ` 11	                    ` 13
             `  9		               ` 10 - 11
                  `  8	                    `  9
        `  3 -  4		          `  4 -  6 -  7
    			                    `  5
    			               `  2 -  3
    			                    `  1
    
    Assume that all values have at most 2 digits, and print them right-aligned as in the examples above (use the printf format string "%2d"). Also print the dashes and backquotes as shown. Play with the sample executable program "bst" on benjerry in ~schar/cs313/hw3/ for more examples on how the printing should work.

  4. Sethi p. 199, problem 5.4: Starting with the code fragment given in Fig. 5.29, write a C program to parse strings of balanced parentheses. Please call your program parens.c - this will make testing easier. A sample executable program is on benjerry in ~schar/cs313/hw3/parens. Try to match the sample's behavior.

  5. A first taste of Python. Start by reading an online tutorial, for example, the first two sections of the official Python tutorial. Then, implement a small program that asks the user for a start value (use the 'raw_input' function as in the tutorial) and computes the Hailstone sequence from this value and its length. Sample runs:
    > python hailstone.py
    start value? 4
    4 2 1
    2 steps
    
    > python hailstone.py
    start value? 7
    7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
    16 steps
    
    > python hailstone.py
    start value? 27
    27 82 41 124 62 31 94 47 142 71 214 107 322 161 484 242 121 364 182 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
    111 steps
    

  6. How much time did it take you to complete this assignment?