Hey everyone,
Im working on a program for school that requires me to do mathematic functions of 2 Matrices and outputting into a 3rd Matrix (which gets overwritten once it is outputted to screen).
I have to use a MATRIX class, and I understand how to do the mathematics work for each function (add, subtract, multiply). I have the
array initialization and declaration working, and the
displaying of the first 2 matrices working, but I'm having problems getting my first function to work.
If you notice my code, I am using a class and my constructor is not working. I need to be able to say:
Result.FUNCTION(Mat1, Mat2); //Where FUNCTION will be AddMat, SubtMat, etc.
but this isn't working for me at the moment. Can you tell me what is wrong with my constructor and class function AddMat?
I'd rather not use pointers(and I don't think I NEED to), but how do i go about putting the value for each step of AddMat-- which will done using a for loop--into Mat3? I THINK I can just say coordinate of Mat1 + coordinate of Mat2 = value, and then put value into coordinate of Mat3, and then return VOID at the end of AddMat. Is this legal?
Please help! I have another half-week to finish this, and although it doesnt seem TOO difficult, I can't even get my constructor/functions working.
Code:
CODE
#include <cstdlib>
#include <iostream>
using namespace std;
//variables
int col;
int row;
int Mat1[3][3];
int Mat2[3][3];
int Mat3[3][3];
class MATRIX {
public:
Result(void);//constructor
int AddMat(int Mat1, int Mat2);
};
int MATRIX::AddMat(int Mat1, int Mat2)
{
int M1 = Mat1;
int M2 = Mat2;
cout << "Working"<<endl; //TESTING PURPOSES ONLY
}
int main()
{
//Input Matrix1 Values
cout << "Matrix 1" <<endl;
for(int x=0; x<3; x++)
{
for(int y=0; y<3; y++)
{
cout << "Enter a value for Row " << x << ", Column " << y << ": ";
cin >> Mat1[x][y];
}
}
//Input Matrix2 Values
cout << "Matrix 2" <<endl;
for(int x=0; x<3; x++)
{
for(int y=0; y<3; y++)
{
cout << "Enter a value for Row " << x << ", Column " << y << ": ";
cin >> Mat2[x][y];
}
}
//Display Mat1
for(int x=0; x<3; x++)
{
for(int y=0; y<3; y++)
{
cout << Mat1[x][y] << " ";
if (y == 2)
{
cout << "" << endl;
}
}
}
//Display Mat2
for(int x=0; x<3; x++)
{
for(int y=0; y<3; y++)
{
cout << Mat2[x][y] << " ";
if (y == 2)
{
cout << "" << endl;
}
}
}
MATRIX Result();
Result.AddMat(Mat1, Mat2);
system("PAUSE");
return 0;
}