Sign in to follow this  
Tiger99

find the max value

Recommended Posts

Hi I was wondering is there a build in max function in C that return the max value and index of an array? If there is none, does anyone know the best way to create one? Thanks in advance for your help

Share this post


Link to post
Share on other sites
int max_value = 0;
int max_index = -1;

for (int x = 0 ; x < ARRAY_MAX ; x++)
{
If (array[x] > max_value)
{
max_value = array[x];
max_index = x;
}
}

Now you have the max_value and max value index.

Share this post


Link to post
Share on other sites

int index = -1;
int value = 0x80000000; // lowest possible value
for (int i=0; i<length; i++)
{
if (array[i] > value)
{
value = array[i];
index = i;
}
}


if the array is sorted, then this reduces to:


int index = length-1;
int value = array[index];

;-)

Share this post


Link to post
Share on other sites
You should initialize with the first value in the array, rather than some arbitrary constant.

int* find_max(int* array, unsigned int length)
{
int* max = (length != 0) ? array : NULL;
for(int* ptr=array; ptr != array+length; ++ptr)
if(*ptr > *max) max = ptr;

return max;
}


In C++, you'd just use std::max_element from <algorithm>.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Hi
I was wondering is there a build in max function in C that return the max value and index of an array?
If there is none, does anyone know the best way to create one?

Is this all you need or do you need to look at this image?
http://i35.photobucket.com/albums/d196/tometoyou/containerchoice.png

Share this post


Link to post
Share on other sites
What you want is a container class.

Look up STL's Vector class on this page and on Google.

In general, vectors (some languages call them Array Lists or just Lists) are "smart" arrays which usually have the following properties:

* They remember their size information (also called the "length").

* They are dynamic (they can expand and contract).

* They often perform bounds checking (throwing an exception when you try to access before 0 or beyond the length).

* They are almost always what you want to use. Arrays make for many more errors.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Fruny
The OP mentioned C, not C++.


true, but this still stands as you create the structures.
Quote:
Is this all you need or do you need to look at this image?

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
true, but this still stands as you create the structures.
Quote:
Is this all you need or do you need to look at this image?


Oh, I don't dispute that, I just want to keep things on track, at least until we hear more from the OP: he didn't ask about containers, just about finding the largest element of an array.[smile]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this