从键盘输入R行C列二维整型数组,找出每一行的次最大值并显示出来。要求:
(1)R和C为符号常量;
(2)二维数组是动态申请生成的;
(3)每一行的次最大值是利用单独的函数调用获得的;
(4)对数组元素的访问以及函数参数声明等都是利用指针实现的。
#include<iostream>
using namespace std;
void find_second(int**a, int R, int C)
{
for (int i = 0; i < R; i++) {
int flag = 1;
int max = 0;
int second = 0;
for (int j = 0; j < C; j++) {
if (j != 0 && a[i][j] > max) flag = 0;
if (a[i][j] > max) {
second = max;
max = a[i][j];
}
}
if (flag) {
cout << "Row " << i << " there is no second largest number:" << endl;
}
else {
cout << "Row " << i << " second largest number is :" <<second<< endl;
}
}
}
int main()
{
int R, C;
cout << "please enter R and C" << endl;
cin >> R >> C;
int**a = new int *[R];
for (int i = 0; i < R; i++)
{
a[i] = new int[C];
}
cout << "please enter the array numbers:" <<endl;
for (int i = 0; i < R; i++)
{
for (int j = 0; j < C; j++)
{
cin >> *(*(a + i) + j);
}
}
cout << "The array is:" << endl;
for (int m = 0; m < R; m++)
{
for (int n = 0; n < C; n++)
{
cout << *(*(a + m) + n) << '\t';
}
cout << endl;
}
find_second(a, R, C);
system("pause");
}