Thread: How do I find product of the matrices?

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    254

    How do I find product of the matrices?

    Hi

    I'm trying to learn to find the product of two matrices using arrays. Please help me. I'm really stuck. And please don't introduce those topics which I won't be able to understand (I'm a beginner). In other words, please keep it simple. Thanks.

    Code:
    // product_of_two_matrices_1x3_and_3x1.cpp
    // read two matrices and find product
    
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    int main()
    {
            float m1[1][3];
            float m2[3][1];
            float m3[1][3];
            int r, c;
    
            cout << "Enter matrix #1 below\n\n";
    
            for ( r=0; r<1; r++)
            {
                    for (c=0; c<3 ; c++)
                    {
                            cout << "enter entry for row #" << (r+1) << " and column"
                                 << " #" << (c+1) << ": ";
                            cin >> m1[r][c];
                    }
            }
    
            cout << "\n\nEnter matrix #2 below\n\n";
    
            for ( r=0; r<3; r++)
            {
                    for (c=0; c<1; c++)
                    {
                            cout << "enter entry for row #" << (r+1) << " and column"
                                 << " #" << (c+1) << ": ";
                            cin >> m2[r][c];
                    }
            }
    
            cout << "\n\nproduct of matrix #1 and matrix #2 is given below\n\n";
    
            //How do I find product of the matrices?
    
            return 0;
    
    }
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Do you know how to multiply matrices on paper?

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    17
    ^^ +1 I was just going to say that.

    If you do not know how then check this out:
    4. Multiplication of Matrices

    Or maybe use Paul's Online Notes, another excellent resource. The basics look like a nested for loop with a running sum and the loops controlling the rows/columns. I'd write it out more but I'm at work :P If someone hasn't helped you with it in a couple hours I will write it out ha

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by tabstop View Post
    Do you know how to multiply matrices on paper?
    Yes.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Then you just need to convert that to code. Write down the steps you use to do the multiplication. Look for repeated steps that are almost the same, but differ only in which row/column of the original you're using, and put those in a loop.

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Lahore, Pakistan, Pakistan
    Posts
    2
    Code:
    #include<iostream.h>
    
    void input(int t[][3]);
    
    void product(int a[][3], int b[][3], int c[][3]);
    
    void display(int [][3]);
    
    void main()
    
    {
    
    	 int a[3][3], b[3][3], c[3][3];
    
    	 cout << "\tPROGRAM OF PRODUCT OF TWO 3X3 MATRICES\n\nEnter 9 Values for 3x3 Matrix 1 \n";
    
    	 input(a);
    
    	 cout << "Enter 9 Values for 3x3 Matrix 2 \n";
    
    	 input(b);
    
    	 product(a,b,c);
    
    	 cout << "\nProduct of two matrices are \n\n";
    
    	 display(c);
    
    }
    
    void input(int t[][3])
    
    {
    
    	 int i, j;
    
    	 for(i=0; i<3; i++)
    
    	 {
    
    		  for(j=0; j<3; j++)
    
    				cin >> t[i][j];
    
    	 }
    
    
    }
    
    void product(int a[][3], int b[][3], int c[][3])
    
    {
    
    	 int i, j, k;
    
    	 for(i=0; i<3; i++)
    
    	 {
    
    		  for(j=0; j<3; j++)
    
    		  {
    
    				c[i][j]=0;
    
    				for(k=0; k<3; k++)
    
    				{
    
    					 c[i][j] += + a[i][k] * b[k][j];
    
    				}
    
    		  }
    
    	 }
    
    }
    
    void display(int c[][3])
    
    {
    
    	 int i, j;
    
    	 for(i=0; i<3; i++)
    
    	 {
    
    		  for(j=0; j<3; j++)
    
    				cout << c[i][j] << " ";
    
        cout << "\n";
    
    	 }
    
    }
    This is program for multiplying 3x3 matrices.......hope it will help u a lot
    Last edited by Jasir Ali; 06-05-2011 at 10:52 AM.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Thanks a lot, Jasir Ali.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Jasir Ali View Post
    This is program for multiplying 3x3 matrices.......hope it will help u a lot
    Wow Jasir, strike two! I told you in my other post that you need to read the forum guidelines. I linked you to them, and I'll do it again here: forum guidelines (that red text is a link -- click it and read the entire page). You even "liked" my post. Number 1 of the forum guidelines mentions the homework policy (red text again -- click the link and read everything), which states in no uncertain terms this board is not in the business of handing out homework solutions. We're here to help people learn. That is not done by giving out solutions to people, but by letting them to try to solve their problems and giving them a nudge in the right direction when they stray off course. Your code did not help Jackson6612, it only served to cripple him in the long run by ensuring he only learns how to ask for free code on forums, and does not learn to problem solve or learn how to turn solutions into code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cross product?
    By kypronite in forum Game Programming
    Replies: 8
    Last Post: 09-07-2008, 11:15 AM
  2. Search a product using STD::SET find()
    By IndioDoido in forum C++ Programming
    Replies: 11
    Last Post: 11-01-2007, 04:16 PM
  3. which flash product?
    By iain in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 03-21-2005, 01:55 PM
  4. Dot Product.......Help
    By incognito in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 03-07-2003, 08:49 AM