### using OOP to implement matrix class that provide the basic matrix operations

i want to know how i can solve this question? do i need to create a class or write the program codes, i dont understand it
What is the complete question you have been assigned?
the question is using oop implement a matrix class that provides the basic matrix operations (addition, subtraction, multiplication, Inverse, Transportation).
 do i need to create a class or write the program codes

To create a class you write the program's code.

Anyway, using oop means that you have to create a class. That class should contain a matrix (meaning an array of arrays) of elements on which you will perform the calculations needed by matrix operations.
ok, thank you maeriden. I will try and it to you and see if there is any mistake, tanks
this is the solution, pls someone should help me with the correction, the question is using oop implement a matrix class that provides the basic matrix operations (addition, subtraction, multiplication, Inverse, Transportation).

#include <iostream>
using namespace std;
int main;

class matrix
{
int **p, m, n;
public:
matrix(int row = 2, int col = 2)
{
m = row;
n = col;
p = new(int *); m;
for (int i = 0; i < m; i++)
p[i] = new int[n];
}
~matrix()
{
for (int i = 0; i < m; i++)
delete p[i];
delete p;
}
void accept()
{
cout<<"Enter matrix elements:";
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
cin >> p[i][j];
}
}
}
void display()
{
cout <<"The matrix is:";
for(int i = 0; i < m; i++)
{
cout <<endl;
for(int j = 0; j < n; j++)
{
cout << p[i][j] <<" ";
}
}
}
matrix operator +(matrix m2)
{
matrix T(m, n);
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
T.p[i][j] = p[i][j] + m2.p[i][j];
}
}
return T;
}
friend matrix operator * (matrix, matrix);
};
matrix operator * (matrix a , matrix b)
{
if(a.n == b.m)
{
matrix T(a.m, b.n);
for(int i = 0; i < a.m; i++)
{
for(int k = 0; k < b.n; k++)
{
T.p[i][k] = 0;
for(int j = 0; j < a.n; j++)
{
T.p[i][k]+= a.p[i][j] * b.p[j][k];
}
}
}
return T;
}
}
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495`` ``````#include using namespace std; class matrix { int **p, m, n; public: matrix(int row, int col) { m = row; n = col; p = new int*[m]; for (int i = 0; i < m; i++) p[i] = new int[n]; } ~matrix() { for (int i = 0; i < m; i++) delete p[i]; delete p; } void accept() { cout<<"Enter matrix elements:"; for(int i = 0; i < m; i++) { for(int j = 0; j < n; j++) { cin >> p[i][j]; } } } void display() { cout <<"The matrix is:"; for(int i = 0; i < m; i++) { cout <
Thank you smac89, it compile successfully but when i run the program it prints process exited with return value 0, pls what can i do?
You have to add more things for it to do:

Here is a revision of the previous, still has some bugs. I have to use friend operators instead of matrix operators to avoid overwritting valueable data. This can be easily. I will check back here tomorrrow

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123`` ``````#include using namespace std; class matrix { int **p, m, n; public: matrix(int row, int col) { m = row; n = col; p = new int*[m]; for (int i = 0; i < m; ++i) { p[i] = new int[n]; for (int j = 0; j < n; ++j) p[i][j] = 0; } } // ~matrix() // { // delete [] p; // } void accept() { cout<<"Enter matrix elements: "; for(int i = 0; i < m; i++) { for(int j = 0; j < n; j++) { cin >> p[i][j]; } } } void display() { cout <<"The matrix is:\n"; for(int i = 0; i < m; i++) { for(int j = 0; j < n; j++) { cout << p[i][j] <<" "; } cout <
Last edited on
So you sent me a private msg last Friday asking me to do this assignment for you. I foolishly wrote the code on Saturday, but as you haven't replied to any of my messages, I haven't handed the code over. Good luck...
Last edited on
You could use a pseudomatrix
http://www.cplusplus.com/doc/tutorial/arrays/
See the "pseudo-multidimensional array" part.

I have made my own matrix class using a pseudo-multidimensional array
Last edited on
 You could use a pseudomatrix http://www.cplusplus.com/doc/tutorial/arrays/ See the "pseudo-multidimensional array" part. I have made my own matrix class using a pseudo-multidimensional array

Yes, that's what I did with my solution, except I used a vector.
please im so sorry ajh32, i didnt receive any of your message, thank you @ eklavya sharma 2, scam89, and ajh32, i really appreciate your contributions, i will do it know, thanks once again
Topic archived. No new replies allowed.