Other answers may point your wrong. But according to me, here are points that
- For printing you don't need return functions.
- So, void functions should be used.
- You should use macro to specify size of arrays. Because when you want to increase or decrease size of arrays it will make convenient.
- First dimension's size may not be written in prototypes and parameters. But second must.
- Whenever possible and useful situtations, try to make function.
- You wanted to
return
with 0
, it is okey but for error handling
you should choose another number like -1
.
- Whenever possible avoid global variables ! Why?
- I also recommend that declare function prototypes before define functions because if you write wrong type of variable or miss, compiler warns you.
- You can use the code but review your wrongs.
I've tried to improve your code
#include <stdio.h>
#define SIZE 20
void take_data(int a[][SIZE], int b[][SIZE], int fa,int ca, int fb, int cb);
void multiplication(int a[][SIZE],int b[][SIZE],int mult[][SIZE],int fa,int ca,int fb,int cb);
void display(int mult[][SIZE], int fa, int cb);
int main()
{
int a[SIZE][SIZE], b[SIZE][SIZE], mult[SIZE][SIZE], fa, ca, fb, cb;
printf("Enter rows and column for first matrix: ");
scanf("%d%d", &fa, &ca);
printf("Enter rows and column for second matrix: ");
scanf("%d%d",&fb, &cb);
/* If colum of first matrix in not equal to row of second matrix, asking user to enter the size of matrix again. */
while (ca!=fb)
{
printf("Error! column of first matrix not equal to row of second.\n");
printf("Enter rows and column for first matrix: ");
scanf("%d%d", &fa, &ca);
printf("Enter rows and column for second matrix: ");
scanf("%d%d",&fb, &cb);
}
take_data(a,b,fa,ca,fb,cb); /* Function to take matices data */
multiplication(a,b,mult,fa,ca,fb,cb); /* Function to multiply two matrices. */
display(mult,fa,cb); /* Function to display resultant matrix after multiplication. */
return 0;
}
void take_data(int a[][SIZE], int b[][SIZE], int fa,int ca, int fb, int cb)
{
int i,j;
printf("\nEnter elements of matrix 1:\n");
for(i=0; i<fa; ++i)
for(j=0; j<ca; ++j)
{
printf("Enter elements a%d%d: ",i+1,j+1);
scanf("%d",&a[i][j]);
}
printf("\nEnter elements of matrix 2:\n");
for(i=0; i<fb; ++i)
for(j=0; j<cb; ++j)
{
printf("Enter elements b%d%d: ",i+1,j+1);
scanf("%d",&b[i][j]);
}
}
void multiplication(int a[][SIZE],int b[][SIZE],int mult[][SIZE],int fa,int ca,int fb,int cb)
{
int i,j,k;
/* Initializing elements of matrix mult to 0.*/
for(i=0; i<fa; ++i)
for(j=0; j<cb; ++j)
{
mult[i][j]=0;
}
/* Multiplying matrix a and b and storing in array mult. */
for(i=0; i<fa; ++i)
for(j=0; j<cb; ++j)
for(k=0; k<ca; ++k)
{
mult[i][j]+=a[i][k]*b[k][j];
}
}
void display(int mult[][SIZE], int fa, int cb)
{
int i, j;
printf("\nOutput Matrix:\n");
for(i=0; i<fa; ++i)
for(j=0; j<cb; ++j)
{
printf("%d ",mult[i][j]);
if(j==cb-1)
printf("\n\n");
}
}
We need to use malloc()
to return array and don't forget free
it.With return array
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 20
void take_data(int a[][SIZE], int b[][SIZE], int fa,int ca, int fb, int cb);
int** multiplication(int a[][SIZE],int b[][SIZE],int fa,int ca,int fb,int cb);
void display(int fa, int cb, int** mult);
void free_mult(int **mult, int Rows);
int main()
{
int a[SIZE][SIZE], b[SIZE][SIZE], fa, ca, fb, cb;
int** mult;
printf("Enter rows and column for first matrix: ");
scanf("%d%d", &fa, &ca);
printf("Enter rows and column for second matrix: ");
scanf("%d%d",&fb, &cb);
/* If colum of first matrix in not equal to row of second matrix, asking user to enter the size of matrix again. */
while (ca!=fb)
{
printf("Error! column of first matrix not equal to row of second.\n");
printf("Enter rows and column for first matrix: ");
scanf("%d%d", &fa, &ca);
printf("Enter rows and column for second matrix: ");
scanf("%d%d",&fb, &cb);
}
take_data(a,b,fa,ca,fb,cb); /* Function to take matices data */
mult = multiplication(a,b,fa,ca,fb,cb); /* Function to multiply two matrices. */
display(fa,cb,mult); /* Function to display resultant matrix after multiplication. */
free_mult(mult, fa);
return 0;
}
void take_data(int a[][SIZE], int b[][SIZE], int fa,int ca, int fb, int cb)
{
int i,j;
printf("\nEnter elements of matrix 1:\n");
for(i=0; i<fa; ++i)
for(j=0; j<ca; ++j)
{
printf("Enter elements a%d%d: ",i+1,j+1);
scanf("%d",&a[i][j]);
}
printf("\nEnter elements of matrix 2:\n");
for(i=0; i<fb; ++i)
for(j=0; j<cb; ++j)
{
printf("Enter elements b%d%d: ",i+1,j+1);
scanf("%d",&b[i][j]);
}
}
int** multiplication(int a[][SIZE],int b[][SIZE],int fa,int ca,int fb,int cb)
{
int i,j,k;
int **mult = (int **)malloc(fa * sizeof(int *));
int row;
// for each row allocate Cols ints
for (row = 0; row < fa; row++) {
mult[row] = (int *)malloc(fa * sizeof(int));
}
/* Initializing elements of matrix mult to 0.*/
for(i=0; i<fa; ++i)
for(j=0; j<cb; ++j)
{
mult[i][j]=0;
}
/* Multiplying matrix a and b and storing in array mult. */
for(i=0; i<fa; ++i)
for(j=0; j<cb; ++j)
for(k=0; k<ca; ++k)
{
mult[i][j]+=a[i][k]*b[k][j];
}
return mult;
}
void display(int fa, int cb, int** mult)
{
int i, j;
printf("\nOutput Matrix:\n");
for(i=0; i<fa; ++i)
for(j=0; j<cb; ++j)
{
printf("%d ",mult[i][j]);
if(j==cb-1)
printf("\n\n");
}
}
void free_mult(int **mult, int Rows)
{
int row;
// first free each row
for (row = 0; row < Rows; row++) {
free(mult[row]);
}
// Eventually free the memory of the pointers to the rows
free(mult);
}