Hi, I am

Ngô Tôn

I am a programmer.

Home / Programming / C/C++ / Cộng 2 ma trận

Cộng 2 ma trận

Yêu cầu:

Viết chương trình cộng 2 ma trận:
– Nhập 2 ma trận
– Cộng 2 ma trận và lưu kết quả vào ma trận kết quả
– Hiển thị ra màn hình

Thuật toán:

Cộng các phần tử tương ứng của 2 ma trận với nhau: C[i][j] = A[i][j] + B[i][j]

Code:

/*********************Add 2 matrix*******************/

#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
#define N 100
#define M 100

void ImportData(int Matrix[][M], int nRow, int nCol, char* nameMatrix);
void PrintMatrix(int** Matrix, int nRow, int nCol);
int** Add2Matrix(int A[][M], int B[][M], int nRow, int nCol);
void main()
{
int iRow, iCol;
int nRow, nCol;
int A[N][M];
int B[N][M];
int** C;

printf("\nNhap kich thuoc hang: ");
scanf("%d", &nRow);
printf("\nNhap kich thuoc cot: ");
scanf("%d", &nCol);
ImportData(A, nRow, nCol, "A");
ImportData(B, nRow, nCol, "B");
C = Add2Matrix(A, B, nRow, nCol);
PrintMatrix(C, nRow, nCol);

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 A[][M], int B[][M], int nRow, int nCol)
{
int iRow, iCol;
int **C;
C = (int**)malloc(nRow * sizeof(int *));
for(iRow = 0; iRow < nRow; iRow++)
{
C[iRow] = (int*)malloc(nCol * sizeof(int));
}
for(iRow = 0; iRow < nRow; iRow++)
for (iCol = 0; iCol < nCol; iCol++)
{
C[iRow][iCol] = A[iRow][iCol] + B[iRow][iCol];
}
return C;
}

/*************************************************************
//Function : Import data for matrix
//Parameter: matrix, and the number of row and column and
//name matrix
//Return : void
*************************************************************/
void ImportData(int Matrix[][M], int nRow, int nCol, char* nameMatrix)
{
int iRow, iCol;
for(iRow = 0; iRow < nRow; iRow++)
for(iCol = 0; iCol < nCol; iCol++)
{
printf("\n%s[%d][%d] = ", nameMatrix, 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)
{
int iRow, iCol;
printf("\nA = ");
for(iRow = 0; iRow < nRow; iRow++)
{
printf("\n");
for (iCol = 0; iCol < nCol; iCol++)
{
printf("%5d", Matrix[iRow][iCol]);
}
}
}

Kết quả:

Nhận xét: Chương trình trên đây có một số nhược điểm như sau:

  • Việc khai báo mảng tĩnh gây lãng phí nếu không dùng hết hoặc thiếu bộ nhớ nếu kích thước mảng khai báo vượt quá 100×100
  • Tham số của hàm ImportData() và Add2Matrix() kiểu A[][M] (M = 100: kích thước cột của mảng 2 chiều và phải là hằng số). Do vậy, không thể sử dụng được 2 hàm này với đối số đầu vào là mảng 2 chiều kích thước cột khác 100
  • Hàm PrintMatrix() chỉ nhận tham số đầu vào là con trỏ 2 chiều int**. Do vậy, chúng ta không thể sử dụng hàm PrintMaxtrix() để hiển thị mảng A, B

Chương trình dưới đây sẽ khắc phục nhược điểm trên.

/*********************Add 2 matrix*******************/

#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);
}
}

Kết quả:

Chú ý: phải giải phóng vùng nhớ đã cấp phát bằng lệnh free hoặc delete

About ngoton

Ngô Tôn is a programmer with passion for tailored software solutions. Comes with 7+ years of IT experience, to execute beautiful front-end experiences with secure and robust back-end solutions.

Check Also

Xây dựng hàm bạn để tính diện tích hình chữ nhật

Yêu cầu: Xây dựng hàm bạn để tính diện tích hình chữ nhật Code: Giải …

Leave a Reply

avatar
  Subscribe  
Notify of