Simulating Code by Hand

Analyze the program below and write a complete simulation table, as described in class, for the program's execution from start to finish. 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 object, file, or function that defines the scope. Draw vertical ines to separate the columns.
  2. Each scope column has 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, file or object, that does not declare new variable names.

Points to look out for

The program code: file codesim.cc

////////////////////////////////////////////////////////////////
////////////// CS177 C++ Programming, Spring 1996 //////////////
////////////// Code simulation exercise           //////////////
////////////////////////////////////////////////////////////////

#include <iostream.h>

const int LENGTH = 25;

class Lucky {
private:
	int A[LENGTH];
	int count;
public:
		Lucky() { count = 0; }
	void Insert(int);
	int Find(int);
	void Increase(int n) { A[n] += 2; }
	int GetCount() { return count; }
};

void Increase(Lucky l) {
	for (int i = 0; i < l.GetCount(); i++)
		l.Increase(i);
}

int main() {
	Lucky happy;
	int num;
	cin >> num;
	while (!cin.eof()) {
		happy.Insert(num);
		cin >> num;
	}
	int found = happy.Find(32);
	if (found >= 0) {
		Increase(happy);
		happy.Find(64);
	}
}

void Lucky::Insert(int n) {
	int i1 = 0, i2;
	while (i1 < count) {
		if (A[i1] > n) {
			i2 = count;
			while (i2 > i1) {
				A[i2] = A[i2 - 1];
				--i2;
			}
			A[i1] = n;
			++count;
			return;
		}
		else
			++i1;
	}
	A[count] = n;
   	++count;
}

int Lucky::Find(int n) {
	for (int i = 0; i < count; i++)
		if (A[i] == n) {
			cout << "Found " << n << " at " << i << endl;
			return i;
		}
	return -1;
}

Input data

Use the values 15, 5, 62, 32 to be read from the keyboard, followed by an end-of-file indication (ctrl-D on UNIX, ctrl-Z on DOS). Extraction from cin will fail if the end-of-file indicator is input, leaving the variable unchanged, and causing the eof() function to succeed.

Due date

Hand in your completed assignment to me (RTH) in class on Friday, September 27th.