Yêu cầu:
Đếm giá trị nhỏ nhất trong ma trận số nguyên
Thuật toán:
Gồm có 2 bước:
– Bước 1: Tìm giá trị nhỏ nhất trong ma trận
– Bước 2: Đếm số lượng giá trị nhỏ nhất
Code:
/******Count the number of minimum value in matrix********** *Author: vncoding *Date : 11/05/2019 ************************************************************/ #include <stdio.h> #include <conio.h> #include <stdlib.h> #define N 100 #define M 100 void ImportData(int[][M], int, int, char*); void PrintMatrix(int[][M], int, int, char); int CntMinInMatrix(int[][M], int, int); void main() { int aRow, aCol; int A[N][M]; int res; printf("\nNumber of row: "); scanf("%d", &aRow); printf("\nNumber of column: "); scanf("%d", &aCol); ImportData(A, aRow, aCol, "A"); PrintMatrix(A, aRow, aCol, 'A'); printf("\nThe number of minimum value in matrix: %d", CntMinInMatrix(A, aRow, aCol)); getch(); } /************************************************************* * Function : CntMinInMatrix() * Parameter : A[][M]: input matrix (I) * aRow : row number of matrix A (I) * aCol : column number of matrix A (I) * * Return : the number of minimum value in matrix * * Description : count the number of minimum value in matrix *************************************************************/ int CntMinInMatrix(int A[][M], int aRow, int aCol) { int iARow, iACol; int cnt = 0; int min; //1. Find minimum value in matrix min = A[0][0]; for (iARow = 0; iARow < aRow; iARow++) { for (iACol = 0; iACol < aCol; iACol++) { if (min > A[iARow][iACol]) { min = A[iARow][iACol]; } } } //2. Count the number of minimum value for (iARow = 0; iARow < aRow; iARow++) { for (iACol = 0; iACol < aCol; iACol++) { if (min == A[iARow][iACol]) { cnt++; } } } 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(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 : PrintMatrix() * Parameter : Matrix: input matrix (I) nRow : row number (I) nCol : column number(I) name : name of matrix (I) * Return : void * Description : Display matrix data *************************************************************/ void PrintMatrix(int Matrix[][M], 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("\t%d\t", Matrix[iRow][iCol]); } } }
Kết quả:
Number of row: 4 Number of column: 4 A[0][0] = 1 A[0][1] = 2 A[0][2] = 3 A[0][3] = 4 A[1][0] = 5 A[1][1] = 6 A[1][2] = 1 A[1][3] = 2 A[2][0] = 3 A[2][1] = 1 A[2][2] = 7 A[2][3] = 1 A[3][0] = 4 A[3][1] = 1 A[3][2] = 4 A[3][3] = 6 A = 1 2 3 4 5 6 1 2 3 1 7 1 4 1 4 6 The number of minimum value in matrix: 5
Leave a Reply
You must be logged in to post a comment.