3x3 Matrix Multiplication HELP!!!

Hi, I'm knew at these so I need a little help with a program I am making, I need too multiply 2 matrixes 3x3, but i dont know what im doing wrong. These is what i have so far.

[code]#include
int a[3][3];
int b[3][3];
int c[3][3];
int i,j,k;
void main()
{
clrscr();
for(i=0;i<=2;i++)
for(j=0;j<=2;j++)
{
printf("A(%d,%d)= ",i+1,j+1);
scanf("%d",a[i][j]);
}
printf("
");
for(i=0;i<=2;i++)
for(j=0;j<=2;j++)
{
printf("B(%d,%d)= ",i+1,j+1);
scanf("%d",b[i][j]);
}
printf("
");
for(i=0;i<=2;i++)
{
for(k=0;k<=2;k++)
{
for(j=0;j<=2;j++)
{
c[i][k]=a[i][j]*b[j][k];
printf("%3d ",c[i][k]);
}
printf("
");
}
}
getch();
}[/code]

Comments

  • : Hi, I'm knew at these so I need a little help with a program I am
    : making, I need too multiply 2 matrixes 3x3, but i dont know what im
    : doing wrong. These is what i have so far.
    :

    The problem is in your last loop. Each c[i][k] is printed for each j (thus 3 times).

    For 2x2 matrices:
    [code]
    1 2 5 6 1*5 + 2*7 1*6 + 2*8
    3 4 x 7 8 = 3*5 + 4*7 3*6 + 4*8
    [/code]

    So:

    [code]
    // This is here to stop the code-block formatting from bugging up
    [/code]

    [code]
    // Outer two loops for printing each c[i][j]
    for (i = 0; i < 3; i++)
    {
    for (j = 0; j < 3; j++)
    {
    // Calculate c[i][j], which is a[i] * b[][j]
    // or the i-th row of A times the j-th column of B

    //Inner loop for each item in the i-th row of A
    //C doesn't initialize anything for us, so start of
    // 0 so we can use + in the loop
    c[i][j] = 0;
    for (k = 0; k < 3; k++)
    {
    c[i][j] += a[i][k] * b[k][j];
    }

    //Here is the place to print c[i][j]
    ...
    }
    }
    [/code]

    Which is almost the code you've got, but I exchanged j with k (which is more readable), and furtherly summed up the a*b parts, which you forgot.

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
  • Hi, me again I did all the changes but now I get a 3x3 matrix full of 0's, I was wondering where my mistake was?
    Here is the new code.

    [code]#include
    int a[3][3];
    int b[3][3];
    int c[3][3];
    int i,j,k;
    void main()
    {
    clrscr();
    for(i=0;i<=2;i++)
    for(j=0;j<=2;j++)
    {
    printf("A(%d,%d)= ",i+1,j+1);
    scanf("%d",a[i][j]);
    }
    printf("
    ");
    for(i=0;i<=2;i++)
    for(j=0;j<=2;j++)
    {
    printf("B(%d,%d)= ",i+1,j+1);
    scanf("%d",b[i][j]);
    }
    printf("
    ");
    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];
    }
    printf("%3d ",c[i][j]);
    }
    printf("
    ");
    }
    getch();
    }[/code]
  • Hi,

    All arguments to [italic]scanf[/italic] must be pointers so the function can know where to save the input. Your code is sending the value of [italic]a[i][j][/italic] and [italic]scanf[/italic] is interpreting that value as a pointer. This will most likely result in the program crashing with a segmentation fault. Use:

    [code]
    scanf("%d", &a[i][j]);
    [/code]

    Regards,
    Michael
    michaelgallagher.au@gmail.com
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories