GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-Feb-2007, 07:52
bengaluke's Avatar
bengaluke bengaluke is offline
New Member
 
Join Date: Sep 2006
Location: Hustonville, KY
Posts: 15
bengaluke is on a distinguished road

C++ Matrix


Please help me with my matrices program. I can't seem to get it to multiply, addition and subtraction work fine. Here's my code

CPP / C++ / C Code:
matrix operator *(matrix b)
{
 matrix ans(r, b.c);	//Allows us to use rows and columns
 float t;
 int i, j, k;

 for (i = 1; i <= r; i++)
{
     for (j = 1; j <= c; j++)
     {
         t = 0;

         for (k = 1; k <= b.r; k++)
         {
              t = t + (mat[k][j] * b.mat[i][j]);
         }

        ans.mat[i][j] = t;
      
     }
    }
     return ans;
}

Any help will be appreciated.
  #2  
Old 02-Feb-2007, 10:45
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,702
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: C++ Matrix


Quote:
Originally Posted by bengaluke
Please help me ... get it to multiply
CPP / C++ / C Code:
      t = t + (mat[k][j] * b.mat[i][j]);

Given matrices
left, which has n rows and x columns
and
right, which has x rows and m columns,

then


if you want product = left times right

the product matrix has n rows and m columns

Each element in the product matrix is defined by:
Code:
product[i][j] = sum over k of (left[i][k] * right[k][j])

Where k is equal to the number of columns of left (and, therefore, is equal to the number of rows of right).

Regards,

Dave
  #3  
Old 03-Feb-2007, 11:22
bengaluke's Avatar
bengaluke bengaluke is offline
New Member
 
Join Date: Sep 2006
Location: Hustonville, KY
Posts: 15
bengaluke is on a distinguished road

Re: C++ Matrix


Thanks for the tip(it does semi-work now), but my real problem is that when I try to multiply a 3x1 and 1x3, I don't get a 3x3. If you know anyway to fix this, then I think it would work correctly.
  #4  
Old 03-Feb-2007, 13:26
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,702
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: C++ Matrix


Quote:
Originally Posted by bengaluke
it does semi-work now

Either it works or it doesn't work. If it gives expected results for some cases and not for others, then, by my definition: It Doesn't Work.

Your original code worked only for two square matrices of the same size.

Since you haven't shown what changes you made (if any), how could I guess why it doesn't work now?

If you haven't made any changes, then go back and look at my description and check the ranges of the loop variables in the multiplication function.

By the way, you have loops that like this:

CPP / C++ / C Code:
for (i = 1; i <= r; i++)

and
CPP / C++ / C Code:
for (j = 1; j <= c; j++)


I don't know how you defined your matrix but in "normal" C (and C++) programs I would expect to see them like the following:
CPP / C++ / C Code:
for (i = 0; i < r; i++)
and
CPP / C++ / C Code:
for (j = 0; j < c; j++)

I can't say that yours are wrong, since I don't know where you got the memory from and it is certainly possible to do a little trick with pointers from dynamically allocated arrays so that they can look like that.


This is done in the infamous "Numerical Recipes in C" book --- written, apparently by a bunch of old FORTRAN programmers. The stuff actually works pretty much as advertised, but just irritates the hell out of most C programmers that I know, and isn't really satisfying to most of the old FORTRAN programmers that I have known.

Anyhow, if you show the code that you used, and give an example of what you tried and what the program gave as a result, maybe someone can help you figure it out.

Regards,

Dave
  #5  
Old 03-Feb-2007, 16:20
bengaluke's Avatar
bengaluke bengaluke is offline
New Member
 
Join Date: Sep 2006
Location: Hustonville, KY
Posts: 15
bengaluke is on a distinguished road

Re: C++ Matrix


O.K. Dave, the code is below, my problem is trying to multiply matrices of anything different sizes, which you stated earlier. If you could do me a favor it would be highly appreciated, when reading my code, about my program, don't critique the minor things, look at the big picture.

