CMPS 115

Homework Assignment 4

This assignment is an individual activity. Due in-class, May 14.

For the following two programs:
(a) draw a control flow graph
(b) list all possible unique paths through the graph
(c) for each path, give data values for the program variables that would cause the path to be executed (if a path is unreachable, indicate this instead).

The language is Pascal-like, readln reads a line of input, write and writeln write to output. Numbers in the leftmost column are program source code line numbers, and are not part of the source code itself.

Program #1

Note that this program doesn't do anything particularly useful, beyond providing a program that can be tested.

[1]    integer a, b, c, i;
[2]    begin
[3]       b := 0; i := 0;
[4]       readln(a);
[5]       readln(c);
[6]       while (i < c)
[7]          if a > 15 then
[8]               b := b + 1;
[9]          else
[10]              b := b + 5;
[11]         end if;
[12]         b := b * 2;
[13]         if a < 10 then
[14]              b := b - 1;
[15]         end if;
[16]      end while;
[17]      write ("The value of b: ");
[18]      writeln (b);
[19]   end;

Program #2

The following program fragment is intended to merge two sorted arrays of n elements each. The input arrays are a and b, and the merged array is c. Separate counters, i, j, and k are maintained for each array. In this example, all variables are integers.

[1]    i := 1; j := 1; k := 1;
[2]    while k <= 2*n 
[3]       if a[i] < b[j] then
[4]          c[k] := a[i];
[5]          i := i + 1;
[6]       else
[7]          c[k] := b[j];
[8]          j := j + 1;
[9]       end if;
[10]       k := k + 1;
[11]    end loop;
[12]    println ("Final array is:");
[13]    println (c[]);