Yêu cầu:
Liệt kê chỉ số các dòng chứa toàn giá trị chẵn trong ma trận số nguyên
Thuật toán:
1. Viết hàm nhập dữu liệu vào ma trận
2. Viết hàm FindEvenRow() trả về mảng chứa các dòng chẵn
Code:
#include <stdio.h> #include <conio.h> #include <stdlib.h> #include <string.h> #define N 100 #define M 100 void ImportData(int[][M], int, int, char*); void PrintMatrix(int[][M], int, int); int* FindEvenRow(int[][M], int, int, int*); void main() { int nRow, nCol; int 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 = FindEvenRow(A, nRow, nCol, &cnt); if (!cnt) printf("\nMatrix does not have even row\n"); else { printf("\nEven row in matrix: "); for (idx = 0; idx < cnt; idx++) { printf("%d, ", res[idx] + 1); } free(res); } getch(); } /************************************************************* * Function : FindEvenRow() * Parameter : A[][M]: input matrix (I) nRow : row number (I) nCol : column number(I) cnt : total number of negative row (O) * Return : pointer to array including even row index * Description : Find even row in matrix *************************************************************/ int* FindEvenRow(int 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; iCol++) { if (A[iRow][iCol]%2 != 0) { 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(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) * Return : void * Description : Display matrix data *************************************************************/ void PrintMatrix(int 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%d\t", Matrix[iRow][iCol]); } } }
Kết quả:
Number of row: 3 Number of column: 3 A[0][0] = 2 A[0][1] = -3 A[0][2] = 2 A[1][0] = 10 A[1][1] = 8 A[1][2] = 6 A[2][0] = 0 A[2][1] = -3 A[2][2] = 4 A = 2 -3 2 10 8 6 0 -3 4 Even row in matrix: 2,
Leave a Reply
You must be logged in to post a comment.