Yêu cầu:
Đếm phần tử cực trị trong mảng số thực. Phần tử được gọi là cực trị nếu nó lớn hơn phần tử xung quanh hoặc nhỏ hơn phần tử xung quanh
Thuật toán:
1. Viết hàm nhập giá trị vào ma trận
2. Viết hàm đếm phần tử cực trị trong ma trận
– Duyệt từng phần của ma trận
– So sánh phần tử hiện tại A[iRow][iCol] với phần tử lân cận A[iRow-1][iCol], A[iRow+1][iCol], A[iRow][iCol-1], A[iRow][iCol+1]
– 2.1 So sánh với phần tử trước đó cùng trên 1 cột
+ Nếu A[iRow][iCol] > A[iRow-1][iCol] => gán flg = 1
+ Nếu A[iRow][iCol] < A[iRow-1][iCol] => gán flg = 2
+ Nếu A[iRow][iCol] == A[iRow-1][iCol] => không tăng biến đếm duyệt phần tử tiếp theo
– 2.2 So sánh với phần tử kế tiếp trên cùng 1 cột và check flg
+ Nếu A[iRow][iCol] > A[iRow+1][iCol] và (flg == 1 hoặc flg == 0) => gán flg = 1
+ Nếu A[iRow][iCol] < A[iRow+1][iCol] và (flg == 1 hoặc flg == 0) => gán flg = 2
+ Nếu A[iRow][iCol] == A[iRow+1][iCol] => không tăng biến đếm duyệt phần tử tiếp theo
…
– Tăng biến đếm và duyệt phần tử tiếp theo
Code:
/******Count minimum/maximum elements in matrix************* *Author: vncoding *Date : 31/03/2019 ************************************************************/ #include <stdio.h> #include <conio.h> #include <stdlib.h> #define N 100 #define M 100 void ImportData(float[][M], int, int, char*); void PrintMatrix(float[][M], int, int); int CntMinMaxInMatrix(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 minimum/maximum elements in matrix: %d", CntMinMaxInMatrix(A, nRow, nCol)); getch(); } /************************************************************* * Function : CntMinMaxInMatrix() * Parameter : A[][M]: input matrix (I) nRow : row number (I) nCol : column number(I) * Return : float * Description : Count minum/maximum elements in matrix *************************************************************/ int CntMinMaxInMatrix(float A[][M], int nRow, int nCol) { int iRow, iCol; int cnt = 0; int flg = 0; // 0: init, 1: maximum, 2: minimum for (iRow = 0; iRow < nRow; iRow++) { for (iCol = 0; iCol < nCol; iCol++) { flg = 0; //init // Compare with previous element in column if (iRow - 1 >= 0) { if (A[iRow][iCol] > A[iRow - 1][iCol]) flg = 1; else if (A[iRow][iCol] < A[iRow - 1][iCol]) flg = 2; else continue; } // Compare with next element in column if (iRow + 1 < nRow) { if (A[iRow][iCol] > A[iRow + 1][iCol] && (flg == 1 || flg == 0)) flg = 1; else if (A[iRow][iCol] < A[iRow + 1][iCol] && (flg == 2 || flg == 0)) flg = 2; else continue; } // Compare with previous element in row if (iCol - 1 >= 0) { if (A[iRow][iCol] > A[iRow][iCol - 1] && (flg == 1 || flg == 0)) flg = 1; else if (A[iRow][iCol] < A[iRow][iCol - 1] && (flg == 2 || flg == 0)) flg = 2; else continue; } // Compare with next element in row if (iCol + 1 < nCol) { if (A[iRow][iCol] > A[iRow][iCol + 1] && (flg == 1 || flg == 0)) flg = 1; else if (A[iRow][iCol] < A[iRow][iCol + 1] && (flg == 2 || flg == 0)) flg = 2; else continue; } 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(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: 4 A[0][0] = 4 A[0][1] = -5 A[0][2] = 76 A[0][3] = 23 A[1][0] = -5 A[1][1] = 23 A[1][2] = 12 A[1][3] = 9 A[2][0] = 9 A[2][1] = 4 A[2][2] = 56 A[2][3] = 23 A = 4.00 -5.00 76.00 23.00 -5.00 23.00 12.00 9.00 9.00 4.00 56.00 23.00 The number of minimum/maximum elements in matrix: 8
Leave a Reply
You must be logged in to post a comment.