Homework1

[Homework 1 is due on Jun 15 (Wednesday)]

Files:

Copy all files from the homework1/problem folder. In your home directory, make a homeworks/homework1 folder and issue the following command inside the homeworks/homework1 folder:

cp /comp/15/public_html/homeworks/homework1/problem/* .

Note:

A Makefile has been provided containing the following:

CC=g++
CFLAGS=-std=c++0x -g
SRCS=main.cpp Matrix.cpp
EXEC=Matrix
all: $(SOURCES)
 $(CC) $(CFLAGS) $(SRCS) -o $(EXEC)
clean: 
 rm Matrix *~

You can reuse this Makefile by simply giving the appropriate list of source files (*.cpp) in the SRCS list. This is very convenient as the compilation can be done by simply calling “make”. The name of the executable is specified by EXEC. In this case, the executable generated is called “Matrix”. So, you can run the program by calling “./Matrix” at the command prompt.

Also note that we are creating a debug build (because of using -g flag) and are capable of handling C++11 constructs, if any (because of using -std=c++0x flag).

Problem:

Define 2-d Matrix as a first-class data-type (with elements of integer data-type) having proper definitions of the constructor, the destructor, the copy constructor, the assignment operator, and supporting following operations:

1. Overloaded operator>>: Read the number of rows, the number of columns and then the elements.

Example operation:

Matrix x;
cin >> x; 
// This includes reading of number of rows, 
// number of columns and then the elements

2. Overloaded + operator: Perform element-wise addition of two matrices. Make sure the matrix sizes match before performing the addition.

Example operation:

Matrix c, a, b;
c = a + b; // Check for matching size as well

3. Overloaded operator<< for printing a given matrix.

Example operation:

Matrix a;
...
cout << a << endl;

4. [Bonus question] Derive Square matrix and identity matrix types appropriately. (Optional)

Example operation:

IdentityMatrix i(4); // i is an identity matrix of size 4
SquareMatrix s(5); // s is a square matrix of size 5

5. Overloaded * operator for matrix multiplication:

Example operation:

Matrix m1(6, 4);
IdentityMatrix m2(4);
Matrix m3 = m1 * m2; // Check for expected sizes

Testing your program:

main.cpp

Expected output:

Enter the matrix:

Matrix [0][0] = 1

Matrix [0][1] = 2

Matrix [0][2] = 3

Matrix [1][0] = 4

Matrix [1][1] = 5

Matrix [1][2] = 6

Matrix [2][0] = 7

Matrix [2][1] = 8

Matrix [2][2] = 9

m1:

1 2 3

4 5 6

7 8 9

Enter the matrix:

Matrix [0][0] = 1

Matrix [0][1] = 1

Matrix [0][2] = 1

Matrix [1][0] = 1

Matrix [1][1] = 1

Matrix [1][2] = 1

Matrix [2][0] = 1

Matrix [2][1] = 1

Matrix [2][2] = 1

m2:

1 1 1

1 1 1

1 1 1

m3:

2 3 4

5 6 7

8 9 10

Enter the number of rows: 3

Enter the number of columns: 3

Enter the matrix:

Matrix [0][0] = 2

Matrix [0][1] = 3

Matrix [0][2] = 4

Matrix [1][0] = 5

Matrix [1][1] = 6

Matrix [1][2] = 7

Matrix [2][0] = 8

Matrix [2][1] = 9

Matrix [2][2] = 10

m4:

2 3 4

5 6 7

8 9 10

m5:

51 60 69

96 114 132

141 168 195

i1:

1 0 0

0 1 0

0 0 1

m6:

51 60 69

96 114 132

141 168 195

Leak Check:

You can use “valgrind” to quickly check whether your program has any memory leak. Here is the command:

valgrind --leak-check=yes ./Matrix

Solution:

The solution files are here.