ECE2036_oldLab5.htm



ECE
2036                                                                                                                   Spring 2014


Lab
5: Complex and ComplexMatrix classes


Assigned:
March 26, 2014                                                      Section
B: Due April 10, 2014


Section A: Due
April 11, 2014


 


In this lab you will be coding two classes with
overloaded operators: a Complex class to represent complex numbers,
and a ComplexMatrix class to represent square
matrices populated with complex numbers. You must use composition; i.e. the ComplexMatrix class must include a private member which is
a 2d array of Complex objects.


The code you write should be contained in four files,
named as follows: Complex.h and ComplexMatrix.h,
which will contain the class definitions; and Complex.cpp and ComplexMatrix.cpp,
which will contain the function implementations.  I have coded a main.cpp file which is
designed to test the functionality of your classes.  You can download it here:


http://users.ece.gatech.edu/~bk91/2036/main.cpp


Feel free to modify main.cpp as needed while coding
your classes.  You should write and test
a single function at a time, so I recommend commenting out most of main.cpp and
uncommenting only the parts that utilize the class components that you have
already written.  Alternatively, you
could write your own main.cpp to test your classes.  However, for the purposes of grading, the TA
will use an unmodified copy of the main.cpp file provided above and compile it
along with your code.  The TA will then
check that the combined program (my main.cpp plus your four class files)
provides the correct output in response to values they input.  Sample output is provided below; obviously
the TA or grader will use different inputs when testing your code.


Below is a table listing all of the public member
functions and overloaded operators that your classes must provide.  You may provide more if desired.


Class
Complex














Function or operator

Functionality

constructor

Takes two double precision arguments
which are assigned to the real and imaginary parts of the Complex variable.  Without an argument, the values should
default to zero.

>>

 

Stream a complex value entered at the
command prompt into the Complex variable. 
The complex value must be entered in the format shown in the sample
below.

<<

 

Stream the Complex variable to the output
in the format shown in the sample below.

 (predecrement)

Subtract one from the real part of the
Complex variable, immediately.

 
(postdecrement)

Subtract one from the real part of the
Complex variable, after executing the rest of the statement in which it is
contained.

+ 
(double + complex)

Add a double precision number to the
real part of a Complex variable (double precision before the plus sign).

+ 
(complex + double)

Add a double precision number to the
real part of a Complex variable (double precision after the plus sign).

+ 
(complex + complex)

Add two Complex variables.

*  (complex * complex)

Multiply two Complex variables.

abs()

Return the absolute value of a Complex
variable as a double precision number.


 


Class
ComplexMatrix












Function or operator

Functionality

default constructor

Takes a single integer argument (with
default value zero) which is the desired size of the square matrix.  The constructor must dynamically allocate
memory to create the matrix of Complex objects. 

copy constructor

Takes a single ComplexMatrix
argument which is copied into the new ComplexMatrix
object.  You must also use dynamic
memory allocation here.

destructor

Deallocate the memory
allocated in the constructor and print a confirmation message as shown in the
sample below.

<<

 

Stream the ComplexMatrix
to the output in the format shown in the sample below.

fillMatrix()

Prompt the user to input each Complex
matrix element individually, as shown in the sample below.

+

Add two ComplexMatrix
objects.

*

Multiply two ComplexMatrix
objects.

()

Return a reference to a single matrix
element of the ComplexMatrix object, with row and
column specified by the two integer arguments.


 


Your code must be uploaded to t-square
by the due date and time.  The four class
code files (Complex.h, Complex.cpp, ComplexMatrix.h, and ComplexMatrix.cpp) must be simple
ASCII text files.  After you have
uploaded your code, you should meet with a TA or grader for a check-off.  During the check-off, the TA or grader will
transfer your code from t-square to the Jinx cluster
and compile it using the Gnu compiler (g++). 
The TA or grader will then test the compiled program on Jinx.  Therefore, to ensure your code runs correctly
for the TA or grader, it is strongly recommended that you test your code on
Jinx before submission.  You may choose
to do all of the coding on Jinx at your discretion. 


 


Sample
output from completed code (user input indicated in red)


Initial variable values:


c1 = 1 + i5, c2 = 0 + i0


 


Please enter complex number c3,
in the format X + iY, or X – iY:
0.5 – i2.3


You entered c3 = 0.5 – i2.3


The absolute value of c3 =
2.35372


c3 is postdecremented in this
statement.  c3 = 0.5 – i2.3


c3 is predecremented in this
statement.  c3 = -1.5 – i2.3


 


Please enter a double precision
number d1: 5.1


d1 + c3 = 3.6 – i2.3


c3 + d1 = 3.6 – i2.3


 


Please enter another complex
number c4, in the format X + iY, or X – iY: -7.7 + i3.0


c3 + c4 = -9.2 + i0.7


c3 * c4 = 18.45 + i13.21


 


Please enter an integer size
for the complex matrix M1: 3


You will now be prompted to
enter the complex elements of M1.


Please enter the matrix
elements in the order indicated.


Element (0, 0): 1 + i1


Element (0, 1): 2 + i2


Element (0, 2): 3.3 + i4.4


Element (1, 0): 0 + i0


Element (1, 1): -10 + i0


Element (1, 2): 0 – i6


Element (2, 0): -1 – i2


Element (2, 1): 1 + i2


Element (2, 2): 5 – i0.5


You entered M1 =


         1 + i1         2 + i2       3.3 + i4.4


         0 + i0       -10 + i0         0 – i6


        -1 – i2         1 + i2         5 – i0.5


 


After copying from M1, M2 =


         1 + i1         2 + i2       3.3 + i4.4


         0 + i0       -10 + i0         0 – i6


        -1 – i2         1 + i2         5 – i0.5


 


We will now modify a single
element of M2.


Please enter the row of the
element of M2 to modify, remembering that we are counting from 0 in C++: 2


Please enter the column of the
element of M2 to modify: 1


Please enter a new complex
value for the element: 0.3 + i0.4


After modifying the element, M2
=


         1 + i1         2 + i2       3.3 + i4.4


         0 + i0       -10 + i0         0 – i6


        -1 – i2       0.3 + i0.4         5 – i0.5


 


M3 = M1 + M2 =


         2 + i2         4 + i4       6.6 + i8.8


         0 + i0       -20 + i0         0 – i12


        -2 – i4       1.3 + i2.4        10 – i1


M4 = M2 * M3 =


        11 – i18    -46.27 – i18.36      59.2 + i32.1


       -24 + i12     214.4 – i7.8        -6 + i60


       -10 – i25       5.7 – i8.65      65.3 – i35.6


The memory allocated for the
matrix has been deleted.


The memory allocated for the
matrix has been deleted.


The memory allocated for the
matrix has been deleted.


The memory allocated for the
matrix has been deleted.