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 << " ";
}
|