PullMonkey Blog

26 Mar

ECE231 – Spring 2009 – Programming Assignment 3


This is a step-by-step tutorial for this assignment and an explanation of the basics of how it works for those that are having a difficult time understanding or just are stuck somewhere. To view the assignment click here.

So for this assignment we are going to be doing a lot of math calculations so we are going to need the include the both the iostream and the cmath libraries. We will also need the value of pi.

1
2
3
4
5
6
7
8
9

#include <iostream>
#include <cmath>
using namespace std;

#define PI 3.141592654
or
const double PI = 3.141592654;

Defining the Complexn Class

For the complexn class we need 2 private variables r and i, 3 constructors, a copy constructor, 4 member functions, 9 overloaded opperators, and 2 friend functions.

Like the previous program we will define everything with in the class and have the actual functions at the bottom.

So here is what your class should look like (keep in mind that this all goes at the top before the main function):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

class complexn
{
  private:
    double r; // real part of the complex number
    double i; // imaginary part of the complex number
  public:
    complexn();
    complexn(double );
    complexn(double , double );
    complexn(const complexn & );

    double complexabs();
    double complexangle();
    complexn complexconj();
    double distance(const complexn & );

    complexn operator + (const complexn & );
    complexn operator - (const complexn & );
    complexn operator * (const complexn & );
    complexn operator / (const complexn & );
    complexn operator = (const complexn & );

    complexn operator ++ ();
    complexn operator ++ (int );
    complexn operator -- ();
    complexn operator -- (int );

    friend ostream & operator << (ostream & , const complexn & );
    friend istream & operator >> (istream & , complexn & );
};

Writing the Actual Functions for the Complexn Class

First we have the 3 constructors and the copy constructor:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

// takes no arguments and sets r and i to 0.0
complexn::complexn()
{
  r = i = 0.0;
}
// takes 1 argument and sets r to the argument and i to 0.0
complexn::complexn(double real)
{
  r = real;
  i = 0.0;
}
// takes 2 arguments and sets r to the first and i to the second
complexn::complexn(double real, double imag)
{
  r = real;
  i = imag;
}
// this is a copy constructor that dereferences the complexn variable if it is referenced
complexn::complexn(const complexn &c)
{
  r = c.r;
  i = c.i;
}

Next, we have the 3 member functions:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

// takes no arguments and returns the distance from the 0 as a double
double complexn::complexabs()
{
  return sqrt(pow(r,2.0) + pow(i,2.0));
}
// takes no arguments and returns the angle of the complex point as a double in radians
double complexn::complexangle()
{
  double angle; // angle of coordinate from positive x axis
  angle = atan(r / i);
  if(r < 0 and i >= 0)
    return PI - abs(angle);
  if(r < 0 and i < 0)
    return -(PI - abs(angle));
  return angle;
}
// takes no arguments and returns the conjugate of the complex number as a complexn class
complexn complexn::complexconj()
{
  complexn temp = *this; // complex number used for calculations
  temp.i *= -1;
  return temp;
}
// takes one complexn type argument and returns the distance between the argument and the current instance
double complexn::distance(const complexn &temp)
{
  return sqrt(pow(temp.r -r, 2.0) + pow(temp.i - i, 2.0));
}

Next, we have the 9 overloaded operators:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

