面向对象编程上机作业

从键盘输入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");


		}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值