Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed

(2) Write a CMatrix class, which represents an nsn matris of integers. Such a matris is called a square matrix. Note: An ordinary user is going to use your matris. So to her him the index of row is from 1 to a while the index of column is from 1 to n as well But internally, we know that the array indes should be from 0 to -1. For instance if she wants to get the element at (5. 4. this element actually corresponds to (4.3) in your 2D dynamic array in 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 a ble Dimension represents the dimension of the mais Constructor accepts as a parameter and contracts a matris of the requested size Initialize all entries to and use the default value = 2 int getDimension: returns the dimension of the matris int getElementAtinti intj) returns the elementat (1) Use assert to ensure that the indices are within the valid range ist replaceElementAt(int i intj, int beint replaces the element at (.) and return the old elementat (L . Use assert to ensure that the indices are within the valid range void swap Elements.Atint il, int jl, int 12, int 13): swaps between the two elements at andat (12.2. Use assert to ensure that all the indices are within the vald range void resize Matrit Bewsize): resizes the matrix from its original size to the neste We can enlarge the original mats we copy the on ainal matris to the new one and set 0 to all the additional elements in the new one. We can also shrink the original maths we copy the elements of the original matris within the wste to the new ope and ignore those beyond the size. Use assert to ensure that the noise is valid, ie, not a negative number void read Matrixo: reads a n elements from the input using cin. For example for a 33 3 matrix, the format should be Input matrix element at(1.1): 11 Input matris element at(1.2): 2 Input matris element at (1.3): 5 Input matris element at (2, 1): 5 Input matrix element at (.3): 45 Done reading void printMatrixo: prints out the elements in the matris using cout. For example, for the above 3 3 matris, the printout would be formatted to 25 You are required to use a dynamic 2D any to implement the mattis. The declaration of the class is put into matrix while the implementation of the class is put into matrice Create a test program called static.ce. Your test should show that the member functions of the matris class work as expected One good way to do this: Create a 4x4 any read elements in it print it out change some elements, swap some elements, resize it to 6 6. shrink it to 4 4. shrink it to 3 3 4. etc. and etc. Each time when you change something in the matris, 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 anray Matis. 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

Database Systems On GPUs In Databases

Authors: Johns Paul ,Shengliang Lu ,Bingsheng He

1st Edition

1680838482, 978-1680838480

Students also viewed these Databases questions