设计一个名为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");
}