Jump to content
  • Advertisement
Sign in to follow this  
Tiger99

find the max value

This topic is 4466 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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 > value)
{
value = array;
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!