CPP / C++ / C Code:

#include <iostream.h>
#include <stdlib.h>
#include <conio.h>

class matrix
{
	private:
		int r, c;	//r - rows, c - columns
		float mat[11][11];
		int errcode;
	public:
		matrix () : r(0), c(0), errcode(1) {  } //Default undefinded
		matrix (int rows, int coulmns) : r(rows), c(coulmns), errcode(0) {  }  
		//Error codes 
		//0 - No error, 1 - undefinded, 2 - size mismatch, 3 - dervied error
		void input()
		{
			char change;
			int i;
			
			if (r != 0 && c != 0)
			{
				cout << "There are " << r << " rows and " << c << " coulmns\n";
				cout << "Do you wish to change it? ";
				cin >> change;
				if (change == 'Y' || change == 'y')
				{
					cout << "How many rows are there? ";
					cin >> r;
					cout << "How many cols are there? ";
					cin >> c;					
				}

				int q = 1;
				while (q <= r)
				{
					cout << "Enter row " << q << ": ";
					for (i = 1; i <= c; i++)	//If the i is equal to the row then exit the loop
					{	
						cin >> mat[q][i]; 
					}	//End for
					q++;
				}	//End while
			} //End if
			else
			{
				cout << "A matrix cannot equal 0\n";
			}	//End else
		}	//End input()

		void print()
		{
			int i;
			int q = 1;
			while (q <= r)
			{
				cout << "[ ";
				for (i = 1; i <= c; i++)	//If the i is equal to the row then exit the loop
				{
					cout << mat[q][i] << " "; 
				} //End for
				q++;
				cout << "]\n";
			} //End while
		} // End print()

		matrix operator +(matrix b)
		{
			//matrix addition
			matrix ans(r, c);

			for (int i = 1; i <= r; i++)
			{
				for (int j = 1; j <= c; j++)
				{
					ans.mat[i][j] = mat[i][j] + b.mat[i][j];		//Add rows and cols of matrices
				}
			}
			return ans;
		}

		matrix operator -(matrix b)
		{
			matrix ans(r, c);

			for (int i = 1; i <= r; i++)
			{
				for (int j = 1; j <= c; j++)
				{
					ans.mat[i][j] = mat[i][j] - b.mat[i][j];		//subtract rows and cols of matrices
				}
			}

			return ans;
		}

		matrix operator *(matrix b)
		{
			matrix ans(r, b.c);	//Allows us to use rows and columns.
			float t;
			int i, j, k;

			for (i = 1; i <= r; i++)
			{
				for (j = 1; j <= c; j++)
				{
					t = 0;

					for (k = 1; k <= b.r; k++)
					{
						
						t += ((mat[i][k]) * (b.mat[k][j])); //multiplies matrices
					}

					ans.mat[i][j] = t;
				}
			}

			return ans;
		}

	


};		//ends matrix class


void main()
{ 

	matrix a(2, 2), b(2, 2), c;

	int ch = 0;
	do 
	{
		cout << "Welcome to the matrix program.\n";
		cout << "Choose one: \n";
		cout << "1) Input Matrix #1\n";
		cout << "2) Input Matrix #2\n";
		cout << "3) Add\n";
		cout << "4) Subtract\n";
		cout << "5) Multiply\n";
		cout << "6) Help\n";
		cout << "7) Exit\n";
		cin >> ch;

		if (ch == 1) 
		{
			a.input();
			a.print();		//get matrix a
		}
		else if (ch == 2) 
		{
			b.input();
			b.print();		//get matrix b
		}
		else if (ch == 3)
		{
			c = a + b;		//addtion
			c.print();
		}
		else if (ch == 4)

		{
			c = a - b;		//subtract
			c.print();
		}
		else if (ch == 5)
		{
			c = a * b;
			c.print();
		}
		else if (ch == 6)
		{
			system("cls");
			cout << "Enter matrix 1 and 2 then add, subtract, or multiply\n";
			cout << "NOTE: When multipling matrices that are not of the \n";
			cout << "same size you must enter them as the same size and \n";
			cout << "fill in the spaces with zeros(0)\n\n";
		}
		else
		{
			//Do not do anything
		}

	} 
	while (ch != 7);

}		//ends my main program

