Thread: c-program on multiplication of matrices using functions

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    8

    c-program on multiplication of matrices using functions

    when i am running this program no errors are being shown butas soon as i press ctr+f9 to run the program my tc++ window closes..... please help me find out the problem....

    Code:
    #include<stdio.h>
    #include<conio.h>
    void multiply(int m1[50][50],int ,int,int m2[50][50],int ,int );
    
    
    void main()
    {
    int i,j,m,n,p,q,m1[50][50],m2[50][50];
    printf("Enter the dimensions of the matrix1:");
    scanf("%d %d",&m,&n)   ;
    printf("enter the matrix1\n");
    for(i=0;i<m;i++)
    {
    for(j=0;j<n;j++)
    scanf("%d",&m1[i][j]);
    }
    printf("Enter the dimensions of the matrix2:") ;
    scanf("%d %d",&p,&q);
    printf("enter the matrix 2\n");
    for(i=0;i<p;i++)
    {
    for(j=0;j<q;j++)
     scanf("%d",&m2[i][j]);
     }
    
    
    multiply( m1,m,n,m2,p,q);
    
    
    
    
              }
    
    
     void multiply(int m1[][50],int m,int n,int m2[][50],int p,int q)
     {
                 int i,j,k,m3[50][50];
                 if(n!=p)
                 printf("you have entered incorrect dimensions...the matrices cannot be multiplied:\n");
                 else
                 {
        for(i=0;i<q;i++)
        {
          for(j=0;j<m;j++)
            {
             m3[j][i]=0;
            for(k=0;k<n;k++)
             m3[j][i] = m3[j][i] + (m1[j][k]*m2[k][i]);
                    }
                        }
    
    
          printf("the multiplication of the 2 matrices gives the following result:\n");
    
    
         for(i=0;i<m;i++)
         {
         for(j=0;j<q;j++)
         printf(" %d",m3[i][j]);
         printf("\n");
         }
    
    
         }
    
    
             }

  2. #2

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    I can't help you until you decide to use a compiler that's at least younger than my grandpa...
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    8
    these were the changes that i made in my program->
    .replaced void by int
    .added getch() in the end
    .but still that didnt help..
    and i dont think so there is any problem in my compiler as this problem never occured before...this is the first time i m facing such a problem.....

    Code:
    
    #include<stdio.h>
    Code:
    #include<conio.h>
    void multiply(int m1[50][50],int ,int,int m2[50][50],int ,int );
    
     int main()
    {
    int i,j,m,n,p,q,m1[50][50],m2[50][50];
    printf("Enter the dimensions of the matrix1:");
    scanf("%d %d",&m,&n)   ;
    printf("enter the matrix1\n");
    for(i=0;i<m;i++)
    {
    for(j=0;j<n;j++)
    scanf("%d",&m1[i][j]);
    }
    printf("Enter the dimensions of the matrix2:") ;
    scanf("%d %d",&p,&q);
    printf("enter the matrix 2\n");
    for(i=0;i<p;i++)
    {
    for(j=0;j<q;j++)
     scanf("%d",&m2[i][j]);
     }
    
    
    multiply( m1,m,n,m2,p,q);
     getch();
     return 0;
    
    
              }
    
    
     void multiply(int m1[][50],int m,int n,int m2[][50],int p,int q)
     {
                 int i,j,k,m3[50][50];
                 if(n!=p)
                 printf("you have entered incorrect dimensions...the matrices cannot be multiplied:\n");
                 else
                 {
        for(i=0;i<q;i++)
        {
          for(j=0;j<m;j++)
            {
             m3[j][i]=0;
            for(k=0;k<n;k++)
             m3[j][i] = m3[j][i] + (m1[j][k]*m2[k][i]);
                    }
                        }
    
    
          printf("the multiplication of the 2 matrices gives the following result:\n");
    
    
         for(i=0;i<m;i++)
         {
         for(j=0;j<q;j++)
         printf(" %d",m3[i][j]);
         printf("\n");
         }
    
    
         }
    
    
             }
    
    Last edited by dishagarg; 02-28-2013 at 10:10 AM.

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    After you have read all numbers from the user there is still a newline character left in the input stream. getch() will happily read it and thus your program doesn't wait for another key press.

    Read also FAQ > How do I avoid a "dangling" newline when reading single character user input? - Cprogramming.com

    If your IDE doesn't have an option to keep the console window open, then that's another reason to ditch it.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomial Multiplication program
    By daggerhunt in forum C Programming
    Replies: 2
    Last Post: 11-02-2012, 07:57 AM
  2. Multiplication Table Program
    By jamesallen4u in forum C Programming
    Replies: 3
    Last Post: 10-17-2011, 07:48 AM
  3. multiplication program
    By wankel in forum C Programming
    Replies: 11
    Last Post: 06-13-2009, 12:33 PM
  4. Replies: 9
    Last Post: 04-20-2009, 12:34 PM
  5. Matrices and Multiplication
    By SyntaxBubble in forum Game Programming
    Replies: 3
    Last Post: 01-07-2002, 09:53 PM