CS 313 - Homework 3 - Parameter passing and C

Due: Wednesday 3/8 at 11:15am

Common problems / tips:

Problem 1 (params):

Problem 2 (bst): Problem 3 (parens):


This homework consists of three programming problems in C. Make sure you write "pure C", i.e., your C programs should compile with the options "-ansi -pedantic".

When you are done, please submit your homework via the link at the bottom of this page. Please use the file names specified (params.c, bst.c, and parens.c) so I can test them easily.

  1. Read Sethi Chapters 5.1-3, and 15.2 (on C).

  2. Consider the following program, written in Pascal syntax:
    var a : integer; 
        B : array[1..2] of integer;
    
    procedure P(x, y: integer);
    begin
       x := a + 1;
       y := a * x;
    end;
    
    begin
       a := 1;
       B[1] := 3; B[2] := 5;
       P(a, B[a]);
       writeln('call by ______: a = ', a, ', B[1] = ', B[1], ', B[2] = ', B[2]);
    end.
    
    Write a C program params.c that simulates the following four parameter passing mechanisms and prints the output of the above program for each:
    1. Call by value
    2. Call by reference
    3. Call by value-result
    4. Macro expansion
    Before writing the program, test your own understanding of the parameter passing mechanisms to predict what the output would be. Then, translate the program into C, and write 4 different functions that use or simluate the different mechanisms (with the help of pointers, additional local variables, and/or macro definitions). The output of your program should be formatted as follows, with actual numbers replacing the underscores.
    call by value:        a = _,  B[1] = _,  B[2] = _
    call by reference:    a = _,  B[1] = _,  B[2] = _
    call by value-result: a = _,  B[1] = _,  B[2] = _
    macro expansion:      a = _,  B[1] = _,  B[2] = _
    

  3. (Sethi problem 4.9) Implement a binary search tree again, this time in C. Call your program bst.c. 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 the CS lab machines in ~schar/cs313/hw3/ for more examples on how the printing should work.

  4. (Sethi problem 5.4) Starting with the function defined below, write a C program to parse strings of balanced parentheses (including round parentheses, angle brackets, and square brackets). Call your program parens.c. A sample executable "parens" is on the CS lab machines in ~schar/cs313/hw3/. Try to match its behavior.

    void parens() {
        for (;;) {
            switch (lookahead) {
            case '(': M('('); parens(); M(')'); continue;
            case '<': M('<'); parens(); M('>'); continue;
            case '[': M('['); parens(); M(']'); continue;
            default: return;
            }
        }
    }
    
    Note that this code should be taken as is, without modification. In order to make it work you'll need global variables holding the input string and the current position in the input string, and possibly also a global variable to keep track if the string is still balanced. The global variable 'lookahead' should hold the next character of the input string. The function M (for "match") should compare its parameter to lookahead. If they match, it should advance the input string and update lookahead to next character. If they don't match, your program should report that the string is not balanced.

    Call the function parens from your main program. If it returns without finding a mismatch, AND if there is no remaining input left, then it should report the string is balanced, otherwise unbalanced.

Online submission

Upload your files params.c, bst.c, and parens.c via the HW 3 submission page by the beginning of class on Wed.