//End the matrix.cpp file

Regards,

Me
  #6  
Old 03-Feb-2007, 17:35
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,702
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: C++ Matrix


Quote:
Originally Posted by bengaluke
O.K. Dave, the code is below, my problem is trying to multiply matrices of anything different sizes,

OK. Without editorial comment, I will go over the contents of the multiplication function. You got it "almost" right.


What is the sizeof ans.mat? It has r rows and b.c columns. So your call to the constructor looks good.


Now, you want to calculate ans.mat[i][j] for all rows and all columns of ans.

So you have a loop for i and a nested loop for j and finally an inner loop for k that does the calculation for each element of the product matrix.

What is the range of i? It should go from 1 to r
What is the range of j? It should go from 1 to b.c

Finally, the inner loop: for each value of i and j, the inner loop adds up the product of the column elements of the left hand and the corresponding row element of the right hand side.
I see that you have corrected the subscripts on the product terms, and you only have to check the loop limit for k.

What is the range of k? It should go from 1 to c
(or you could say that it should go from 1 to b.r since c is equal to b.r).

Code:
Welcome to the matrix program. Choose one: 1) Input Matrix #1 2) Input Matrix #2 3) Add 4) Subtract 5) Multiply 6) Help 7) Exit 1 There are 2 rows and 2 coulmns Do you wish to change it? y How many rows are there? 3 How many cols are there? 1 Enter row 1: 1 Enter row 2: 2 Enter row 3: 3 [ 1 ] [ 2 ] [ 3 ] Welcome to the matrix program. Choose one: 1) Input Matrix #1 2) Input Matrix #2 3) Add 4) Subtract 5) Multiply 6) Help 7) Exit 2 There are 2 rows and 2 coulmns Do you wish to change it? y How many rows are there? 1 How many cols are there? 3 Enter row 1: 4 5 6 [ 4 5 6 ] Welcome to the matrix program. Choose one: 1) Input Matrix #1 2) Input Matrix #2 3) Add 4) Subtract 5) Multiply 6) Help 7) Exit 5 [ 4 5 6 ] [ 8 10 12 ] [ 12 15 18 ] Welcome to the matrix program. Choose one:


Regards,

Dave
  #7  
Old 03-Feb-2007, 20:14
bengaluke's Avatar
bengaluke bengaluke is offline
New Member
 
Join Date: Sep 2006
Location: Hustonville, KY
Posts: 15
bengaluke is on a distinguished road
Lightbulb

Re: C++ Matrix


Thank you so much, you don't know how much I appreciate your help Thanks for helping me get it to work!

P.S.
Sorry for being a jerk earlier.
  #8  
Old 03-Feb-2007, 21:38
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,702
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: C++ Matrix


Quote:
Originally Posted by bengaluke
P.S.
Sorry for being a jerk earlier.

Did I miss something? I thought I was the jerk. Oh, well...

Regards,

Dave
 
 

Recent GIDBlogHalfway done! by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
i need help in C++ PLZ its_me C++ Forum 3 04-Dec-2006 21:51
how to initialize a char matrix matrix with empty elements? swedenguy C Programming Language 4 22-Aug-2006 11:43
how to initialize a char matrix matrix with empty elements? swedenguy C Programming Language 1 22-Aug-2006 11:15
debugging multi-dimensional vector/ matrix class counterflow C++ Forum 1 15-Mar-2006 12:14
Combining Vectors and References Frankg C++ Forum 7 14-Jan-2006 06:17

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:07.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.