Declare a class matrix with the following features
a) A double** data type to store elements of the array
b) Constructor to allocate memory based on size specified and initialize elements of the matrix
c) copy constructor
d) member function to find and return the transpose of the matrix
e) Overloaded ‘+’ operator to add two matrices of same dimensions, and add a scalar to all
elements
f) Overloaded operator ‘<<’ to print the matrix in proper format. Demonstrate all the
functions with the help of a main program.
g) Destructor to free any dynamically allocated memory
h) Optional member functions: i.) reduce matrix to row echelon form
How to write code for following problem?
Hello @2000bhaveshkumar,
You have specified the entire program in the question itself.
To write the code for above either watch the following videos or recall the following concept:
- Dynamic allocation and de-allocation of 2D array.
- Class
- Constructor and destructor.
- Operator overloading.
Write a code and if you’ll face any issue, let me know.
I have written the code but it is showing run time error on execution.
Please reply instantly.
Hello @2000bhaveshkumar,
There are two issues with your code:
-
Run-time error:
You have not declared the dynamic array properly.
You have not declared the columns for N rows you have assigned. -
No output:
You are using N in the print function but you have not assigned any value to it.
Rather use size as you have assigned it the value you have passed in the constructor.
Hope, this would help.
Give a like if you are satisfied.
I have completed the code according to problem,
but it is giving error on compilation.
Also Thanks for quick reply.
I am waiting for your quick reply!
Hello @S18ML0016,
-
Where’s the copy constructor?
-
I would suggest you to read https://chortle.ccsu.edu/VectorLessons/vmch13/vmch13_14.html to understand the transpose operation.
-
As we know that the matrix you have declared is a private member of the class.
So, we cannot access it with their object.
To overcome this, you have created a function to print that matrix.
Invalid access: cout<m[i][j];
Correct your code.
Hope, this would help.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.