我有一个生成10
行和5
列的程序,用户输入数据。我的问题是,如何找到每一行的最大值和最小值?我已经为此工作了一个小时,但无法弄清楚。我已经尝试解决了很多次。这是我当前的代码。
#include <iostream>
#include <iomanip>
using namespace std;
int returnMax(int[][]);
int main()
{
double sales[10][5];
string name[10];
double highest;
double lowest;
double avg;
// Populating table
for (int row = 0; row < 1; row++)
{
cout << "Enter the salesman's name: " << endl;
cin >> name[row];
cout << "Enter the amount of sales for the five years" << endl;
for (int col = 0; col < 5; col++) {
cin >> sales[row][col];
}
}
cout << returnMax(sales[1][0]) << endl;
return 0;
}
int returnMax(int a[][])
{
int max;
for (int i = 0; i < 1; i++) {
max = a[i][0];
for (int j = 0; j < 5; j++) {
if (a[i][j] > max)
max = a[i][j];
}
}
return max;
}
答案 0 :(得分:2)
您的逻辑在这里:
cout << returnMax(sales[1][0]) << endl;
是错误的。 sales[1][0]
仅是整个sales
数组的单个元素。就是说
sales[1][0] = element of 1st row and 0th column
中的sales
数组,其中您没有初始化任何值。因为您在行中仅在整个数组中仅初始化了一行,所以您需要一行:
for (int row = 0; row < 1; row++)
在C ++中记住,索引从0
开始,而不是从1
开始。话虽如此,上面的结果(未初始化的变量)将使您拥有undefined behavior。
在现代C ++中,与使用原始数组相比,您有更好的选择。对于 例如,使用 std::vector<> 或std::array<> 使您的代码既简单又安全。就您而言,您可以 有
std::vector<int> sales(50, 0) // one dimentional: with 10 * 5 entries
并相应地操作行(请参阅solution-1 )或
std::vector<std::vector<int>> sales(10, std::vector<int>(5, 0));
// two dimensional: with 10 rows and 5 columns
并使用基于范围的for循环,这样就永远不会以 超出范围的问题(请参阅解决方案2 )。
关于查找每个行条目的最小值和最大值,您可以
只需应用称为
std::minmax_element
来自algorithm标头。
示例解决方案-1
使用一维矢量数组的示例解决方案如下所示: SEE LIVE
#include <iostream>
#include <vector> // std::vector
#include <algorithm> // std::minmax_element
#include <string>
int main()
{
constexpr std::size_t rawMax = 2;
constexpr std::size_t colMax = 5;
// one dimentional array with size = (rawMax * colMax)
std::vector<int> sales(rawMax * colMax, 0);
std::vector<std::string> name(rawMax);
// Populating table
for (std::size_t row = 0; row < rawMax; ++row)
{
std::cout << "Enter the salesman's name: "; std::cin >> name[row];
std::cout << "Enter the amount of sales for the five years: " ;
for (std::size_t col = 0; col < colMax; ++col)
std::cin >> sales[(row*colMax) + col]; // convert col and raw to 1D index.
}
/// get the begin and end of each row as iterators
auto rowBeginIter = sales.begin();
auto rowEndIter = sales.begin() + colMax - 1;
for (const std::string& str: name)
{
std::cout << "salesman's name: "; std::cout << str;
auto getMinMaxRow = std::minmax_element(rowBeginIter, rowEndIter + 1);
std::cout << " min: " << *getMinMaxRow.first
<< " max: " << *getMinMaxRow .second << std::endl;
rowBeginIter += colMax; // increment both iterator to the next raw
rowEndIter += colMax;
}
return 0;
}
示例解决方案-2
使用vectors(2D)的向量的示例解决方案如下所示: SEE LIVE
#include <iostream>
#include <vector> // std::vector
#include <algorithm> // std::minmax_element
#include <string>
int main()
{
constexpr std::size_t rawMax = 2; // to test
constexpr std::size_t colMax = 5;
// initilize thw 2D vector of vectors with (rawMax x colMax)
std::vector<std::vector<int>> sales(rawMax, std::vector<int>(colMax, 0));
// initilize with 0's with a size that of maximum number of rows.
std::vector<std::string> name(rawMax, "");
// Populating table
for (std::size_t row = 0; row < rawMax; row++)
{
std::cout << "Enter the salesman's name: "; std::cin >> name[row];
std::cout << "Enter the amount of sales for the five years: " ;
for (std::size_t col = 0; col < colMax; col++) {
std::cin >> sales[row][col];
}
}
/* print max and min of each person
* use range based for loops to loop through them
* (optional: index based loops can also be used like above)
*/
auto nameIter = name.cbegin();
for(const std::vector<int>& each_row: sales)
{
std::cout << "salesman's name: "; std::cout << *nameIter << "\t";
auto getMinMaxRow = std::minmax_element(each_row.cbegin(), each_row.cend());
std::cout << " min: " << *getMinMaxRow.first
<< " max: " << *getMinMaxRow.second << std::endl;
++nameIter; // increment the iterator of name-vector
}
return 0;
}
答案 1 :(得分:1)
首先,以这种方式准备环境:
#define NROWS 10 //use a constant for number of rows
#define NCOLUMNS 5 // use a constant for number of columns
typedef int Matrix[NROWS][NCOLUMNS]; // declare the type Matrix which is 2d Array using NROWS and NCOLUMNS as size
int returnMaxForRow(int,Matrix); //If you want to know the max value of a row, you need to pass the row
因此您基本上可以这样做:
int main () {
Matrix sales; //You don't need to specify the size, which is done before
string name[10];
double highest;
double lowest;
double avg;
ecc....
现在您的函数应该执行以下操作:
int returnMaxForRow (int row, Matrix a) {
int max = a[row][0];
for (int i = 0; i < NCOLUMNS; i++) {
if (a[row][i] > max){
max = a[row][i];
}
}
return max;
}
所以您可以这样称呼它:
cout<< returnMaxForRow(0,sales);
cout<< returnMaxForRow(1,sales);
cout<< returnMaxForRow(2,sales);
cout<< returnMaxForRow(3,sales);
一些建议:
使用常量或变量来设置数组的索引,例如define语句 当您进行sales [1] [0]时,您会得到一个值(第1行,第0列),而不是所有行 使用typedef声明具有不同尺寸的自定义数组,这样更易于处理
如果需要,可以更改函数以返回所有行的所有最大值。 如果要获取矩阵的最大值,则方法类似。