Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed

image text in transcribed

(2) Write a CMatrix class, which represents an nx 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 ton - 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 td 0 and use the default value n=2. int getDimension(): returns the dimension of the matrix. int getElement At(int i, int j) returns the element at (1,). Use assert to ensure that the indices are within the valid range. entAtinti, int j, int newint): replaces the element at (i, /) and return the old element at (i, j). Use assert to ensure that the indices are within the valid range. void swap Elements At(int il, int jl, int i2, int i3): swaps between the two elements at (17,71) and at (12, 12). 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 read Matrix(): reads n x n elements from the input using cin. For example for a 3 x 3 matrix, the format should be: Input matrix clement at(1, 1): 11 Input matrix clement 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 .. ... 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 x6, shrink it to 4 xy4, 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 allocate Matrix Memory and deallocate Matrix Memory to dynamically manage the dynamic 2D array Matrix. We have discussed the details of those operations

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

Object Oriented Databases Prentice Hall International Series In Computer Science

Authors: John G. Hughes

1st Edition

0136298745, 978-0136298748

More Books

Students also viewed these Databases questions

Question

L05 Analyze the importance of critical periods of development.

Answered: 1 week ago