473,320 Members | 2,054 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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.

Nov 14 '05 #1
7 30409

"fdunne2" <fd*****@nospam.yahoo.ie> wrote in message
news:7d******************************@localhost.ta lkaboutprogramming.com...
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
Nov 14 '05 #2
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

Nov 14 '05 #3
Mike Wahler wrote:

"fdunne2" <fd*****@nospam.yahoo.ie> wrote in message
news:7d******************************@localhost.ta lkaboutprogramming.com...
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

Nov 14 '05 #4
In article <c0************@news.t-online.com>,
Kurt Watzka <wa****@stat.uni-muenchen.de> wrote:
Mike Wahler wrote:

"fdunne2" <fd*****@nospam.yahoo.ie> wrote in message

news:7d******************************@localhost.ta lkaboutprogramming.com...
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?

Nov 14 '05 #5
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.
Nov 14 '05 #6
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
Nov 14 '05 #7
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[i] > m) {
m = iarr[i];
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 ---
Nov 14 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Rade | last post by:
Please have a look at the following program: #include <iostream> template <const int array, size_t index> class ArrayIndex { public: static const int value = array; };
5
by: Robert Oschler | last post by:
I am converting a Perl script over to "C" for a potential open source project. I need some open source "C" code that will give me the same functionality of a Perl Style associative array: ...
4
by: Bill Sun | last post by:
Hi, All I have a conventional question, How to create a 3 dimension array by C language. I see some declare like this: int *** array; int value; array = create3Darray(m,n,l);
24
by: RyanTaylor | last post by:
I have a final coming up later this week in my beginning Java class and my prof has decided to give us possible Javascript code we may have to write. Problem is, we didn't really cover JS and what...
10
by: Ocean | last post by:
I'm a novice on ASP using VBScript, and I encountered a problem when I want to get and use a return value of a web method in my ASP code. There is a web method called "getQuote", which takes a...
2
by: sicapitan | last post by:
I'm trying to build some xml from an array with the help from another array. In one array I have $cells Array ( =CELL0
1
by: Neil Chambers | last post by:
I'm currently using multidimensional arrays in PoSH by assigning values to index positions. I'm doing this because AFAIK there is no way to initialise an array simply with the 'number' of...
3
by: katak | last post by:
hi... 1. how to do ranking value in array ? let said i hv 200000 sets of data need to store in an array after that i need to do ranking for the 200000 sets of data and find out the top 20 sets...
2
by: mndprasad | last post by:
Hi friends, Am new to AJAX coding, In my program am going to have two texbox which going to implent AJAX from same table. One box is going to retrieve the value of other and vice...
7
by: Christof Warlich | last post by:
Hi, the subject says it all: I need to instantiate an array of objects where each object "knows" its arrary index. So far, this is easy as long as index is not a compile-time constant: class ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.