max value in an array 
November 14th, 2005, 02:28 AM
| | | max value in an array
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. | 
November 14th, 2005, 02:28 AM
| | | Re: max value in an array
"fdunne2" <fdunne2@nospam.yahoo.ie> wrote in message
news:7d7d547488009092e5dde0c90badeb7a@localhost.ta lkaboutprogramming.com...[color=blue]
> Hi,
>
> Is there a function in C that returns the max value in an array?[/color]
No there is not, but it should be trivial to write one.
[color=blue]
> I need a function that returns the max value and the corresponding array[/color]
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.
[color=blue]
>
> Also, when reading float values from a binary file how do you know when[/color]
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 | 
November 14th, 2005, 02:29 AM
| | | Re: max value in an array
fdunne2 wrote:[color=blue]
> 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.[/color]
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
[color=blue]
> Also, when reading float values from a binary file how do you know when you've reached the end of the file?
>
> Regards,
> Fergal.
>[/color]
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 | 
November 14th, 2005, 02:29 AM
| | | Re: max value in an array
Mike Wahler wrote:
[color=blue]
>
> "fdunne2" <fdunne2@nospam.yahoo.ie> wrote in message
>[/color]
news:7d7d547488009092e5dde0c90badeb7a@localhost.ta lkaboutprogramming.com...[color=blue][color=green]
>> Hi,
>>
>> Is there a function in C that returns the max value in an array?[/color]
>
> No there is not, but it should be trivial to write one.
>
>[color=green]
>> I need a function that returns the max value and the corresponding array[/color]
> 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.[/color]
This will probably not find the maximum value of { -7, -3, -1, -10 }.
Better start with the value of the first element.
[color=blue]
>[color=green]
>>
>> Also, when reading float values from a binary file how do you know when[/color]
> 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.[/color]
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 | 
November 14th, 2005, 02:29 AM
| | | Re: max value in an array
In article <c0jbkg$fu$04$2@news.t-online.com>,
Kurt Watzka <watzka@stat.uni-muenchen.de> wrote:
[color=blue]
> Mike Wahler wrote:
>[color=green]
> >
> > "fdunne2" <fdunne2@nospam.yahoo.ie> wrote in message
> >[/color]
> news:7d7d547488009092e5dde0c90badeb7a@localhost.ta lkaboutprogramming.com...[color=green][color=darkred]
> >> Hi,
> >>
> >> Is there a function in C that returns the max value in an array?[/color]
> >
> > No there is not, but it should be trivial to write one.
> >
> >[color=darkred]
> >> I need a function that returns the max value and the corresponding array[/color]
> > 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.[/color]
>
> This will probably not find the maximum value of { -7, -3, -1, -10 }.[/color]
Why not? | 
November 14th, 2005, 02:29 AM
| | | Re: max value in an array
Thomas Matthews wrote:[color=blue]
> fdunne2 wrote:[color=green]
>> 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.[/color]
>
> No, but it is trivial to write one.[/color]
Perhaps not /that/ trivial :-).
[color=blue]
> 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;
> }[/color]
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. | 
November 14th, 2005, 02:29 AM
| | | Re: max value in an array
Kurt Watzka wrote:[color=blue]
>
> Mike Wahler wrote:[/color]
[color=blue][color=green]
> > Just create a temp variable, set it to the value of element zero,[/color][/color]
[color=blue]
> Better start with the value of the first element.[/color]
Element zero, is the first element.
array[0]
--
pete | 
November 14th, 2005, 02:29 AM
| | | Re: max value in an array
fdunne2 wrote:[color=blue]
>
> 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.
>[/color]
#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[i] > m) {
m = iarr[i];
x = i;
}
printf("The greatest of %d values is %d at index %d\n", siz, m, x);
return 0;
}
[color=blue]
> Also, when reading float values from a binary file how do you know when you've reached the end of the file?[/color]
Assuming you are reading them one at a time, your fread() will return 0
instead of 1.[color=blue]
>
> Regards,
> Fergal.[/color]
--
Joe Wright http://www.jw-wright.com
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein --- | | Thread Tools | Search this Thread | | | |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 220,662 network members.
|