Sunday, February 19, 2012

Matrix multiply using pointer


/*A program to multiply two matrices using pointer */

#include<stdio.h>
int main()
{
    int a[10][10],b[10][10],c[10][10],sum=0;
    int m1,m2,n1,n2,i,j,k;
    int *ptr1,*ptr2,*ptr3;
    ptr1=a;ptr2=b;ptr3=c;

    printf("enter no of row and column for 1st matrix\n");
    scanf("%d%d",&m1,&n1);
    printf("enter no of row and column for 2nd matrix\n");
    scanf("%d%d",&m2,&n2);

    if(n1==m2)
    {
        printf("for 1st matrix\n");
        for(i=0;i<m1;i++)
            for(j=0;j<n1;j++)
            {
                printf("a[%d][%d]=",i,j);
                scanf("%d",ptr1+i*10+j);
            }

        printf("\nfor 2nd matrix\n");
        for(i=0;i<m2;i++)
            for(j=0;j<n2;j++)
            {
                printf("b[%d][%d]=",i,j);
                scanf("%d",ptr2+i*10+j);
            }


        for(i=0;i<m1;i++)
        {
            for(j=0;j<n2;j++)
            {
               for(k=0;k<n2;k++)
                 sum+=*(ptr1+i*10+k)**(ptr2+k*10+j);
            *(ptr3+i*10+j)=sum;
            sum=0;
            }
        }

        printf("product of entered matrices :-\n");
        for(i=0;i<m1;i++)
        {
            for(j=0;j<n2;j++)
                printf("%d\t",*(ptr3+i*10+j));
            printf("\n");
        }


    }
    else
    printf("multiplication is not possible");
    return 0;
}

8 comments:

  1. Can you explain this as i'm a beginner

    ReplyDelete
  2. someone pls explain

    ReplyDelete
  3. Something is missing in this program

    ReplyDelete
  4. I don't think so. In pointer operations here it will be the original row no in the place of 10

    ReplyDelete
  5. Thanks madam for the program...☺

    ReplyDelete