complexn complexn::operator + (const complexn &temp)
{
  complexn cn; // complex number used for calculations
  cn.r = r + temp.r;
  cn.i = i + temp.i;
  return cn;
}
complexn complexn::operator - (const complexn &temp)
{
  complexn cn; // complex number used for calculations
  cn.r = r - temp.r;
  cn.i = i - temp.i;
  return cn;
}
complexn complexn::operator * (const complexn &temp)
{
  complexn cn; // complex number used for calculations
  cn.r = r * temp.r - i * temp.i;
  cn.i = r * temp.i + i * temp.r;
  return cn;
}
complexn complexn::operator / (const complexn &temp)
{
  complexn cn; // complex number used for calculations
  cn.r = (r * temp.r + i * temp.i) / (pow(temp.r, 2.0) + pow(temp.i, 2.0));
  cn.i = (i * temp.r - r * temp.i) / (pow(temp.r, 2.0) + pow(temp.i, 2.0));
  return cn;
}
complexn complexn::operator = (const complexn &temp)
{
  r = temp.r;
  i = temp.i;
  return *this;
}
complexn complexn::operator ++ ()
{
  r += 1;
  return *this;
}
complexn complexn::operator ++ (int x)
{
  complexn temp = *this;
  r += 1;
  return temp;
}
complexn complexn::operator -- ()
{
  r -= 1;
  return *this;
}
complexn complexn::operator -- (int x)
{
  complexn temp = *this;
  r -= 1;
  return temp;
}

Last, we have the 2 friend functions:

1
2
3
4
5
6
7
8
9
10
11
12

ostream & operator << (ostream &os, const complexn &temp)
{
  os << temp.r << " + " << temp.i << "i";
  return os;
}
istream & operator >> (istream &is, complexn &temp)
{
  is >> temp.r >> temp.i;
  return is;
}

Testing Your program

I wrote a fairly good program for testing this complexn class.

To test your program copy your program to [name].h file and in the main.cpp file change the header file at the top to match your file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127

#include <iostream>
#include "yourprogram.h" // the name of your comlexn header file goes here

using namespace std;

