You are on page 1of 2

#include <stdio.

h>
#include <stdlib.h>

int main()
{
int i, j, k, row_A, row_B, col_A, col_B, **A, **B, **C;
printf("\nEnter the size of Matrix A:\n");
scanf("%d%d", &row_A, &col_A);
printf("\nEnter the size of Matrix B:\n");
scanf("%d%d", &row_B, &col_B);
if(col_A != row_B)
{
printf("\nThe matrix multiplication criteria is not satisfied.\n");
exit(1);
}
A = (int **)malloc(sizeof(int)*row_A);
for(i=0; i<row_A; i++)
A[i] = (int *)malloc(sizeof(int)*col_A);
B = (int **)malloc(sizeof(int)*row_B);
for(i=0; i<row_B; i++)
B[i] = (int *)malloc(sizeof(int)*col_B);
C = (int **)malloc(sizeof(int)*row_A);
for(i=0; i<row_A; i++)
C[i] = (int *)malloc(sizeof(int)*col_B);
printf("\nEnter the elements of Matrix A:\n");
for(i=0; i<row_A; i++)
{
for(j=0; j<col_A; j++)
scanf("%d", &A[i][j]);
}
printf("\nEnter the elements of Matrix B:\n");
for(i=0; i<row_B; i++)
{
for(j=0; j<col_B; j++)
scanf("%d", &B[i][j]);
}
printf("\n********* Entered elements of Matrix A are: *********\n");
for(i=0; i<row_A; i++)
{
for(j=0; j<col_A; j++)
printf("%d\t", A[i][j]);
printf("\n");
}
printf("\n********* Entered elements of Matrix B are: *********\n");
for(i=0; i<row_B; i++)
{
for(j=0; j<col_B; j++)
printf("%d\t", B[i][j]);
printf("\n");
}
#pragma omp parllel private for(i,j)
for(i=0; i<row_A; i++)
{
for(j=0; j< col_B; j++)
{
C[i][j] = 0;
for(k=0; k<col_A; k++)
C[i][j] += A[i][k] * B[k][j];
}
}
printf("\n********* Resultant elements of Matrix AxB are: *********\n");
for(i=0; i<row_A; i++)
{
for(j=0; j<col_B; j++)
printf("%d\t", C[i][j]);
printf("\n");
}
return 0;
}

Output:

Enter the size of Matrix A:


2
2

Enter the size of Matrix B:


2
2

Enter the elements of Matrix A:


1
1
1
1
Enter the elements of Matrix B:
1
1
1
1

********* Entered elements of Matrix A are: *********


1 1
1 1

********* Entered elements of Matrix B are: *********


1 1
1 1

********* Resultant elements of Matrix AxB are: *********


2 2
2 2

Process returned 0 (0x0) execution time : 12.669 s


Press any key to continue.

You might also like