Yêu cầu:
Đếm số lượng giá trị có trong ma trận số thực.
Lưu ý: Nếu ma trận có k phẩn tử (k>1) trong ma trận bằng nhau thì ta chỉ tính là 1.
Thuật toán:
1. Viết hàm nhập giá trị vào ma trận
2. Viết hàm đếm số phần tử khác nhau ở trong ma trận
– Cấp phát động mảng 1 chiều pVal kiểu float để lưu giá trị khác nhau của ma trận.
– Duyệt từng phần tử của ma trận và so sánh A[iRow][iCol] với các giá trị được lưu mảng pVal được cấp phát ở trên.
– Nếu phần tử A[iRow][iCol] khác với các giá trị trong mảng pVal, lưu giá trị A[iRow][iCol] vào mảng pVal và đồng thời tăng biến đếm.
Code:
/************Count different elements in matrix************* *Author: vncoding *Date : 31/03/2019 ************************************************************/ #include <stdio.h> #include <conio.h> #include <stdlib.h> #include <memory> #define N 100 #define M 100 void ImportData(float[][M], int, int, char*); void PrintMatrix(float[][M], int, int); int CntValInMatrix(float[][M], int, int); void main() { int nRow, nCol; float average; float A[N][M]; printf("\nNumber of row: "); scanf("%d", &nRow); printf("\nNumber of column: "); scanf("%d", &nCol); ImportData(A, nRow, nCol, "A"); PrintMatrix(A, nRow, nCol); printf("\nThe number of different elements in matrix: %d", CntValInMatrix(A, nRow, nCol)); getch(); } /************************************************************* * Function : CntValInMatrix() * Parameter : A[][M]: input matrix (I) nRow : row number (I) nCol : column number(I) * Return : float * Description : Count value in matrix *************************************************************/ int CntValInMatrix(float A[][M], int nRow, int nCol) { int iRow, iCol; int i, cnt = 0; int flg = 0; float *pVal = NULL; pVal = (float*)malloc(nRow * nCol * sizeof(float)); if (!pVal) { return -1; } memset(pVal, NULL, nRow * nCol * sizeof(float)); for (iRow = 0; iRow < nRow; iRow++) { for (iCol = 0; iCol < nCol; iCol++) { flg = 0; for (i = 0; i < cnt; i++) { if (A[iRow][iCol] == pVal[i]) { flg = 1; break; } } if (flg == 0) { pVal[cnt++] = A[iRow][iCol]; } } } free(pVal); return cnt; } /************************************************************* * Function : ImportData() * Parameter : A[][M]: input matrix (I) nRow : row number (I) nCol : column number(I) nameMatrix: matrix name (I) * Return : void * Description : Import matrix data *************************************************************/ void ImportData(float 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("%f", &Matrix[iRow][iCol]); } } /************************************************************* * Function : PrintMatrix() * Parameter : Matrix: input matrix (I) nRow : row number (I) nCol : column number(I) * Return : void * Description : Display matrix data *************************************************************/ void PrintMatrix(float Matrix[][M], int nRow, int nCol) { int iRow, iCol; printf("\nA = "); for (iRow = 0; iRow < nRow; iRow++) { printf("\n"); for (iCol = 0; iCol < nCol; iCol++) { printf("\t%.2f\t", Matrix[iRow][iCol]); } } }
Kết quả:
Number of row: 3 Number of column: 3 A[0][0] = -2 A[0][1] = 12.2 A[0][2] = 12 A[1][0] = 12.2 A[1][1] = -6.9 A[1][2] = 3.4 A[2][0] = -2 A[2][1] = 0 A[2][2] = 34 A = -2.00 12.20 12.00 12.20 -6.90 3.40 -2.00 0.00 34.00 The number of different elements in matrix: 7
Leave a Reply
You must be logged in to post a comment.