#include "stdio.h" #include "conio.h" #include "stdlib.h" void ImportData(int** Arr, int nRow, int nCol, char name); int** Add2Matrix(int** Arra, int** Arrb, int nRow, int nCol); void PrintMatrix(int** Arr, int nRow, int nCol, char name); void FreeMem(int **Buff, int n);
void main() { int iRow, iCol; int nRow, nCol; int **A, **B, **C; printf("\nsize of row: "); scanf("%d", &nRow); printf("\nsize of column: "); scanf("%d", &nCol); if(nRow <= 0 || nCol <= 0) return; // Allocate memory for array A, B A = (int**)malloc(nRow * sizeof(int *)); if (!A) { return; } for(iRow = 0; iRow < nRow; iRow++) { A[iRow] = (int*)malloc(nCol * sizeof(int)); if (!A[iRow]) { return; } } B = (int**)malloc(nRow * sizeof(int *)); if (!B) { return; } for(iRow = 0; iRow < nRow; iRow++) { B[iRow] = (int*)malloc(nCol * sizeof(int)); if (!B[iRow]) { return; } } // Import data ImportData(A, nRow, nCol, 'A'); ImportData(B, nRow, nCol, 'B'); // Implement addition of 2 array C = (int**)Add2Matrix(A, B, nRow, nCol); PrintMatrix(A, nRow, nCol, 'A'); PrintMatrix(B, nRow, nCol, 'B'); PrintMatrix(C, nRow, nCol, 'C'); // Free allocated memory FreeMem(A, nRow); FreeMem(B, nRow); FreeMem(C, nRow); getch(); } /************************************************************* *Function : Add 2 matrix *Parameter: 2 matrix, and the number of row and column *Return : 2 dimension pointer points to memory which * contains result *************************************************************/ int** Add2Matrix(int** Arra, int** Arrb, int nRow, int nCol) { int iRow, iCol; int **Arrc; Arrc = (int**)malloc(nRow * sizeof(int *)); for(iRow = 0; iRow < nRow; iRow++) { Arrc[iRow] = (int*)malloc(nCol * sizeof(int)); } for(iRow = 0; iRow < nRow; iRow++) for (iCol = 0; iCol < nCol; iCol++) { Arrc[iRow][iCol] = Arra[iRow][iCol] + Arrb[iRow][iCol]; } return Arrc; } /************************************************************* //Function : Import data for matrix //Parameter: matrix, and the number of row and column and //name matrix //Return : void *************************************************************/ void ImportData(int** Matrix, 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", &Matrix[iRow][iCol]); } } /************************************************************* *Function : display matrix on console screen *Parameter: matrix, and the number of row and column *Return : void *************************************************************/ void PrintMatrix(int** Matrix, 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", Matrix[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); } }
0 nhận xét:
Đăng nhận xét