Location类(10.17练习题)C++

设计一个名为Location的类,定位二维数组中的最大值及其位置。

head.h

#include <iostream>

using namespace std;

class Location
{
  public:
	Location(int a[][4])  //**C++中使用二维数组做参数需要指明列的大小**
	{
		RowSize = 0;
		ColumSize = 0;
		MaxValue = a[0][0];
		for (int i = 0;i < 3;i++)
		{

			for (int j = 0;j < 4;j++)
			{
				if (a[i][j] > MaxValue)

				{
					RowSize = i;
					ColumSize = j;
					MaxValue = a[i][j];

				}
			}
		}
		//cout << "数组中最大值为:" << MaxValue << "坐标为(" << RowSize << "," << ColumSize << ")";
	}

	void output()
	{
		cout << "数组中最大值为:" << MaxValue << "坐标为(" << RowSize << "," << ColumSize << ")";

    }


 private:
	double MaxValue;
	int RowSize;
    int ColumSize ;
	
};

Location.cpp

#include "head.h"
using namespace std;

int main()
{
	int a[3][4];
	cout<<"输入一个三行四列的数组:"<<endl;
	for (int i = 0;i < 3;i++)
	{
		for (int j = 0;j < 4;j++)
		{
			cin>> a[i][j];
		}
		cout << " " << endl;
	}
	Location lc ( a );
	lc.output();
	cout << "" << endl;
	system("pause");
}
发布了21 篇原创文章 · 获赞 10 · 访问量 2656

猜你喜欢

转载自blog.csdn.net/weixin_42199022/article/details/97796306
今日推荐