找數組最值 按以下函數原型編程從鍵盤輸入一個m行n列的二維數組,而後計算數組中元素的最大值及其所在的行列下標值。其中,m和n的值由用戶鍵盤輸入。已知m和n的值都不超過10。 void Input...

找數組最值
按以下函數原型編程從鍵盤輸入一個m行n列的二維數組,而後計算數組中元素的最大值及其所在的行列下標值。其中,m和n的值由用戶鍵盤輸入。已知m和n的值都不超過10。
void InputArray(int *p, int m, int n);
int  FindMax(int *p, int m, int n, int *pRow, int *pCol);//函數返回最大值,pRow和pCol分別返回最大值所在的行列下標

例如,程序的1次運行結果以下:
Input n:
3,4↙
Input 3*4 array:
1 2 3 4↙
5 6 7 8↙
9 0 -1 -2↙
max=9,row=2,col=0

數組維數輸入提示信息: "Input m,n:\n"
數組維數輸入格式: "%d,%d"

數組元素輸入提示信息: "Input %d*%d array:\n"
數組元素輸入格式::"%d"

輸出格式: "max=%d,row=%d,col=%d\n"
 
#include <stdio.h>
void Input(int a[][10], int n,int m); int Findmax(int a[][10], int n, int m,int *pos,int *col); int main() { int  n,m, a[30][10]; int maxNum;//存放數組最大值
    int pRow;//存放最大值下標
    int pcol; printf("Input m,n:\n"); scanf("%d,%d",&n,&m); getchar(); Input(a,n,m); maxNum = Findmax(a,n,m,&pRow,&pcol); printf("max=%d,row=%d,col=%d\n", maxNum, pRow,pcol); return 0; } //讀入數組元素的值
void Input(int a[][10], int n,int m) { int i,j; printf("Input %d*%d array:\n",n,m); for (i = 0; i < n; i++) { for(j=0;j<m;j++) { scanf("%d", &a[i][j]); } } } //計算數組最大值及最大值下標
int Findmax(int a[][10], int n,int m, int *pos,int *col) { int i,j ,maxNum; *pos = 0; *col=0; maxNum = a[0][0]; for(i=0;i<n;i++) { for(j=0;j<m;j++) { if(a[i][j]>maxNum) { maxNum = a[i][j]; *pos = i; *col= j; } } } return maxNum; }
相關文章
相關標籤/搜索