编程从键盘输入一个5行5列的二维数组数据,并找出数组中的最大值及其所在的行下标和列下标

2024-11-30 01:35:07
推荐回答(2个)
回答1:

#include
int main()
{
int i,j,max,row,col,min;
int a[5][5]={{2,33,1,22,12},{24,3,25,6,7,},{31,1,22,4,3},{8,76,5,33,42},{21,22,7,89,10}};
max = a[0][0];
row=0;col=0;
for(i=0;i<5;i++)
for(j=0;j<5;j++)
if(a[i][j]>max)
{
max=a[i][j];
row=i;
col=j;
}
printf("the max nunber:a[%d][%d]=%d\n",row,col,max);
min = a[0][0];
row=0;col=0;
for(i=0;i<5;i++)
for(j=0;j<5;j++)
if(a[i][j]{
min=a[i][j];
row=i;
col=j;
}
printf("the min nunber:a[%d][%d]=%d\n",row,col,min);
}

回答2:

#include "stdafx.h"
#include
#include
int _tmain(int argc, _TCHAR* argv[])
{
void getMax_Min(int myNum[5][5],int n,int maxMsg[3],int minMsg[3]);
int Mynum[5][5];
int i=0,j=0;
printf("请输入5*5的二维数组:\n");
do
{
j = 0;
do
{
printf("第%d行的%d列数:",i+1,j+1);
scanf_s("%d",&Mynum[i][j]);
j++;
} while (j<5);
i++;
} while (i < 5);
int minMsg[3],maxMsg[3];
getMax_Min(Mynum,5,maxMsg,minMsg);
printf("Max=%d,row=%d,col=%d\n",maxMsg[0],maxMsg[1]+1,maxMsg[2]+1);
printf("Min=%d,row=%d,col=%d",minMsg[0],minMsg[1]+1,minMsg[2]+1);
int wait;
scanf_s("%d",&wait);
return 0;
}
void getMax_Min(int myNum[5][5],int n,int maxMsg[3],int minMsg[3])
{
maxMsg[0] = myNum[0][0];
minMsg[0] = myNum[0][0];
for (int i = 0;i < n ;i++)
{
for (int j = 0;j < n ;j ++)
{
if (myNum[i][j] > maxMsg[0])
{
maxMsg[0] = myNum[i][j];
maxMsg[1] = i;
maxMsg[2] = j;
}
if (myNum[i][j] < minMsg[0])
{
minMsg[0]= myNum[i][j];
minMsg[1] = i;
minMsg[2] = j;
}
}
}
}