Reputation: 378
I have a program that generates 10
rows and 5
columns and the user inputs data. My question is, how can I find the maximum and lowest values in each row? I have been working on this for a good hour but cannot figure this out. I have attempted to solve this many times; here is my current code.
#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;
}
Upvotes: 1
Views: 6362
Reputation: 32847
Your logic here:
cout << returnMax(sales[1][0]) << endl;
is wrong. The sales[1][0]
is only a single element of entire sales
array. That means,
sales[1][0] = element of 1st row and 0th column
in sales
array in which you did not have any values initilized. Because you have initilzed only one single row in your entire array as you have the line:
for (int row = 0; row < 1; row++)
Remember in C++ indexing starts from 0
not from 1
. That being said, the above result(uninitialized variables) will lead you to have undefined behavior.
In modern C++ you have better options than using raw arrays. For example, use of std::vector<> or std::array<> makes your code both simpler and safer. In your case, you can either have
std::vector<int> sales(50, 0) // one dimentional: with 10 * 5 entries
and manipulate the rows accordingly(see solution-1) or
std::vector<std::vector<int>> sales(10, std::vector<int>(5, 0));
// two dimensional: with 10 rows and 5 columns
and use range-based for loops so that, you never end up with out of bounds problems(see solution-2).
Regarding finding the min and max of each row entries, you can
simply apply algorithm function called
std::minmax_element
from algorithm header.
Sample solution - 1
A sample solution using one-dimensional vector array would look like this: 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;
}
Sample solution - 2
A sample solution using a vector of vectors(2D) would look like this: 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;
}
Upvotes: 2
Reputation: 908
First of all, prepare your environment this way:
#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
so in the main you can do:
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....
Now your function should do this:
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;
}
so you can call it this way:
cout<< returnMaxForRow(0,sales);
cout<< returnMaxForRow(1,sales);
cout<< returnMaxForRow(2,sales);
cout<< returnMaxForRow(3,sales);
Some Advices:
Use constants or variable to set array's index, such as define statement When you do sales[1][0] you get a single value (row 1, column 0) and not all the row Use typedef to declare custom array with different dimensions, it is easier to handle them this way
If you want, you can change the function to return all the max of all the rows. If you want to get the max of the matrix, the approach is similar.
Upvotes: 1