找數組最值 按如下函數原型編程從鍵盤輸入一個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);


https://iuwe.cc/index.php/archives/80/
找數組最值 按如下函數原型編程從鍵盤輸入一個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;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM