[ILUG] OT: max function

Caolan McNamara cmc at stardivision.de
Mon May 22 13:33:40 IST 2000


>>>>>>>>>>>>>>>>>> Original Message <<<<<<<<<<<<<<<<<<

On 22.05.00, 12:32:49, "John P. Looney (Kate)" <jplooney-ilug at online.ie> 
wrote regarding Re: [ILUG] OT: max function:

> int arraymax(int *array, int arraysize)
> {
>     int i;
>     int max= (*array);  //Max is first element in the array

>     for(i=1;i<arraysize;i++) { //From the 2nd element, to the end
>         if(*(array+i)<max) {   //If what we are looking at is bigger than 
max...
>             max=*(array+i);
>         }
>     }
> }

"if(*(array+i)<max)"

What the!, if the contents of this array element is LESS than max !!, 
you want if(*(array+i)>max)

and theres no need to go bezerk with the brackety pointer arithmetic, 
square brackets are quite acceptable

int arraymax(int *array, int arraysize)
{
	int i;
	int max=array[0]; 

	for(i=1;i<arraysize;i++)
		if (array[i] > max)
			max = array[i];
	return max;
}

And while Im at it those look suspiciously like c++ comments in a C 
program. Getting crotchety in my old age. 

C.





More information about the ILUG mailing list