Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ function to find max value in an array of doubles?

I frequently find myself writing max value functions that search through an array of doubles I use functions like these to normalize data before graphic display.

Is there a better way to find the max value of an array of doubles? Is there a standard function to find the max value in an array? Are there some intrinsic for this operation? I remember specialized ASM instruction existed in DSP chips.

like image 937
Mikhail Avatar asked Mar 11 '12 07:03

Mikhail


People also ask

What is the maximum double value in C?

The value of this constant is positive 1.7976931348623157E+308.

How do you find the maximum number of elements in two arrays?

Use a hash to pick unique n maximum elements of both arrays, giving priority to A[]. Initialize result array as empty. Traverse through A[], copy those elements of A[] that are present in the hash. This is done to keep the order of elements the same.

Can a double be an array in C?

You can declare an array of any data type (i.e. int, float, double, char) in C.


1 Answers

Yep! There's a function called std::max_element:

double arr[LENGTH] = /* ... */
double max = *std::max_element(arr, arr + LENGTH);

You'll need to #include <algorithm> to do this. That header has a whole bunch of goodies in it, and it's worth taking the time to learn more about the STL containers and algorithms libraries, as they can make your life so much easier.

As long as we're on the subject, consider looking into std::vector or std::array as replacements for raw C++ arrays. They're safer and a bit easier to use.

Hope this helps!

like image 79
templatetypedef Avatar answered Oct 25 '22 04:10

templatetypedef