int main()
{
  complexn c1;
  complexn c2(1);
  complexn c3(3, -5);
  complexn c4, c5, c6;

  cout << "Complex Numbers: " << endl;
  cout << "  C1: " << c1 << endl;
  cout << "  C2: " << c2 << endl;
  cout << "  C3: " << c3 << endl;

  cout << "nEnter values for C1: ";
  cin >> c1;
  cout << "  You enetered: " << c1 << endl;
  cout << "Enter values for C2: ";
  cin >> c2;
  cout << "  You enetered: " << c2 << endl;
  cout << "Enter values for C3: ";
  cin >> c3;
  cout << "  You enetered: " << c3 << endl;

  cout << "nDistances from origin: " << endl;
  cout << "  C1(" << c1 << "): " << c1.complexabs() << endl;
  cout << "  C2(" << c2 << "): " << c2.complexabs() << endl;
  cout << "  C3(" << c3 << "): " << c3.complexabs() << endl;

  cout << "nConjugates:" << endl;
  cout << "  C1(" << c1 << "): " << c1.complexconj() << endl;
  cout << "  C2(" << c2 << "): " << c2.complexconj() << endl;
  cout << "  C3(" << c3 << "): " << c3.complexconj() << endl;

  cout << "nDistances between:" << endl;
  cout << "  C1(" << c1 << ") and C2(" << c2 << "): " << c1.distance(c2) << endl;
  cout << "  C1(" << c1 << ") and C3(" << c3 << "): " << c1.distance(c3) << endl;
  cout << "  C2(" << c2 << ") and C3(" << c3 << "): " << c2.distance(c3) << endl;

  cout << "nAddition:" << endl;
  cout << "  C1(" << c1 << ") + C2(" << c2 << "): " << c1 + c2 << endl;
  cout << "  C1(" << c1 << ") + C3(" << c3 << "): " << c1 + c3 << endl;
  cout << "  C2(" << c2 << ") + C3(" << c3 << "): " << c2 + c3 << endl;

  cout << "nSubtraction:" << endl;
  cout << "  C1(" << c1 << ") - C2(" << c2 << "): " << c1 - c2 << endl;
  cout << "  C1(" << c1 << ") - C3(" << c3 << "): " << c1 - c3 << endl;
  cout << "  C2(" << c2 << ") - C3(" << c3 << "): " << c2 - c3 << endl;

  cout << "nMultiplication:" << endl;
  cout << "  C1(" << c1 << ") * C2(" << c2 << "): " << c1 * c2 << endl;
  cout << "  C1(" << c1 << ") * C3(" << c3 << "): " << c1 * c3 << endl;
  cout << "  C2(" << c2 << ") * C3(" << c3 << "): " << c2 * c3 << endl;

  cout << "nDivision:" << endl;
  cout << "  C1(" << c1 << ") / C2(" << c2 << "): " << c1 / c2 << endl;
  cout << "  C1(" << c1 << ") / C3(" << c3 << "): " << c1 / c3 << endl;
  cout << "  C2(" << c2 << ") / C3(" << c3 << "): " << c2 / c3 << endl;

  cout << "nIncrementing:" << endl;
  cout << "  C1 =  " << c1   << endl;
  cout << "  C1++: " << c1++ << endl;
  cout << "  C1 =  " << c1   << endl;
  cout << "  ++C1: " << ++c1 << endl;
  cout << "  C1 =  " << c1   << endl;

  cout << "  C2 =  " << c2   << endl;
  cout << "  C2++: " << c2++ << endl;
  cout << "  C2 =  " << c2   << endl;
  cout << "  ++C2: " << ++c2 << endl;
  cout << "  C2 =  " << c2   << endl;

  cout << "  C3 =  " << c3   << endl;
  cout << "  C3++: " << c3++ << endl;
  cout << "  C3 =  " << c3   << endl;
  cout << "  ++C3: " << ++c3 << endl;
  cout << "  C3 =  " << c3   << endl;

  cout << "nDecrementing:" << endl;
  cout << "  C1 =  " << c1   << endl;
  cout << "  C1--: " << c1-- << endl;
  cout << "  C1 =  " << c1   << endl;
  cout << "  --C1: " << --c1 << endl;
  cout << "  C1 =  " << c1   << endl;

  cout << "  C2 =  " << c2   << endl;
  cout << "  C2--: " << c2-- << endl;
  cout << "  C2 =  " << c2   << endl;
  cout << "  --C2: " << --c2 << endl;
  cout << "  C2 =  " << c2   << endl;

  cout << "  C3 =  " << c3   << endl;
  cout << "  C3--: " << c3-- << endl;
  cout << "  C3 =  " << c3   << endl;
  cout << "  --C3: " << --c3 << endl;
  cout << "  C3 =  " << c3   << endl;

  c4 = c1;
  c5 = c2;
  c6 = c3;

  cout << "nEquals:" << endl;
  cout << "  C4 = C1(" << c1 << "): " << c4 << endl;
  cout << "  C5 = C2(" << c2 << "): " << c5 << endl;
  cout << "  C6 = C3(" << c3 << "): " << c6 << endl;

  c1 = complexn ( 1, 1);
  c2 = complexn (-1, 1);
  c3 = complexn (-1,-1);
  c4 = complexn ( 1,-1);
  c5 = complexn ( 0, 1);
  c6 = complexn (-1, 0);
  cout << "nAngles:" << endl;
  cout << "  C1(" << c1 << "): " << c1.complexangle() << endl;
  cout << "  C2(" << c2 << "): " << c2.complexangle() << endl;
  cout << "  C3(" << c3 << "): " << c3.complexangle() << endl;
  cout << "  C4(" << c4 << "): " << c4.complexangle() << endl;
  cout << "  C5(" << c5 << "): " << c5.complexangle() << endl;
  cout << "  C6(" << c6 << "): " << c6.complexangle() << endl;

  return 0;
}



2 Responses to “ECE231 – Spring 2009 – Programming Assignment 3”

  1. By Demo on Mar 26, 2009 | Reply

    I don’t know who you are, but you are a great man. Thank you for doing these guides on the ECE 231 assignments, they are amazingly helpful. Do you do private tutoring by any chance?

  2. By slaive on Mar 26, 2009 | Reply

    Well I’m glad it is of help!

    I can do private tutoring if you need it. My email is scnaegle@yahoo.com

    So just shoot me a message and we will work things out.

    –slaive

Sorry, comments for this entry are closed at this time.