C語言學習之求解二維陣列的最大/最小元素
阿新 • • 發佈:2019-01-23
#include <stdio.h> #include <conio.h> #define ROW 3 #define COL 4 void main() { int a[ROW][COL]; int i,j; int r_max,r_min; //輸入A矩陣 printf("please enter the element of matrixA:%d*%d\n",ROW,COL); for (i = 0; i < ROW; i++) { for (j = 0; j < COL; j++) { scanf("%d",&a[i][j]); } } //輸出a矩陣 printf("matri*b %d*%d :\n",ROW,COL); for (int i = 0; i < ROW; i++) { for (int j = 0; j < COL; j++) { printf("%8d",a[i][j]); } printf("\n"); } r_max = a[0][0]; r_min = a[0][0]; for (i = 0; i < ROW; i++) { for (j = 0; j < COL; j++) { if (r_max < a[i][j]) { r_max = a[i][j]; } if (r_min > a[i][j]) { r_min = a[i][j]; } } } printf("the largest number of the matrix is %d\n",r_max); printf("the smallest number of the matrix is %d\n",r_min); getch(); }