max value in an array

Discussion in 'C Programming' started by Mike Wahler, Feb 13, 2004.

  1. Mike Wahler

    Mike Wahler Guest

    "fdunne2" <> wrote in message
    news:...
    > Hi,
    >
    > Is there a function in C that returns the max value in an array?


    No there is not, but it should be trivial to write one.


    > I need a function that returns the max value and the corresponding array

    index.

    Just create a temp variable, set it to the value of element zero,
    traverse the array, setting the temp to each value if it's greater.
    Track the index the same way, with a temp variable.

    >
    > Also, when reading float values from a binary file how do you know when

    you've reached the end of the file?

    It doesn't matter how you're interpreting the data, in any case,
    most of the i/o functions return EOF upon an a attempt to read past
    end of file.

    -Mike
    Mike Wahler, Feb 13, 2004
    #1
    1. Advertising

  2. Mike Wahler

    fdunne2 Guest

    Hi,

    Is there a function in C that returns the max value in an array? I need a function that returns the max value and the corresponding array index.

    Also, when reading float values from a binary file how do you know when you've reached the end of the file?

    Regards,
    Fergal.
    fdunne2, Feb 13, 2004
    #2
    1. Advertising

  3. fdunne2 wrote:
    > Hi,
    >
    > Is there a function in C that returns the max value in an array? I need a function that returns the max value and the corresponding array index.


    No, but it is trivial to write one.

    int max_value(VALUE_TYPE * p_array,
    unsigned int values_in_array,
    VALUE_TYPE * p_max_value)
    {
    int position;

    position = 0;
    *p_max_value = p_array[position];
    for (position = 1; position < values_in_array; ++position)
    {
    if (p_array[position] > *p_max_value)
    {
    *p_max_value = p_array[position];
    break;
    }
    }
    return position;
    }

    Since you didn't specify the type of the array,
    I used VALUE_TYPE. If the array is of float,
    then VALUE_TYPE will be float.

    This could be simplified by returning the position or
    index of the maximum value. The maximum value can then
    be retreived from the array using the position:
    maximum_value = array[position];

    Besure to check the C FAQ section about arrays:
    http://www.eskimo.com/~scs/c-faq/s6.html


    > Also, when reading float values from a binary file how do you know when you've reached the end of the file?
    >
    > Regards,
    > Fergal.
    >


    As far as detecting end of file, check the C language FAQ:
    http://www.eskimo.com/~scs/c-faq/s12.html

    A very good idea is to always check the FAQ first, and
    search the newsgroups before posting.

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.raos.demon.uk/acllc-c /faq.html
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
    Thomas Matthews, Feb 13, 2004
    #3
  4. Mike Wahler

    Kurt Watzka Guest

    Mike Wahler wrote:

    >
    > "fdunne2" <> wrote in message
    >

    news:...
    >> Hi,
    >>
    >> Is there a function in C that returns the max value in an array?

    >
    > No there is not, but it should be trivial to write one.
    >
    >
    >> I need a function that returns the max value and the corresponding array

    > index.
    >
    > Just create a temp variable, set it to the value of element zero,
    > traverse the array, setting the temp to each value if it's greater.
    > Track the index the same way, with a temp variable.


    This will probably not find the maximum value of { -7, -3, -1, -10 }.
    Better start with the value of the first element.

    >
    >>
    >> Also, when reading float values from a binary file how do you know when

    > you've reached the end of the file?
    >
    > It doesn't matter how you're interpreting the data, in any case,
    > most of the i/o functions return EOF upon an a attempt to read past
    > end of file.


    OBC: Or in case of an I/O error. After fread(), fscanf(), fgetc() or
    similar functions return EOF, feof() and ferror() are handy tools
    to determine the reason for the failure to read another item/token/byte.

    Kurt Watzka
    Kurt Watzka, Feb 13, 2004
    #4
  5. Mike Wahler

    Clark Cox Guest

    In article <c0jbkg$fu$04$-online.com>,
    Kurt Watzka <-muenchen.de> wrote:

    > Mike Wahler wrote:
    >
    > >
    > > "fdunne2" <> wrote in message
    > >

    > news:...
    > >> Hi,
    > >>
    > >> Is there a function in C that returns the max value in an array?

    > >
    > > No there is not, but it should be trivial to write one.
    > >
    > >
    > >> I need a function that returns the max value and the corresponding array

    > > index.
    > >
    > > Just create a temp variable, set it to the value of element zero,
    > > traverse the array, setting the temp to each value if it's greater.
    > > Track the index the same way, with a temp variable.

    >
    > This will probably not find the maximum value of { -7, -3, -1, -10 }.


    Why not?
    Clark Cox, Feb 13, 2004
    #5
  6. Thomas Matthews wrote:
    > fdunne2 wrote:
    >> Hi,
    >>
    >> Is there a function in C that returns the max value in an array? I
    >> need a function that returns the max value and the corresponding
    >> array index.

    >
    > No, but it is trivial to write one.


    Perhaps not /that/ trivial :).

    > int max_value(VALUE_TYPE * p_array,
    > unsigned int values_in_array,
    > VALUE_TYPE * p_max_value)
    > {
    > int position;
    >
    > position = 0;
    > *p_max_value = p_array[position];
    > for (position = 1; position < values_in_array; ++position)
    > {
    > if (p_array[position] > *p_max_value)
    > {
    > *p_max_value = p_array[position];
    > break;
    > }
    > }
    > return position;
    > }


    This function returns the position of the first value in the array
    that's greater than the first value in the array. Here's a version
    that returns a pointer to the maximum element:

    T *maxelem(const T *array, size_t size) {
    const T *pos = array, *end = array + size;
    for (; array != end; array++) {
    if (*array > *pos) {
    pos = array;
    }
    }
    return (T *)pos;
    }

    Jeremy.
    Jeremy Yallop, Feb 13, 2004
    #6
  7. Mike Wahler

    pete Guest

    Kurt Watzka wrote:
    >
    > Mike Wahler wrote:


    > > Just create a temp variable, set it to the value of element zero,


    > Better start with the value of the first element.


    Element zero, is the first element.
    array[0]

    --
    pete
    pete, Feb 13, 2004
    #7
  8. Mike Wahler

    Joe Wright Guest

    fdunne2 wrote:
    >
    > Hi,
    >
    > Is there a function in C that returns the max value in an array? I need a function that returns the max value and the corresponding array index.
    >

    #include <stdio.h>
    int main(void) {
    int iarr[] = { -7, -3, -1, -10 };
    int siz = sizeof iarr / sizeof iarr[0];
    int m, i, x = 0;
    m = iarr[0];
    for (i = 1; i < siz; ++i)
    if (iarr > m) {
    m = iarr;
    x = i;
    }
    printf("The greatest of %d values is %d at index %d\n", siz, m, x);
    return 0;
    }

    > Also, when reading float values from a binary file how do you know when you've reached the end of the file?


    Assuming you are reading them one at a time, your fread() will return 0
    instead of 1.
    >
    > Regards,
    > Fergal.


    --
    Joe Wright http://www.jw-wright.com
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
    Joe Wright, Feb 14, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Summercool
    Replies:
    9
    Views:
    871
    dorayme
    Oct 23, 2007
  2. Greg Ferris

    Textarea max rows and max characters per row

    Greg Ferris, Jan 16, 2004, in forum: Javascript
    Replies:
    2
    Views:
    567
    Greg Ferris
    Jan 16, 2004
  3. IJALAB
    Replies:
    16
    Views:
    228
  4. kriton
    Replies:
    0
    Views:
    329
    kriton
    Apr 22, 2005
  5. timothytoe

    Help. Finding max value in array of objects.

    timothytoe, Feb 6, 2008, in forum: Javascript
    Replies:
    18
    Views:
    172
    Dr J R Stockton
    Feb 10, 2008
Loading...

Share This Page