#include <iostream>
#include <iomanip>

using namespace std;

class Matrix
{
public:
	Matrix (int =0, int =0);
	~Matrix ();
	Matrix (const Matrix&);

	void operator = (const Matrix&);
	Matrix operator + (const Matrix&); 
	Matrix operator * (const Matrix&);
	Matrix operator - (const Matrix&);

	
	int* getMember(int =0, int =0) const; //returns pointer to matrix member
	void setMatrix (); //set matrix members one at a time
	void displayMatrix ();
	void nulify (); // set matrix members to 0's
private:
	int * pointer; //pointer to matrix member
	int * reset; //pointer to the begunning of the matrix array [m*n] in memory
	int m,n; //matrix dimensions
};

void nulify (Matrix&);

int main ()
{
	cout <<"MATRIX ALGEBRA PROGRAM : "<<endl;
	int choice=0;
	do
	{
		cout <<"1) ADDITION"<<endl;
		cout <<"2) SUBSTRACTION"<<endl;
		cout <<"3) MULTIPLICATION"<<endl;
		cout <<"4) EXIT "<<endl;
		cout <<"your choice : ";
		cin >> choice;
		switch (choice)
		{
		case 1:
			{
				cout <<"enter matrix sizes: "<<endl;
				cout <<"row number : ";
				int m;
				cin >> m;
				cout <<"column number : ";
				int n;
				cin >> n;
				Matrix A (m,n), B (m,n), C(m,n);
				cout <<"enter matrix A"<<endl;
				A.setMatrix();
				cout <<"enter matrix B"<<endl;
				B.setMatrix();
				cout <<"matrix A = "<<endl;
				A.displayMatrix();
				cout <<"matrix B = "<<endl;
				B.displayMatrix();
				C=A+B;
				cout <<"C = A + B "<<endl;
				C.displayMatrix();
			} break;
		case 2:
			{
				cout <<"enter matrix sizes: "<<endl;
				cout <<"row number : ";
				int m;
				cin >> m;
				cout <<"column number : ";
				int n;
				cin >> n;
				Matrix A (m,n), B (m,n), C(m,n);
				cout <<"enter matrix A "<<endl;
				A.setMatrix();
				cout <<"enter matrix B "<<endl;
				B.setMatrix();
				cout <<"matrix A = "<<endl;
				A.displayMatrix();
				cout <<"matrix B = "<<endl;
				B.displayMatrix();
				C=A-B;
				cout <<"C = A - B "<<endl;
				C.displayMatrix();
			} break;
		case 3:
			{
				cout <<"enter matrix A sizes: "<<endl;
				cout <<"row number : ";
				int mA;
				cin >> mA;
				cout <<"column number : ";
				int nA;
				cin >> nA;
				cout <<"enter matrix B sizes: "<<endl;
				cout <<"row number : ";
				int mB;
				cin >> mB;
				cout <<"column number : ";
				int nB;
				cin >> nB;
				Matrix A (mA,nA), B (mB,nB), C(mA,nB);
				cout <<"enter matrix A"<<endl;
				A.setMatrix();
				cout <<"enter matrix B" <<endl;
				B.setMatrix();
				cout <<"matrix A = "<<endl;
				A.displayMatrix();
				cout <<"matrix B = "<<endl;
				B.displayMatrix();
				C=A*B;
				cout <<"C = A * B "<<endl;
				C.displayMatrix();
			} break;
		default:
			break;
		}
	} while (choice !=4);
	

	return 0;
}

Matrix::Matrix (int x, int y) : m(x), n(y) //matrix will be constructed initialy to hold only 0s
{
	if (m*n <=0)
	{
		cout <<"error : user constructor - matrix dimensions must be positive integers"<<endl;
		exit (0);
	} else
	{
		pointer = new int [m*n]; 
		reset = pointer;
		for (int i=0; i<m*n; i++)
			*(pointer+i) =0;
	}
	pointer = reset;
}

Matrix::~Matrix()
{
	delete [] pointer;
}
Matrix::Matrix(const Matrix& M)
{
	pointer = new int [M.m*M.n];
	m=M.m;
	n=M.n;
	for (int i=0; i<m*n; i++)
			*(pointer+i)=*(M.pointer+i);
}

void Matrix::displayMatrix ()
{
	int newLine=n-1;
	for (int i=0; i<m*n; i++)
	{
		cout <<setw(4)<<*(pointer+i);
		if (i == newLine)
		{
			cout <<endl;
			newLine+=n;
		}
	}
}

