Anna
Anna

Reputation: 83

how to find the value and index of max/min value in C++

I have an array and I want to get the max value and its index. I'm using this code:

#include <iostream>
#include <algorithm>    //max-element
using namespace std;
#define J 5

int main(int argc, char** argv)
...
double ucolumn[J]={};

for(j=0;j<J;j++)
    for(i=0;i<I;i++)
        ucolumn[j]+=u[i][j];

double q=*max_element(ucolumn[0],ucolumn[J]) << endl;
 return 0;
}

but it gives me an error of "illegal indirection", "mismatch in formal parameter list"

Upvotes: 0

Views: 695

Answers (1)

Dietmar K&#252;hl
Dietmar K&#252;hl

Reputation: 154035

You need to use the algoritm with iterator rather than valies:

std::max_element(ucolumn, ucolumn + J)

Upvotes: 1

Related Questions