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:
- 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.
- Each scope column has sub-columns within it for each variable defined in the scope. Constants can be omitted.
- There is a column for output.
- If the program reads input, that the input values are made explicit in a separate section (not in the table).
- 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.
- It is acceptable to ignore a scope, whether function or file, that does not declare new variable names.
- Function call and return are indicated by horizontal lines in that function's scope column .
Points to watch out for
- Finding the end of a while loop.
- Finding the end of an if-else construct.
- Passing a value to a function.
- printf with the %d specifier to print out an integer
- scanf with the %d specifier to read in an integer from the keyboard
- Returning a value from a function.
- Local declarations within a function body.
- 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.