CS117 Programming Methodology
Fall 1999
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.
7. Function call and return is indicated by horizontal lines in that function's scope column .
· the default constructor and when it is called
· pass-by-reference parameters: BE VERY CAREFUL HERE
· global functions and member functions
· calling a member function for an object
· member functions defined inline, or externally to the class
· array indexing with [...]
· member function prototypes
· the standard output streams cout
· the insertion (<<) operator on streams
· declaration of a for loop variable in the loop header
Hand in your completed assignment to me (RTH) in class on Monday, November 15th. in class.
#include
<iostream>
using
namespace std;
const
int SIZE = 3;
const
int BIG = 256;
class C
{
private:
int maxValInd, minValInd;
int arr[SIZE];
public:
C() {
arr[0] = 140;
arr[1] = 221;
arr[2] = 56;
}
int* getArray() { return arr; }
int getMaxValInd() { return maxValInd; }
void findMax();
void findMin();
void printRange();
};
void
C::findMax() {
int val = 0;
for (int i = 0; i < SIZE; i++)
if (arr[i] > val) {
val = arr[i];
maxValInd = i;
}
}
void
C::findMin() {
int val = 256;
for (int i = 0; i < SIZE; i++)
if (arr[i] < val) {
val = arr[i];
minValInd = i;
}
}
void
C::printRange() {
cout << "Range is " <<
arr[minValInd] << " to " << arr[maxValInd] << endl;
}
void
normalize(C& c) {
int* a = c.getArray();
int max = a[c.getMaxValInd()];
for (int i = 0; i < SIZE; i++)
a[i] = a[i] * BIG / max;
}
int
main() {
C c;
c.findMax();
normalize(c);
c.findMin();
c.printRange();
return 0;
}