题目:
main函数中定义一个3*4的二维数组a,调用函数max_array求出其中值最大的那个元素的值,以及其所在的行号和列号,并利用引用类型参数返回结果,并在main函数中输出结果。(说明:这个程序中的函数max_array需要返回两个值,一个是行号,一个是列号,这是不可能用return语句返回的,所以应该考虑用引用的方法返回两个值)
代码:
#include <iostream>
using namespace std;
void max_array(int &m, int &n, int a[3][4])
{ int i, j;
int max = a[0][0];
for (i = 0; i < 3; i++)
{ for (j = 0; j < 4; j++)
{ if (a[i][j] > max)
{ m = i;
n = j;
}
}
}
}
int main()
{ int a[3][4] = {1, 2, 3, 4, 5, 6, 7, 8, 10, 9, 13, 25};
int x = 0, y = 0;
max_array(x, y, a);
cout << "其行数为:" << x << "其列数为:" << y << endl;
return 0;
}