Yêu cầu:
Viết chương trình nhân 2 ma trận:
– Nhập kích thước và dữ liệu cho 2 ma trận
– Nhân 2 ma trận và lưu kết quả vào ma trận trung gian
– Hiển thị kết quả lên màn hình
Giải thuật:
A[M][N], B[N][P], C[M][P]
Điều kiện thực hiện phép nhân ma trận AxB là: kích thước cột ma trận A = kích thước hàng ma trận B
Nhân ma trận theo công thức: c[i][k] = a[i][j] * b[j][k]
Trong đó: i = (1 – M), j = (1 – N), k = (1 – P)
Code:
/*********************Multiple 2 matrix***************/
#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
void ImportData(int** Arr, int nRow, int nCol, char name);
int** Mul2Matrix(int** Arra, int** Arrb, int m, int n, int p);
void PrintMatrix(int** Arr, int nRow, int nCol, char name);
void FreeMem(int **Buff, int n);
void main()
{
int m, n, p;
int i, j, k;
int **A, **B, **C;
printf("\nsize of row of A: ");
scanf("%d", &m);
printf("\nsize of column A & row of B: ");
scanf("%d", &n);
printf("\nsize of column of B: ");
scanf("%d", &p);
if(m <= 0 || m <= 0 || p <= 0)
return;
// Allocate memory for array A, B
A = (int**)malloc(m * sizeof(int *));
if (!A)
{
return;
}
for(i = 0; i < m; i++)
{
A[i] = (int*)malloc(n * sizeof(int));
if (!A[i])
{
return;
}
}
B = (int**)malloc(n * sizeof(int *));
if (!B)
{
return;
}
for(i = 0; i < n; i++)
{
B[i] = (int*)malloc(p * sizeof(int));
if (!B[i])
{
return;
}
}
// Import data
ImportData(A, m, n, 'A');
ImportData(B, n, k, 'B');
// Implement addition of 2 array
C = (int**)Mul2Matrix(A, B, m, n, p);
PrintMatrix(A, m, n, 'A');
PrintMatrix(B, n, p, 'B');
PrintMatrix(C, m, p, 'C');
// Free allocated memory
FreeMem(A, m);
FreeMem(B, n);
FreeMem(C, m);
getch();
}
/*************************************************************
*Function : Multiple 2 matrix
*Parameter: 2 matrix, and the number of row and column
*Return : 2 dimension pointer points to memory which
* contains result
*************************************************************/
int** Mul2Matrix(int** Arra, int** Arrb, int m, int n, int p)
{
int i, j, k;
int sum = 0;
int **Arrc;
Arrc = (int**)malloc(m * sizeof(int *));
for(i = 0; i < m; i++)
{
Arrc[i] = (int*)malloc(p * sizeof(int));
}
// c[i][k] += (a[i][j]*b[j][k])
for (i = 0; i < m; i++)
{
for (k = 0; k < p; k++)
{
sum = 0;
for (j = 0; j < n; j++)
{
sum += Arra[i][j] * Arrb[j][k];
}
Arrc[i][k] = sum;
}
}
return Arrc;
}
/*************************************************************
//Function : Import data for matrix
//Parameter: matrix, and the number of row and column and
//name matrix
//Return : void
*************************************************************/
void ImportData(int** Arr, int nRow, int nCol, char name)
{
int iRow, iCol;
for(iRow = 0; iRow < nRow; iRow++)
for(iCol = 0; iCol < nCol; iCol++)
{
printf("\n%c[%d][%d] = ", name, iRow, iCol);
scanf("%d", &Arr[iRow][iCol]);
}
}
/*************************************************************
*Function : display matrix on console screen
*Parameter: matrix, and the number of row and column
*Return : void
*************************************************************/
void PrintMatrix(int** Arr, int nRow, int nCol, char name)
{
int iRow, iCol;
printf("\n%c = ", name);
for(iRow = 0; iRow < nRow; iRow++)
{
printf("\n");
for (iCol = 0; iCol < nCol; iCol++)
{
printf("%5d", Arr[iRow][iCol]);
}
}
}
/*************************************************************
*Function : free allocated memory
*Parameter: pointer to memory
*Return : void
*************************************************************/
void FreeMem(int **Buff, int n)
{
int i, j;
for (i = 0; i < n; i++)
{
if (Buff[i])
{
free(Buff[i]);
Buff[i] = NULL;
}
}
if (Buff)
{
free(Buff);
}
}
Kết quả:
Leave a Reply