multiplying matrices

This is a discussion on multiplying matrices within the C++ Programming forums, part of the General Programming Boards category; i've written a program that will multiply two matrices, and as of right now it outputs the answers all in ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    11

    multiplying matrices

    i've written a program that will multiply two matrices, and as of right now it outputs the answers all in column..I was attempting to be able to output the answers in another matrix, matrix C, but I have no idea how to even get around to doing that. I did all I knew, which was to declare the matrix..other than that im stuck
    ...any suggestions to help me out?


    my code thus far:

    Code:
    
                                                                                                                                                                                                                                                                 
    include <iostream>			//library for basic
    #include <math.h>
    
    
    
    using namespace std;
    
    //declare functions
    void matrixA();
    void matrixB();
    void multiply();
    void validityCheck();
    //declare variables                                                  
    int arrayB[100][100];
    int i,j,num,k=1;
    		//matrix B variables
    int arrayA[100][100];
    int row,col,numb=1;
    int sum;
    int answer[10][10];
    
    
    
    int main()
    {
    matrixA();
    matrixB();
    multiply();
    }
    
    /*------------------------------My Functions----------------------------*/
    
    void matrixA()
    {
    
    //creates first matrix
    
    cout<< "Matrix A "<<endl;
    	
    	for(i=1;i<=2;i++)
    	{
    		for(j=1;j<=2;j++)
    		{
    			arrayA[i][j]=num;
    			num=num+1;
    		}
    	}
    	
    
    for(i=1;i<=2;i++)
    	{
    		for(j=1;j<=2;j++)
    		{
    			cout.width(5);
    			cout<<arrayA[i][j];
    		}
    		cout << endl;
    	}
    cout<<"                                                   "<<endl;
    
    }
    
    void matrixB()
    {
    
    //creates second matrix
    
    cout<< "Matrix B"<<endl;
    
    	for(i=1;i<=2;i++)
    	{
    		for(j=1;j<=2;j++)
    		{
    			arrayB[i][j]=num;
    			num=num+1;
    		}
    	}	
    	
    
    for(i=1;i<=2;i++)
    	{
    		for(j=1;j<=2;j++)
    		{
    			cout.width(5);
    			cout<<arrayB[i][j];
    		}
    		cout << endl;
    	}
    }
    
    
    
    
    void multiply()
    {
    
    	for(i=1;i<=2;i++)
    	{
    	 	for(j=1;j<=2;j++)
    	 	{
    	 	
    	 	
    	 		for(k=1;k<=2;k++)	
    	 		{
    	 		sum=sum+(arrayA[i][k]*arrayB[k][j]);	
    	 		}
    answer[i][j]=sum;
    	cout<<sum<<" sum "<<endl;
    	sum=0;
    
    	 	}
    	
    	}
    	
    
    }

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Code:
    void multiply()
    {
    	for(i=1;i<=2;i++)
    	{
    	 	for(j=1;j<=2;j++)
    	 	{
    	 		for(k=1;k<=2;k++)	
    	 		{
    	 			sum=sum+(arrayA[i][k]*arrayB[k][j]);	
    	 		}
    			
    			answer[i][j]=sum;
    			cout<<sum<<" sum "<<endl;
    			sum=0;
    	 	}
    	}
    }
    1) Are you aware that arrays have zero based indexes, i.e. they start at index 0? The rows start at index 0, and the columns in every row start at index 0. You are skipping the entire first row of a matrix as well as the first spot in every row, which is a waste of memory--although only a tiny bit is wasted.

    2) You don't need three for-loops to multiply a couple of 2 x 2 matrices together--you should only need two for-loops. I assume you want to multiply each value in one matrix by the value in the same spot in the other matrix. Let's take a look at what your three loops do:
    Code:
    i=1
    	j=1
    		k=1
    			arrayA[1,1] * arrayB[1,1] //ok
    		k=2
    			arrayA[1,2] * arrayB[2,1] //??
    	j=2
    		k=1
    			arrayA[1,1] * arrayB[1,2] //??
    		k=2
    			arrayA[1,2] * arrayB[2,2] //??
    
    i=2
    etc., etc.
    Is that what you want?

    3) To output an array to look like a matrix, in your case, use two for-loops. The outer loop will specify the row, and the inner loop will specify the column in the row. You can use cout in the inner loop to output the value at that position, and follow it with a space: " ". Just after the closing bracket of the inner for loop, which is before the outer loop has a chance to increment the row, use cout to start a newline: cout<<endl;
    Last edited by 7stud; 03-22-2005 at 10:57 PM.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    ???????
    Code:
    int c[100][100];
    for(int i = 0; i < 100; i++)
         for(int j = 0; j < 100; j++)
                c[i][j] = a[i][j] * b[i][j];
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overflow multiplying matrices
    By Logicbynumbers in forum C Programming
    Replies: 11
    Last Post: 11-17-2008, 05:53 PM
  2. adding matrices, help with switches
    By quiet_forever in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2007, 08:21 AM
  3. What is a matrix's purpose in OpenGL
    By jimboob in forum Game Programming
    Replies: 5
    Last Post: 11-13-2004, 11:19 PM
  4. Multiplying matrices
    By Star Lancer in forum C++ Programming
    Replies: 7
    Last Post: 05-22-2003, 06:07 AM
  5. Problem multiplying rotation matrices together
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 03-04-2003, 08:20 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21