#include
#include
#include
#define random(x) (rand()%x)
//找最值函数
void FindValue(int (*arr)[4], int &nMax, int &nMin)
{
//假设一组最大或者最小值
nMax = arr[0][0];
nMin = arr[0][0];
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 4; j++)
{
if (arr[i][j] > nMax) //最大值比较
{
nMax = arr[i][j];
}
else if (arr[i][j] < nMin) //最小值比较
{
nMin = arr[i][j];
}
}
}
}
int main()
{
//定义一个5*4的二维数据。
int arr[5][4] = {0};
//给arr[5][4]赋值,这里将给出随机数(100范围内,取不同值)
//就省得一个一个输入了
srand((int)time(0));
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 4; j++)
{
arr[i][j] = random(100);
}
}
//找出最大值和最小值
int nMax, nMin = 0;
FindValue(arr, nMax, nMin);
//将得到的数显示出来
printf("Max:%d\n", nMax);
printf("Min:%d\n", nMin);
return 0;
}
你看看这样行不行
#include
using namespace std;
int max(int** a, int m, int n)
{
int Max(a[0][0]);
for (short i = 0; i != m; i++)
{
for (short j = 0; j != n; j++)
{
if (a[i][j] > Max)
{
Max = a[i][j];
}
}
}
return Max;
}
int min(int** a, int m, int n)
{
int Min(a[0][0]);
for (short i = 0; i != m; i++)
{
for (short j = 0; j != n; j++)
{
if (a[i][j] < Min)
{
Min = a[i][j];
}
}
}
return Min;
}
int main()
{
int **a = new int*[5];
for (int i = 0; i != 5; i++)
{
a[i] = new int[4];
}
short i, j;
for (i = 0; i != 5; i++)
{
for (j = 0; j != 4; j++)
{
cin >> a[i][j];
}
}
cout << "max=" << max(a, 5, 4) << endl;
cout << "min=" << min(a, 5, 4) << endl;
for (int i = 0; i < 5; i++)
{
delete[] a[i];
}
delete[]a;
system("pause");
return 0;
}
#include
using namespace std;
int * findMaxMin(int arg[5][4])
{
int max, min;
max = arg[0][0];
min = arg[0][0];
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 4; j++)
{
if (max < arg[i][j])
{
max = arg[i][j];
}
if (min > arg[i][j])
{
min = arg[i][j];
}
}
}
int re[2] = { max,min };
return re;
}
void main()
{
int arg[5][4] = {
1, 2, 3, 4,
6, 7, 8, 9,
5, 4, 3, 2,
1, 4, 5, 1,
1, 7, 4, 8
};
cout << findMaxMin(arg)[0] << endl;
cout << findMaxMin(arg)[1] << endl;
}
#include
using namespace std;
int main()
{
int max = 0, min = 0;
int arr[5][4] = {0};
cout<<"请依次输入数组各元素:"<for(int i = 0;i<5;i++)
{
for(int j = 0;j<4;j++)
{
cout<<"yunxing";
cin>>arr[i][j];
}
}
for(int i = 0;i<5;i++)
{
for(int j = 0;j<4;j++)
{
if(arr[i][j]>max)
max = arr[i][j];
if(arr[i][j]min = arr[i][j];
}
}
cout<<"最大值为:"<}
可能主函数中传递的参数,造成了函数中数组越界。