finding the index of max element in array

Hi guys, i try to find the index of max value of an array, like this

{2,3,1,6,7,3}

my code:

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <algorithm>

using namespace std;

int const dimensi = 6;
int k, matrik[dimensi] = {2,3,1,6,7,3};

int main()
{
	 cout<< max_element(matrik,matrik);
}


i thought the output should be 4, (because first element starting with index 0).

but when i ran the program the output :
0x481004

is that memory location? how can i get index's number output?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <algorithm>
#include <iterator>

using namespace std;

int const dimensi = 6;
int k, matrik[dimensi] = {2,3,1,6,7,3};

int main()
{
	 std::cout << "Max element: " << *max_element(matrik, matrik + sizeof(matrik)/sizeof(matrik[0])) << "\n";
	 std::cout << "Max element location: " <<
        std::distance(matrik, max_element(matrik, matrik + sizeof(matrik)/sizeof(matrik[0]))) << "\n";
}
Thank you Gunnerfunner
one more question, if i use 2 dimentional array how to print out the index?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <algorithm>
#include <iterator>

using namespace std;

int const dimensi = 3;
int k, matrik[dimensi][dimensi] = {{2,13,0},{13,4,6},{0,6,7}};

int main()
{
	 std::cout << "Max element: " << *max_element(matrik, matrik + sizeof(matrik)/sizeof(matrik[0][0])) << "\n";
	 std::cout << "Max element location: " <<
     std::distance(matrik, max_element(matrik, matrik + sizeof(matrik)/sizeof(matrik[0][0]))) << "\n";

	cout << sizeof(matrik)/sizeof(matrik[0][0]);
}


this code doesnt work in 2D
Last edited on
Apply std::max_element() to each row and save the result in the variable, say global_max. After first row, global_max is whatever std::max_element returns from the first row, now check the second row and update global_max if std::max_element(second row) > global_max and so on …
at the same time keep a running tab on the position of the element with std::distance() and update that as/when global_max is updated – there will be 2 std::distance() calls – one for the rows and one for the cols
sorry for not putting in some code, i'm going into a skype for the next hour or so, if you still have the problems after that drop a line
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>
#include <tuple>

// return the position - pair (row,col) - containing the max element
template < typename T, std::size_t NROWS, std::size_t NCOLS >
std::pair< std::size_t, std::size_t > pos_max_element( T (&matrix)[NROWS][NCOLS] )
{
    const auto ptr = std::max_element( std::begin( matrix[0] ), std::end( matrix[NROWS-1] ) ) ;
    const auto pos = ptr - std::begin( matrix[0] ) ;
    return { pos/NCOLS, pos%NCOLS } ;
}

int main()
{
    const int a[5][6] =
    {
        { 12, 34, 78, 92, 51, 40 },
        { 12, 34, 78, 92, 51, 40 },
        { 12, 34, 78, 92, 51, 40 },
        { 93, 34, 78, 92, 99, 40 },
        { 12, 34, 78, 92, 95, 40 }
    };

    std::size_t row, col ;
    std::tie(row,col) = pos_max_element(a) ; // unpack the pair into row,col

    std::cout << "max element is a[" << row << "][" << col << "] == " << a[row][col] << '\n' ;
}

http://coliru.stacked-crooked.com/a/d2ffd28b1e891ad5
Last edited on
Topic archived. No new replies allowed.