Simulating code by hand

Analyze the program on the last page and simulate the execution of the program using the pencil-and-paper method demonstrated in class. Make sure that the following are made clear in your simulation table:
  1. Each scope is contained in a column of the table, headed by the name of the function (or just global) that defines the scope. Draw vertical ines to separate the columns.
  2. Each scope column has sub-columns within it for each variable defined in the scope. Constants can be omitted.
  3. There is a column for output.
  4. If the program reads input, that the input values are made explicit in a separate section (not in the table).
  5. There is only one change of value per row in the table. i.e. each assignment changes one value only, and each row corresponds to a separate assignment.
  6. It is acceptable to ignore a scope, whether function or file, that does not declare new variable names.
  7. Function call and return are indicated by horizontal lines in that function's scope column .

Points to watch out for

  1. Finding the end of a while loop.
  2. Finding the end of an if-else construct.
  3. Passing a value to a function.
  4. printf with the %d specifier to print out an integer
  5. scanf with the %d specifier to read in an integer from the keyboard
  6. Returning a value from a function.
  7. Local declarations within a function body.
  8. The != (not equals) operator.

/*****************************************************
 CS167 C Programming, Spring 1997
Hand simulation of code *****************************************************/ #include <stdio.h>

int doIt(int smallest) {
int n1, n2, temp;
printf("Enter two numbers: "); scanf("%d%d", &n1, &n2);

while (n1 != 9999) { if (n1 <= n2) printf("In order they are: %d %d\n", n1, n2); else { temp = n1; n1 = n2; n2 = temp; printf("In order they are: %d %d (swapped)\n", n1, n2); } if (n1 < smallest) smallest = n1; printf("Enter two numbers: "); scanf("%d%d", &n1, &n2); } return smallest;
} int main() { int smallest;
smallest = doIt(32767);
printf("Finished: smallest number is %d\n", smallest); }

Use the following input sequence of values typed at the keyboard:

23 45

52 24

25 19

9999 0

Due date

Hand in your completed simulation table to me (RTH) by Wednesday 19th. in class.