# Thread: c-program on multiplication of matrices using functions

1. ## 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. I can't help you until you decide to use a compiler that's at least younger than my grandpa...

3. these were the changes that i made in my program->
.replaced void by int
.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");
}

}

}
```

4. 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