[OCLUG-devel] ArrayMaxValue() - how could I improve it?
James Colannino
james at colannino.org
Wed Jun 2 22:27:11 PDT 2004
The following is a function I whipped up to answer one of the
programming exercises in my book which asked for a function capable of
returning the highest value in an array. Since I'm new to programming
and haven't been exposed to a lot of the other functions that are out
there lurking in the standard libs, and since other people have
different ways of doing things, I was just wondering if anybody sees
where I could improve upon this. I know that one limitation I'd like to
do away with is the fact that instead of just parsing through the array
until the user's input is finished, it's going to go all the way up to
element 20, even if the user only enters two or three numbers. Also,
how does my coding style look? Are my comments too verbose? Does
anything about the way I make use of whitespace make things more
confusing? If I'm starting off my stay on OCLUG-devel by asking too
many questions feel free to let me know :-P C is just really exciting
to me right now and I have so many things I want to learn and so many
questions I want to ask...
James
/******************************************************\
* This function parses through the elements in an *
* array and returns the largest value. the variable *
* 'number' is assigned each element in the array and *
* is compared to the variable 'MaxValue' which *
* contains the highest value found in each pass of *
* the loop. The loop breaks when array[position] is *
* greater than the number of elements in the array. *
* The result is then returned to main(). *
\******************************************************/
ArrayMaxValue(int number, int array[20])
/* array[ ] can hold a maximum of 20 elements */
{
int position = 0; /* our current position in the array */
int MaxValue = 0; /* stores the highest value found */
while (array[position] <= ( sizeof(array) / sizeof( array[0] )){
number = array[position];
/* store the current element in 'number' */
if (number > MaxValue)
/* if the current element is greater... */
MaxValue = number;
/* then assign 'number' to 'MaxValue' */
}
return (MaxValue);
}
--
My blog: http://www.crazydrclaw.com/
My homepage: http://james.colannino.org/
"There are no uninteresting things; only uninterested people." --G.K.
Chesterton
More information about the OCLUG-devel
mailing list