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:
////////////////////////////////////////////////////////////////
////////////// 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;
}
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.
Hand in your completed assignment to me (RTH) in class on Friday, September 27th.