Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(2) Write a CMatrix class, which represents an n x n matrix of integers. Such a matrix is called a square matrix. Note: An ordinary

(2) Write a CMatrix class, which represents an n x n matrix of integers. Such a matrix is called a square matrix. Note: An ordinary user is going to use your matrix. So to her/him, the index of row is from 1 to n while the index of column is from 1 to n as well. But internally, we know that the array index should be from 0 to n 1. For instance, if s/he wants to get the element at (5, 4), this element actually corresponds to (4, 3) in your 2D dynamic array in C++. Please pay attention to this.

Your class should include the following member functions. The member variable is called Matrix, which is a 2D dynamic array, and the member variable Dimension represents the dimension of the matrix.

Constructor: accepts n as a parameter and constructs a matrix of the requested size. Initialize all entries to 0 and use the default value n = 2. int getDimension(): returns the dimension of the matrix. int getElementAt(int i, int j): returns the element at (i, j). Use assert to ensure that the indices are within the valid range. int replaceElementAt(int i, int j, int newint): replaces the element at (i, j) and return the old element at (i, j). Use assert to ensure that the indices are within the valid range. void swapElementsAt(int i1, int j1, int i2, int i3): swaps between the two elements at (i1, j1) and at (i2, j2). Use assert to ensure that all the indices are within the valid range. void resizeMatrix(int newsize): resizes the matrix from its original size to the newsize. We can enlarge the original matrix: we copy the original matrix to the new one and set 0 to all the additional elements in the new one. We can also shrink the original matrix: we copy the elements of the original matrix within the newsize to the new one and ignore those beyond the newsize. Use assert to ensure that the newsize is valid, i.e., not a negative number. void readMatrix(): reads n x n elements from the input using cin. For example for a 3 x 3 matrix, the format should be:

Input matrix element at (1, 1): 1

Input matrix element at (1, 2): 2

Input matrix element at (1, 3): 5

Input matrix element at (2, 1): 5

Input matrix element at (3, 3): 45

Done reading

void printMatrix(): prints out the elements in the matrix using cout. For example, for the above 3 x 3 matrix, the printout would be formatted to:

1 2 5

5

45

You are required to use a dynamic 2D array to implement the matrix. The declaration of the class is put into matrix.h while the implementation of the class is put into matrix.cc. Create a test program called testMatrix.cc. Your test should show that the member functions of the matrix class work as expected. One good way to do this: create a 4 x 4 array, read elements in it, print it out, change some elements, swap some elements, resize it to 6 x 6, shrink it to 4 x 4, shrink it to 3 x 4, etc. and etc. Each time when you change something in the matrix, just print it out to verify (show) that your program works.

You can create two helper functions called allocateMatrixMemory and deallocateMatrixMemory to dynamically manage the dynamic 2D array Matrix. We have discussed the details of those operations.

please help with this program

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Sams Teach Yourself Beginning Databases In 24 Hours

Authors: Ryan Stephens, Ron Plew

1st Edition

067232492X, 978-0672324925

More Books

Students also viewed these Databases questions

Question

Distinguish between a bond's coupon yield and a current yield?

Answered: 1 week ago