int* Matrix::getMember (int i, int j) const
{
	if (i*j <= 0)
	{
		cout <<"error : getMember - row and column index are positive integers"<<endl;
		exit (0);
	}
	else
		return (pointer + (i-1)*(n)+(j-1));
}
void Matrix::setMatrix ()
{
	int row=1;
	int column=1;
	int newLine = n-1;
	for (int i=0; i<m*n; i++)
	{
		cout <<"matrix["<<row<<"]["<<column<<"]= ";
		column++;
		if (i==newLine)
		{
			column=1;
			++row;
			newLine+=n;
		}
		cin >>*(pointer +i);
	}
}

void Matrix::operator = (const Matrix& right)
{
	if (m==right.m && n==right.n)
		for (int i=0; i<m*n; i++)
			*(pointer +i)=*(right.pointer+i);
	else 
	{
		cout <<" error : operator= - matrix dimensions must be equal. "<<endl;
	}
}
Matrix Matrix::operator +(const Matrix& right)
{
	
	Matrix temp (right.m, right.n);
	for (int i=0; i<m*n; i++)
		*(temp.pointer+i)=*(pointer+i)+*(right.pointer+i);
	return temp;
}
Matrix Matrix::operator *(const Matrix& right)
{

	if (n==right.m)
	{
		Matrix temp(m,right.n);
		Matrix current = *this;
		for (int i=0; i<temp.m; i++)
			for (int j=0; j<temp.n; j++)
			{
				int sum =0;
				for (int k=0; k<right.m; k++)
				{
					sum+=*current.getMember(i+1,k+1)* *right.getMember(k+1,j+1);
				}
				*temp.getMember(i+1,j+1)=sum;
			}
		return temp;
	} else
	{
		cout <<"error : operator * - matrix sizes bad : A[m,k]*B[k,n]=C[m,n] , m,n,k"
			<<"- number of rows and columns."<<endl;
		exit (0);
	}
}	
Matrix Matrix::operator -(const Matrix& right)
{
	Matrix temp (right.m, right.n);
	for (int i=0; i<m*n; i++)
		*(temp.pointer+i)=*(pointer+i)-*(right.pointer+i);
	return temp;
}
void Matrix::nulify ()
{
	for (int i=0; i<m*n; i++)
		*(pointer +i)=0;
}

//tell me if this is any good (or bad :) - be gentle

ok, i have just noticed that the text is really bad : there is no spaces and all of the structure is gone. i pasted it from MS Visual studio. here's a question:

how can i post my code as I see it in MS Visual Studio? with all it's structure and coolness?

>there is no spaces and all of the structure is gone.
That's because you ignored the multiple notifications and instructions the forum automatically gives you on using code tags. I'll add code tags for you this time, but feel free to consider this your warning to use them next time. We specify this in the rules, announcements, and stickies. I suggest you read them before posting again.

ok, i consider myself warned.
still, i have some questions:
1) what is the point of solving practice problems if nobody coments on your code? i've been searching this forum and, correct me if i'm wrong, nobody has solved any problems from the practice problems sticky, besides me and i know my problem can be improved because i found some "errors" myself.

>what is the point of solving practice problems if nobody coments on your code?
Practice problems aren't for other people, they're for you. You solve them to practice your skills and gain insights. If someone comments on your code, that's merely icing on the cake.

Generally you learn more by doing. The fact that you found some of your own errors means that you're learning, and the fact that you acknowledge your code needs improvement means that you'll push to improve it. I'm afraid I don't see what your problem is.

Concerning your code: at a glance I don't see any glaring errors and the code is consistent and well formed. To pick out any real problems I would have to go out of my way to test and debug it. Presumably you haven't gotten any comments because others came to that conclusion as well and didn't have the time to properly critique your program.

ok, i've been learning c++ on my own for a year now intensily (my mechanical engineering college doesn't have any courses in programming, and that's a problem for me because i'll be making my Phd in computer simulations if things work out), from a number of books. i guess i misunderstood the purpose of the online community. i know my program "works" but it uses too much memory (i did a silly thing with %class type% current = *this somewhere). i'm merely saying that making a practice program work is not the same as making it run efficiently and that is what makes a difficult problem an impossible one for a newbie like me, without guidance. that's why i said i've misunderstood the purpose of online community, because that guidance should be searched for in books, for larger programs at least. IT forums are great for general IT and syntax advice. my bad. :) thank you for your advice and time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.