Yêu cầu:
Kiểm tra các giá trị trong ma trận có giảm dần theo dòng và cột không.
Ví dụ: ma trận sau giảm dần theo hàng và cột
87 75 64 62
46 40 33 28
20 18 15 10
Thuật toán:
1. Viết hàm nhập dữ liệu ma trận
2. Viết hàm isDescendingMatrix() kiểm tra ma trận giảm dần theo cột và hàng
– Kiểm tra tất cả các hàng xem có giảm dần không
– Kiểm tra tất cả các cột xem có giảm dần không
Code:
/***************Descending matrix******************** *Author: vncoding *Date : 06/04/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); bool isDescendingMatrix(float[][M], int, int); void main() { int nRow, nCol; float A[N][M]; bool res; printf("\nNumber of row: "); scanf("%d", &nRow); printf("\nNumber of column: "); scanf("%d", &nCol); ImportData(A, nRow, nCol, "A"); PrintMatrix(A, nRow, nCol); res = isDescendingMatrix(A, nRow, nCol); if (res) printf("\nMatrix is descending\n"); else printf("\nMatrix is not descending\n"); getch(); } /************************************************************* * Function : isDescendingMatrix() * Parameter : A[][M]: input matrix (I) nRow : row number (I) nCol : column number(I) sCol : specified column(I) * Return : bool * Description : Check descending matrix *************************************************************/ bool isDescendingMatrix(float A[][M], int nRow, int nCol) { int iRow, iCol; // Check descending row for (iRow = 0; iRow < nRow; iRow++) { for (iCol = 0; iCol < nCol - 1; iCol++) { if (A[iRow][iCol] > A[iRow][iCol+1]) continue; else return false; } } // Check descending column for (iCol = 0; iCol < nCol; iCol++) { for (iRow = 0; iRow < nRow - 1; iRow++) { if (A[iRow][iCol] > A[iRow+1][iCol]) continue; else return false; } } return true; } /************************************************************* * 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] = 87 A[0][1] = 75 A[0][2] = 64 A[0][3] = 62 A[1][0] = 46 A[1][1] = 40 A[1][2] = 33 A[1][3] = 28 A[2][0] = 20 A[2][1] = 18 A[2][2] = 15 A[2][3] = 10 A = 87.00 75.00 64.00 62.00 46.00 40.00 33.00 28.00 20.00 18.00 15.00 10.00 Matrix is descending
Leave a Reply
You must be logged in to post a comment.