2d array

How can I print 2D array each line biggest numbers to 1D array?
for each row in 2D
  get maximum value of row
  store value to 1D
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <algorithm>

int main()
{
    const int ROWS = 3;
    const int COLS = 5;

    int arr_2D[ROWS][COLS] =
    {
        {4, 2, 8, 19, 7},
        {12, 34, 2, 6, 15},
        {90, 2, 4, 23, 89}
    };

    int arr_1D[ROWS];

    for(int i = 0; i < ROWS; i++)
    {
        int m = arr_2D[i][0]; // set m (max) to first element the row
        for(int j = 0; j < COLS; j++)
        {
            m = std::max(m, arr_2D[i][j]); // find max of row
        }
        arr_1D[i] = m; // put max in 1D array
    }

    for(const int& i : arr_1D) std::cout << i << " ";
}
@Arslanwhatever
1
2
3
4
5
int m = arr_2D[i][0]; // set m (max) to first element the row
for(int j = 0; j < COLS; j++)
{
     m = std::max(m, arr_2D[i][j]); // find max of row
}


Should be :
1
2
3
4
5
int m = arr_2D[i][0]; // set m (max) to first element the row
for(int j = 1; j < COLS; j++)
{
     m = std::max(m, arr_2D[i][j]); // find max of row
}


The code needs a little bit tweaking so it can be perfect.

Edit : Hello, @Arslanwhatever.
Last edited on
@Sakurawhatever: You must be very bitter about our previous exchange to be "perfecting" my code like that. Why don't you get off the internet for a while. Logout option is on the top right.
Last edited on
@Arslanwhatever: You must be very bitter about our previous exchange to be caring about this small stuff like that. Why don't you get off the internet for a while. Logout option is on the top right.

P.S : Correction is a good thing you know.
If it really must be tweaked:
1
2
3
4
5
{
    using namespace std ;
    transform( begin(arr_2D), end(arr_2D), begin(arr_1D),
               [] ( const auto& row ) { return *max_element( begin(row), end(row) ); } );
}
@JLBorges: Nice, but I feel that may be out of the scope of the OP, which defeats the purpose of answering his question in the first place. When we answer a question, we should answer it on the level of the questioner.
Thank you! :)
When we answer a question, we should answer it on the level of the questioner.

How do you know the level of the questioner? There wasn't enough content to determine anything other that possibly the OP wanted someone (you?) to do his work for him. Good job, by the way.


@jib: How do I know the level of the questioner? By looking at the question. The question is relatively simple, therefore I inferred the questioner is a relative beginner. You'd think that as a programmer you'd be able to figure that out.
I'd think you'd be able to figure out the OP wasn't interested in learning, just interested in getting some fool to do the work for him.

I think you are making assumptions, while calling me and JLBorges fools. Congratulations.
Just to be accurate I'm not calling either JLBorges or SakurasouBusters a fool.
Really? Cause they sure do fit the description you gave:
...some fool to do the work for him.


JLBorges did the work, as did I.

But even so, I guess you are saying im the only fool because I deflated your initial argument of "how can you know the level of the questioner".
Last edited on
@Arslanwhatever
Really? Cause they sure do fit the description you gave :

Remember that I did not really participate in this. I just merely gave a correction.
Topic archived. No new replies allowed.