<I>

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: 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. Each scope column has 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, file or object, that does not declare new variable names. Function call and return is indicated by horizontal lines in that function's scope column .

Points to look out for

the default constructor and when it is called pass-by-value parameters: BE VERY CAREFUL HERE global functions and member functions with the same name calling a member function for an object member functions defined inline, or externally to the class the arithmetic assignment operator += the increment and decrement operators ++ and -- array indexing with [...] member function prototypes the standard input and output streams cin and cout the insertion (<<) and extraction (>>) operators on streams return from within a while loop (the "early" return)

The program code: file handsim.cc

////////////////////////////////////////////////////////////////
////////////// CS177 C++ Programming, Spring 1997 //////////////
////////////// Hand simulation of code            //////////////
////////////////////////////////////////////////////////////////

#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, 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 Wednesday, February 19th.