0

I need to use array of pointers to multiply the 2 matrices. its working but i m getting the wrong output .If i give m1=n1=m2=n2 = 2 and both the matrices as 1 2 3 4. I get the o/p matrix as 7 17 32 54 while the right answer is 7 10 15 22. PLEASE HELP ME IM STUCK HERE SINCE 2 DAYS...........................................................................

#include <stdio.h>
#include <stdlib.h>

int main()

{

    int m1,n1,m2,n2,i,j,k,sum=0,*a[10],*b[10],*c[10];   
    printf("Enter m1 and n1 : ");
    scanf("%d%d",&m1,&n1);
    printf("Enter m2 and n2 : ");
    scanf("%d%d",&m2,&n2);
    if(n1==m2)
    {
        for(i=0;i<m1;i++)
        {
            a[i] = (int *)malloc(n1*sizeof(int));
        }
        for(i=0;i<m2;i++)
        {
            b[i] = (int *)malloc(n2*sizeof(int));
        }
        for(i=0;i<m1;i++)
        {
            c[i] = (int *)malloc(n2*sizeof(int));
        }
        printf("Enter the first matrix : ");
        for(i=0;i<m1;i++)
        {
            for(j=0;j<n1;j++)
            {
                scanf("%d",(*(a+i)+j));
            }
        }
        printf("Enter the second matrix : ");
        for(i=0;i<m2;i++)
        {
            for(j=0;j<n2;j++)
            {
                scanf("%d",(*(b+i)+j));
            }
        }

        for(i=0;i<m1;i++)
        {
            for(j=0;j<n2;j++)
            {
                for(k=0;k<n2;k++)
                {
                    sum += a[i][k] * b[k][j];
                }
                *(*(c+i)+j) = sum;
            }
        }
        printf("The Resultant Matrix : ");
        for(i=0;i<m1;i++)
        {
            for(j=0;j<n2;j++)
            {
                printf("%d ",*(*(c+i)+j));
            }
        printf("\n");
        }
    }
    else
    printf("Invalid Matrix\n");
}
JCWasmx86
  • 3,473
  • 2
  • 11
  • 29
KrevoL
  • 103
  • 1
  • 1
  • 7
  • Because this is `C`, this expression (and others like it.): `a[i] = (int *)malloc(n1*sizeof(int));` should be expressed as `a[i] = malloc(n1*sizeof(int));` because [it is not recommeded to cast the return of malloc() and family in C](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – ryyker Sep 22 '20 at 13:24
  • But then i cant use array of pointers in any other way and typecasting is working fine because when i print the 2 arrays a and b im getting `1 2 3 4` – KrevoL Sep 22 '20 at 13:29
  • _But then i cant use array of pointers_. Really? – ryyker Sep 22 '20 at 13:32

1 Answers1

1

You forgot to initialize sum before calculating each elements.

                sum = 0; /* add this */

                for(k=0;k<n2;k++)

                {

                    sum += a[i][k] * b[k][j];

                }

                *(*(c+i)+j) = sum;
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • @KrevoL The actual output is cumulative sum of the expected output. This indicates that the error is due to this lack of initialization. – MikeCAT Sep 22 '20 at 13:29
  • 1
    @KrevoL, initializing `sum` to zero at the beginning of the program is not sufficient. You need to set it [back] to zero at the beginning of the computation of each element of the result matrix. – John Bollinger Sep 22 '20 at 13:32
  • @JohnBollinger Thanks manh . I dint notice that . It worked finally !! – KrevoL Sep 22 '20 at 17:09
  • @MikeCAT Thanks bruh.....it worked . Ur help is appreciated manh – KrevoL Sep 22 '20 at 17:10