Yêu cầu:
Liệt kê các dòng giảm dần trong ma trận số thực
Thuật toán:
1. Viết hàm nhập dữ liệu vào ma trận
2. Viết hàm FindDescendingRow() tìm chỉ số hàng giảm dần trong ma trận
– Duyệt các phần tử trên 1 hàng
– So sánh các phần tử trong 1 hàng, nếu không thỏa mãn điều kiện phần tử trước lớn hơn phần tử phía sau, gán idx = -1
– Khi duyệt hết dòng, nếu idx != -1, lưu chỉ số dòng vào mảng pArr
– Lặp lại bước 1
– Cuối cùng, trả về con trỏ pArr lưu chỉ số hàng giảm dần.
Code:
#include <stdio.h> #include <conio.h> #include <stdlib.h> #include <string.h> #define N 100 #define M 100 void ImportData(float[][M], int, int, char*); void PrintMatrix(float[][M], int, int); int* FindDescendingRow(float[][M], int, int, int*); void main() { int nRow, nCol; float A[N][M]; int *res; int cnt; int idx; printf("\nNumber of row: "); scanf("%d", &nRow); printf("\nNumber of column: "); scanf("%d", &nCol); ImportData(A, nRow, nCol, "A"); PrintMatrix(A, nRow, nCol); res = FindDescendingRow(A, nRow, nCol, &cnt); if (!cnt) printf("\nMatrix does not have descending row\n"); else { printf("\nDescending row in matrix: "); for (idx = 0; idx < cnt; idx++) { printf("%d, ", res[idx] + 1); } free(res); } getch(); } /************************************************************* * Function : FindDescendingRow() * Parameter : A[][M]: input matrix (I) nRow : row number (I) nCol : column number(I) cnt : total number of descending row (O) * Return : pointer to array including descending row index * Description : Find descending row in matrix *************************************************************/ int* FindDescendingRow(float A[][M], int nRow, int nCol, int* cnt) { int iRow, iCol; int *pArr = NULL; int idx; *cnt = 0; pArr = (int*)malloc(nRow * sizeof(int)); if (!pArr) { return NULL; } memset(pArr, NULL, nRow * sizeof(int)); for (iRow = 0; iRow < nRow; iRow++) { idx = iRow; for (iCol = 0; iCol < nCol-1; iCol++) { if (A[iRow][iCol] <= A[iRow][iCol+1]) { idx = -1; break; } } if (idx != -1) { pArr[(*cnt)++] = idx; } } return pArr; } /************************************************************* * 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: 4 Number of column: 3 A[0][0] = 3.4 A[0][1] = 3.5 A[0][2] = 3.6 A[1][0] = 4.5 A[1][1] = 3.2 A[1][2] = 0 A[2][0] = 1.2 A[2][1] = 4.5 A[2][2] = -5.6 A[3][0] = 2.3 A[3][1] = 2.3 A[3][2] = 1 A = 3.40 3.50 3.60 4.50 3.20 0.00 1.20 4.50 -5.60 2.30 2.30 1.00 Descending row in matrix: 2,
Leave a Reply
You must be logged in to post a comment.