4 Replies - 1062 Views - Last Post: 10 March 2011 - 09:04 AM Rate Topic: ***** 1 Votes

#1 Dipesh Agrawal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-March 11

Matrix Multiplication

Posted 09 March 2011 - 09:31 AM

Im facing error with this program. this code is not giving the right answer. like when im multiplying 2 3 matrix with 3 2, it not operates with last one. like if i have 1st row(1 2 3) and 1st column (4 5 6)(suppose this is a column) then it multiplies with 1*4 & 2*5 but doesnt multiply with last one 3*6. this is my error point.*/
# include "stdio.h"
main()
{
    int m1[10][10],i,j,k,m2[10][10],mult[10][10],r1,c1,r2,c2;
    printf("Enter number of rows and columns of first matrix (less than 10)\n");
    scanf("%d%d",&r1,&c1);
    printf("Enter number of rows and columns of second matrix (less than 10)\n");
    scanf("%d%d",&r2,&c2);
    if(r2==c1)
    {
        printf("Enter rows and columns of First matrix \n");
        printf("Row wise\n");
        for(i=0;i<r1;i++)
            for(j=0;j<c1;j++)
                scanf("%d",&m1[i][j]);
        printf("First Matrix is :\n");
        for(i=0;i<r1;i++)
        {
            for(j=0;j<c1;j++)
                printf("%d\t",m1[i][j]);
            printf("\n");
        }
        printf("Enter rows and columns of Second matrix \n");
        printf("Row wise\n");
        for(i=0;i<r2;i++)
            for(j=0;j<c2;j++)
                scanf("%d",&m2[i][j]);
        printf("Second Matrix is:\n");
        for(i=0;i<r2;i++)
        {
            for(j=0;j<c2;j++)
                printf("%d\t",m2[i][j]);
            printf("\n");
        }
        printf("Multiplication of the Matrices:\n");
        for(i=0;i<r1;i++)
        {
            for(j=0;j<c2;j++)
            {
                mult[i][j]=0;
                for(k=0;k<r1;k++)
                    mult[i][j]+=m1[i][k]*m2[k][j];
                printf("%d\t",mult[i][j]);
            }
            printf("\n");
        }
    }
    else
    {
        printf("Matrix multiplication cannot be done");
    }
    return 0;
}

This post has been edited by JackOfAllTrades: 09 March 2011 - 12:23 PM
Reason for edit:: Added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Matrix Multiplication

#2 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 943
  • View blog
  • Posts: 2,353
  • Joined: 15-February 11

Re: Matrix Multiplication

Posted 09 March 2011 - 09:35 AM

edit your post and then place the code in [ code ]/*code here*/[/ code ] tags
Was This Post Helpful? 0
  • +
  • -

#3 Dipesh Agrawal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-March 11

Re: Matrix Multiplication

Posted 09 March 2011 - 09:41 AM

Im facing error with this program. this code is not giving the right answer. like when im multiplying 2 3 matrix with 3 2, it not operates with last one. like if i have 1st row(1 2 3) and 1st column (4 5 6)(suppose this is a column) then it multiplies with 1*4 & 2*5 but doesnt multiply with last one 3*6. this is my error point.
# include "stdio.h"
main()
{
int m1[10][10],i,j,k,m2[10][10],mult[10][10],r1,c1,r2,c2;
printf("Enter number of rows and columns of first matrix (less than 10)\n");
scanf("%d%d",&r1,&c1);
printf("Enter number of rows and columns of second matrix (less than 10)\n");
scanf("%d%d",&r2,&c2);
if(r2==c1)
{
printf("Enter rows and columns of First matrix \n");
printf("Row wise\n");
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
scanf("%d",&m1[i][j]);
printf("First Matrix is :\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
printf("%d\t",m1[i][j]);
printf("\n");
}
printf("Enter rows and columns of Second matrix \n");
printf("Row wise\n");
for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
scanf("%d",&m2[i][j]);
printf("Second Matrix is:\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
printf("%d\t",m2[i][j]);
printf("\n");
}
printf("Multiplication of the Matrices:\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
mult[i][j]=0;
for(k=0;k<r1;k++)
mult[i][j]+=m1[i][k]*m2[k][j];
printf("%d\t",mult[i][j]);
}
printf("\n");
}
}
else
{
printf("Matrix multiplication cannot be done");
}
return 0;
} */

This post has been edited by JackOfAllTrades: 09 March 2011 - 09:43 AM
Reason for edit:: Fixed code tags

Was This Post Helpful? 0
  • +
  • -

#4 chinchang  Icon User is offline

  • Indie Game Developer
  • member icon

Reputation: 192
  • View blog
  • Posts: 725
  • Joined: 22-December 08

Re: Matrix Multiplication

Posted 09 March 2011 - 11:56 AM

Line 41:
for(k=0;k<r1;k++)


It should r2 or c1.
Was This Post Helpful? 2
  • +
  • -

#5 Dipesh Agrawal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-March 11

Re: Matrix Multiplication

Posted 10 March 2011 - 09:04 AM

Thanks Chinchang for ur support. hope to see u later.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1