3 Replies - 8146 Views - Last Post: 12 March 2012 - 06:52 AM Rate Topic: -----

#1 Jeet.in  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 304
  • Joined: 30-May 11

3x3 Matrix Multiplication

Posted 12 March 2012 - 05:20 AM

Today our instructor explained on how to write a matrix multiplication program in C. Although I wrote a 3x3 code, the results were not as intended. Where hace I gone wrong in codeing/Logic?

#include <stdio.h>
#include <stdlib.h>
int main()
{
int mat1[3][3], mat2[3][3], i, j, k, mul[3][3], sum ;
printf("Please enter the numbers row-wise: \n\n");
for(i=0; i<3; i++)
for(j=0; j<3; j++)
{
    scanf("%d", &mat1[i][j]);
}
printf("Please enter the numbers of next matrix\n\n");
for(i=0; i<3; i++)
for(j=0; j<3; j++)
{
    scanf("%d", &mat2[i][j]);
}
//Multiplication starts
for(i=0; i<3; i++)
{
    for(j=0; j<3; j++)
    {
        for(k=0; k<3; k++)
        {
            sum=sum+mat1[i][k]*mat2[k][j];
            mul[i][j]=sum;
            sum=0;
        }
    }
}
printf("Product of entered matrices: \n");
for (i=0; i<3; i++)
      {
         for (j=0; j<3; j++)
         printf("%d\t", mul[i][j]);
         printf("\n");
      }
      system("PAUSE");
return 0;
}



Output:

Posted Image

Please help. Thanks !

Is This A Good Question/Topic? 0
  • +

Replies To: 3x3 Matrix Multiplication

#2 Bryston  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 125
  • Joined: 24-January 12

Re: 3x3 Matrix Multiplication

Posted 12 March 2012 - 05:53 AM

Are you sure your data is going into the array in the way you intended?

Here is an example of a nested loop for a 3x3 array for comparison.
#include<iostream>
#include<ctime>
using namespace std;

int main(){
    srand (time(NULL));
    
    int our_matrix[3][3];
    //load the matrix
    for(int i = 0; i < 3; ++i){// outer loop
        for(int j = 0; j < 3; ++j){// inner loop
           our_matrix[i][j] = rand()%100;// stuff in a random number
        }        
    }
    
    // check to make sure the data is there
    for(int i = 0; i < 3; ++i){// outer loop
        for(int j = 0; j < 3; ++j){// inner loop
           cout << our_matrix[i][j] << " : ";// show the numbers we plugged in earlier
        }
        cout << endl;        
    }
return(0);
}


Was This Post Helpful? 1
  • +
  • -

#3 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2102
  • View blog
  • Posts: 3,207
  • Joined: 21-June 11

Re: 3x3 Matrix Multiplication

Posted 12 March 2012 - 06:20 AM

for(k=0; k<3; k++)
{
    sum=sum+mat1[i][k]*mat2[k][j];
    mul[i][j]=sum;
    sum=0;
}



Think about what this is doing. The first time this runs, sum is set to mat1[i][0]*mat2[0][j] plus the old value of sum (which at this point is uninitialized, so we've got undefined behavior right there). Then mul[i][j] is set to the value of sum. After that sum is set to 0 and the loop runs again.

The second time sum is set to mat1[i][1]*mat2[1][j] (plus the old value of sum which is 0 because you just set it to 0). Now that value is stored in mul[i][j] and the old value is overridden. Again sum is set to 0 afterwards.

The third time the same thing happens with mat1[i][1]*mat2[1][j]. Again we're overriding the old value of mul[i][j], so this will be the final value in mul[i][j]. The previously calculated values have no effect on the end result. Clearly this is not what you want.

You should set sum to 0, once before the loop and you should store the result once after the loop. You shouldn't do these things each time inside the loop.

Quote

sh: PAUSE: not found


This can be easily fixed by removing the (rather annoying) system("PAUSE"); line.

This post has been edited by sepp2k: 12 March 2012 - 06:21 AM

Was This Post Helpful? 1
  • +
  • -

#4 Jeet.in  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 304
  • Joined: 30-May 11

Re: 3x3 Matrix Multiplication

Posted 12 March 2012 - 06:52 AM

Trying again. Thanks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1