RETURN LINK: Lab 5 instructions

ENGG 335: Computing for Engineers II
Lab 5 Solutions

Author: Steve Norman
Last modified: Tue Oct 14 16:34:43 MDT 1997

Contents


Exercise A

If you copied the files before about 11:30am, Monday, Oct. 14, you got the necessary files plus Counter.cc, which is junk. You should ignore Counter.cc

This was a very simple exercise, but since I said I would publish a solution, here it is.

Revised class definition (in Counter.h)

class Counter {
public:
  Counter();
  int count() const;
  void increment();
  void decrement();  // This line is the only change.
private:
  int countM;
};

New function definition (in Counter.cpp)

void Counter::decrement()
{
  countM--;
}

Revised definition of main (in exAprog.cpp)

int main()
{
  Counter c;
  cout << "initial value of c.count() is " << c.count() << endl;

  c.increment();
  c.increment();
  cout << "after 2 increment calls, c.count() is " << c.count() << endl;

  // The next three lines are new code to demonstrate the decrement
  // function.
  c.decrement();
  c.decrement();
  cout << "after 2 decrement calls, c.count() is " << c.count() << endl;

  return 0;
}

[back to top of document]


Exercise B

New function definition (in AList.cpp)
void AList::remove(int pos)
{
  assert(pos >= 0 && pos < length());

  lengthM--;
  for (int i = pos; i < lengthM; i++)
    itemM[i] = itemM[i + 1];
}

Program output including tests of remove operation

expected contents: [empty list]
actual contents  : [empty list]

expected contents: 11 22 33 44 55
actual contents  : 11 22 33 44 55

expected contents: 500 400 300 200 100
actual contents  : 500 400 300 200 100

expected contents: 101 501 401 301 201
actual contents  : 101 501 401 301 201

expected contents: 501 401 301 201
actual contents  : 501 401 301 201

expected contents: 501 401 301
actual contents  : 501 401 301

expected contents: 501 301
actual contents  : 501 301

No assertions failed.
If actual contents always matched expected contents, all tests were passed.

[back to top of document]


Exercise D

Point One:
Exercise D: diagram for Point One

Point Two:
Exercise D: diagram for Point Two

Point Three:
Exercise D: diagram for Point Three

[back to top of document]


Exercise E

New function definitions (in Cplx.cpp)
void cplx_subtract(Cplx z1, Cplx z2,  Cplx *presult)
{
  presult->real = z1.real - z2.real;
  presult->imag = z1.imag - z2.imag;
}

Cplx cplx_multiply(const Cplx *pz1, const Cplx *pz2)
{
  Cplx result;
  result.real = pz1->real * pz2->real - pz1->imag * pz2->imag;
  result.imag = pz1->real * pz2->imag + pz1->imag * pz2->real;
  return result;
}

void cplx_divide(const Cplx *pz1, const Cplx *pz2, Cplx *presult)
{
  assert(pz2->real != 0 || pz2->imag != 0);

  double a = pz1->real, b = pz1->imag;
  double c = pz2->real, d = pz2->imag;
  double c2_plus_d2 = c * c + d * d;
  presult->real = (a * c + b * d) / c2_plus_d2;
  presult->imag = (b * c - a * d) / c2_plus_d2;
}
(Note the use of local variables in cplx_divide--doing this makes it much easier to check the code against the formula in the instructions.)

Revised definition of main (in useCplx.cpp)

int main(void)
{
  Cplx w, z;                    /* entered by user */
  Cplx sum;                     /* sum of w and z */
  Cplx diff;                    // w - z
  Cplx product;                 // w * z
  Cplx quotient;                // w / z

  cout << "This programs needs values for complex numbers w and z.\n";

  cout << "  Please enter the real part of w     : ";
  cin >> w.real;
  cout << "  Please enter the imaginary part of w: ";
  cin >> w.imag;

  cout << "  Please enter the real part of z     : ";
  cin >> z.real;
  cout << "  Please enter the imaginary part of z: ";
  cin >> z.imag;

  cout << "\nw is (" << w.real << ") + j(" << w.imag << ").\n";
  cout << "z is (" << z.real << ") + j(" << z.imag << ").\n";

  sum = cplx_add(w, z);
  cout << "\nsum is (" << sum.real << ") + j(" << sum.imag << ").\n";

  cplx_subtract(w, z, &diff);
  cout << "\nw - z is (" << diff.real << ") + j(" << diff.imag << ").\n";

  product = cplx_multiply(&w, &z);
  cout << "\nw * z is (" << product.real
       << ") + j(" << product.imag << ").\n";

  cplx_divide(&w, &z, &quotient);
  cout << "\nw / z is (" << quotient.real
       << ") + j(" << quotient.imag << ").\n";

  return 0;
}

[back to top of document]


Exercise F

Exercise F solution

[back to top of document]