Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,423 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,334 people online right now. Registration is fast and FREE... Join Now!




Math regarding Matrices [school work]

 
Reply to this topicStart new topic

Math regarding Matrices [school work], Code Provided -- Urgent

Sonastylol
14 Oct, 2008 - 04:04 PM
Post #1

D.I.C Head
**

Joined: 15 Dec, 2007
Posts: 124


My Contributions
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;
}



User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Math Regarding Matrices [school Work]
14 Oct, 2008 - 05:01 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,208



Thanked: 214 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Well first remember that the constructor function has to have THE SAME NAME as the class. So you can't call your constructor Result(). It has to be called MATRIX().

The next thing is that you will have to define the body of this constructor too like you did for AddMat.

The last thing I want to talk to you about is how you are setting up your functions. You are not going to want to setup functions like AddMat to take in two integers, you are going to want to have AddMat take in ONE parameter and that parameter is also going to be the type of MATRIX.

Now Martyr2, why in the world would I want to go and do that for? Glad you asked! The idea of adding these matrices together is that you want to write code like the following...

cpp

MATRIX Mat1;
MATRIX Mat2;
MATRIX Result;

// Notice here we are adding Mat2 to Mat1 and it results in a new result matrix
Result = Mat1.AddMat(Mat2);


You are going to have a very difficult time trying to loop and pass the two matrices values into a function like you have setup there and come out with something that will work.

So to give you a leg up on this, consider a function signature setup like this...

cpp

// Defined in your matrix class...
MATRIX AddMat(MATRIX Mat);

// In your implementation...

MATRIX MATRIX::AddMat(MATRIX Mat) {
// Here you will take the values from Mat and add them to the current
// object (using the "this" keyword) and return the result as a new MATRIX object
}


Now before you go and get neck deep in that, just confirm with your instructor that is the direction he/she wants you to go. But as for efficiency and OOP at its best, that would be the idea and it would cut your headaches in half.

Good luck. smile.gif

User is online!Profile CardPM
+Quote Post

Sonastylol
RE: Math Regarding Matrices [school Work]
14 Oct, 2008 - 05:16 PM
Post #3

D.I.C Head
**

Joined: 15 Dec, 2007
Posts: 124


My Contributions
Everything you said about taking Mat1.AddMat(Mat2) Makes sense, but I'm having trouble understanding your:

MATRIX Mat1;
MATRIX Mat2;
MATRIX Result;

If I have already established Mat1 and Mat2 as a 2-d array:
int Mat1[3][3];

How can I make this become type MATRIX, and why must I?
Basically, how compatible is what you typed out for me about using functions with my previous code, since I already have the inputting of Matrix 1 and 2 values working.

Thanks for your hasty reply! Gonna grab my dinner from the oven, hope to hear from you soon.

I am currently changing my Result() to MATRIX etc. I will put in the effort if you can help me succeed.

And Im surprised to see that it was you that replied to my topic, I was JUST reading your blog on an alice in wonderland book program. Was neat to read.
User is offlineProfile CardPM
+Quote Post

Sonastylol
RE: Math Regarding Matrices [school Work]
14 Oct, 2008 - 06:19 PM
Post #4

D.I.C Head
**

Joined: 15 Dec, 2007
Posts: 124


My Contributions
Martyr2,

Here is my code as of now. I went through the changes, but I'm not sure how to set up the constructor (what am I constructing anyway)

Since changing my code to suit your ideas, I have lost control over my program. I don't understand how to convert my matrices Mat1 and Mat2 to type MATRIX like you said, along with their values which they are given immediately at run-time.
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:    
MATRIX();//constructor

MATRIX AddMat(MATRIX Mat);
};

MATRIX MATRIX::AddMat(MATRIX Mat)
{
    cout << "Working"<<endl;                        
}
      
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 MATRIX();
    Mat1.AddMat(Mat2);
    system("PAUSE");
    return 0;
}

Error as of now is:
71 C:\Dev-Cpp\Projects\Project2.cpp request for member `AddMat' in `Mat1', which is of non-class type `int[3][3]'

This must have something to do with me having no constructor, as well as not turning Mat1 and Mat2 into type MATRIX.
Once I have this setup properly, I can type up what AddMat actually DOES, and then create the other math functions (identical to AddMat).

Pressed on time and would love to work on this tonight. Please assist!

User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Math Regarding Matrices [school Work]
14 Oct, 2008 - 07:56 PM
Post #5

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,208



Thanked: 214 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
*sigh*.....

cpp


#include <cstdlib>
#include <iostream>
#include <ctime>
using namespace std;

class MATRIX {
public:
int Mat[3][3];

MATRIX();//constructor
MATRIX AddMat(MATRIX Mat);
void printMatrix(); // Print function
};

// Constructor - initialize to all random numbers
MATRIX::MATRIX() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
// Creating random numbers
Mat[i][j] = (rand() % 10) + 1;
}
}
}

MATRIX MATRIX::AddMat(MATRIX MatB)
{
// Create a result matrix
MATRIX result;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
// Add Matrix B to THIS matrix and store in new result
result.Mat[i][j] = Mat[i][j] + MatB.Mat[i][j];
}
}

// Return new result matrix
return result;
}

void MATRIX::printMatrix() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << "Matrix[" << i << "][" << j << "] = " << Mat[i][j] << endl;
}
}
}

int main()
{
// Seed random generator
srand((int)time(NULL));

// Create Matrix 1, show its contents
MATRIX Mat1;
cout << "Mat 1 printing..." << endl;
Mat1.printMatrix();

// Create Matrix 2, show its contents
MATRIX Mat2;
cout << "Mat 2 printing..." << endl;
Mat2.printMatrix();

// Create result matrix
// Add Matrix2 to Matrix1 and store in result
MATRIX result;
result = Mat1.AddMat(Mat2);

// Now show the contents of the result
cout << "Result printing..." << endl;
result.printMatrix();

system("PAUSE");
return 0;
}



Notice now with this format how easy it is to add matrices together? I just call the function of a Matrix, give it another matrix and it adds it and yields a result. I can do this with subtract as well. This will help you especially in the multiply because the result set can have a different dimension than the original. Your task with this now is to make it so that each class has a dynamic size for its "Mat" variable. That will allow you to start yielding matrices that are 30 X 3 or 20 X 6 etc.

Edit: This is essentially the same thing you would do with overloading operators but I figured you haven't gotten that far yet either. This is one step closer for you to that goal.


Good luck!

"At DIC we be matrices loving code ninjas... we also love Avogadro and his ever fantastic, super incredible, highly touted, and wonderfully insane number! We love you Avogadro!" decap.gif

This post has been edited by Martyr2: 14 Oct, 2008 - 08:35 PM
User is online!Profile CardPM
+Quote Post

Sonastylol
RE: Math Regarding Matrices [school Work]
15 Oct, 2008 - 04:13 AM
Post #6

D.I.C Head
**

Joined: 15 Dec, 2007
Posts: 124


My Contributions
You did more than what was requested, so instead of copying and pasting this I was able to learn from it and convert it to my previous code.

Subtraction is working perfectly at the moment.
Gonna try to tackle Multiplication now.

Thanks Martyr2!
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 01:16PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month