matrix multiplication code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • amit kumar singh
    New Member
    • Oct 2006
    • 1

    matrix multiplication code

    Hi everyone
    I am new to this site and also new to programming world
    can anybody help me writing C code for matrix multiplication (without using pointers)

    Amit
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by amit kumar singh
    Hi everyone
    I am new to this site and also new to programming world
    can anybody help me writing C code for matrix multiplication (without using pointers)

    Amit
    You'd have to try writting something first. It is eaier to help when you've shown some of your ideas first.

    Comment

    • Nanthini Rajendiran
      New Member
      • Mar 2007
      • 2

      #3
      Originally posted by amit kumar singh
      Hi everyone
      I am new to this site and also new to programming world
      can anybody help me writing C code for matrix multiplication (without using pointers)

      Amit
      Hi amrit...Iam writing the simple matrix multiplication code for you without using pointers...
      Code:
      /* Description:To multiply given to matrix using arrays*/
      #include<stdio.h>
      #include<math.h>
      main()
      {
        int i,j,k;
       int a,b,c;/*three variables to get the row and colum of the two matrix*/
       int sum;
       int m[10][10],n[10][10],l[10][10];
       printf("The first matrix:\n");
       printf("Enter the number of rows and columns for the first matrix:\t")  ;
       scanf("%d%d",&a,&b);
       printf("The Second matrix:\n");
       printf(Enter the rows and columns of the second matrix:\t:);
       scanf("%d%d",&b,&c);
      
       /* Note: For matrix multiplication the number of columns in the first matrix should be equal to the number of rows in the second message*/
      
       printf(":Enter the values of the first matrix:\n");
       for(i=0;i<a;i++)
         {
           for(j=0;j<b;j++)
             {
               scanf("%d",&m[i][j]);
             }
         }
        printf("Enter the values of the second matrix:\n");
        for(i=0;i<b;i++)
         {
           for(j=0;j<c;j++)
            {
              scanf("%d",&n[i][j]);
            }
         }
         for(i=0;i<a;i++)
           {
          for(j=0;j<c;j++)
           {
             sum=0;
             for(k=0;k<b;k++)
              {
                sum=sum+(m[i][k]*n[k][j]);
                l[i][j]=sum;
              }
           }
        }
        printf("The multiplied matrix is:\n);
        for(i=0;i<a;i++)
         {
           for(j=0;j<c;j++)
            {
               printf ("%d",l[i][j]);
               printf("\t");
            }
             printf("\n");
         }
      /*This the simple way to do matrix multiplication. ...You can also modify the program to your own choice...*/
      Last edited by horace1; Mar 19 '07, 09:56 PM. Reason: added code tags

      Comment

      • Nanthini Rajendiran
        New Member
        • Mar 2007
        • 2

        #4
        Originally posted by amit kumar singh
        Hi everyone
        I am new to this site and also new to programming world
        can anybody help me writing C code for matrix multiplication (without using pointers)

        Amit
        Amit I have told you the program.just work on th logic Behind it and try to modify the program...

        Comment

        • king99
          New Member
          • Dec 2007
          • 1

          #5
          Hey! Can you post the code in C++ also, I really needs this code for my homework, but you have it in C.

          Comment

          • nikunjsod
            New Member
            • May 2010
            • 1

            #6
            here is the code for c++=====>

            # include <iostream.h>
            # include <conio.h>
            void main ()
            {
            clrscr();
            int i,j,k,sum,a,b,c ;
            int m[10][10];
            int n[10][10];
            int l[10][10];
            cout<<"ENTER THE NO. OF ROWS AND COLUMN OF FIRST MATRIX";
            cin>>a>>b;
            cout<<"ENTER THE VALUES OF FIRST MATRIX-";
            for(i=0;i<a;i++ )
            {
            for(j=0;j<b;j++ )
            {
            cin>>m[i][j];
            }
            }
            cout<<"ENTER THE NO. OF ROWS AND COLUMN OF SECOND MATRIX";
            cin>>b>>c;
            cout<<"ENTER THE VALUES OF SECOND MATRIX-";
            for(i=0;i<b;i++ )
            {
            for(j=0;j<c;j++ )
            {
            cin>>n[i][j];
            }
            }

            for(i=0;i<a;i++ )
            {
            for(j=0;j<c;j++ )
            {
            sum=0;
            for(k=0;k<b;k++ )
            {
            sum=sum+m[i][k]*n[k][j];
            l[i][j]=sum;
            }
            }
            }
            cout<<"THE RESULTANT MATRIX IS-"<<endl;
            for(i=0;i<a;i++ )
            {
            for(j=0;j<c;j++ )
            {
            cout<<l[i][j]<<" ";
            }
            cout<<endl;
            }
            getch();
            }

            Comment

            • serhun
              New Member
              • Nov 2011
              • 1

              #7
              ======> HERE IS FOR JAVA MATRIX MULTIPLICATION
              int A[][]=new int[3][3];
              int B[][]=new int[3][3];
              int C[][]=new int[3][3];
              Scanner srh=new Scanner(System. in);
              Random rnd=new Random();
              for(int i=0;i<3;i++){
              for(int j=0;j<3;j++){
              System.out.prin tln("Enter A: ");
              A[i][j]=srh.nextInt();
              }
              }
              for(int i=0;i<3;i++){
              for(int j=0;j<3;j++){
              System.out.prin tln("Enter B: ");
              B[i][j]=srh.nextInt();
              }
              }
              for(int i=0;i<3;i++){
              for(int j=0;j<3;j++){
              C[i][j]=0;
              for(int k=0;k<3;k++){
              C[i][j]+=A[i][k]*B[k][j];
              }
              }
              }
              for(int i=0;i<3;i++){
              for(int j=0;j<3;j++){
              System.out.prin tln("Value of C: "+C[i][j]);
              }
              }

              Comment

              Working...