Assuming an unsorted array of course I would do something like this:
Code:
#include <stdio.h>
int largest(const int *, int, int);
int main(void)
{
int arr[] = { 1, 8, 29, 3, 9, 23, 94, 2, 9};
printf("Largest: %d\n", largest(arr, 0, (sizeof arr / sizeof arr[0]) - 1));
return 0;
}
int largest (const int *arr, int low, int high)
{
int mid, ret1, ret2;
/* Since there is only one element it must be the highest */
if ( low == high )
return arr[low];
/* Find the mid point of the array */
mid = ((high - low) / 2) + low;
/* Get the largest in the first half */
ret1 = largest(arr, low, mid);
/* Get the largest in the last half */
ret2 = largest(arr, mid+1, high);
/* Return the largest of the two values */
return ret1 > ret2 ? ret1 : ret2;
}
The biggest problem I see with your code (besides formating) is that you have assignment statements that change the array. Since all you are doing is finding the largest value you should not change the values inside the array. To keep yourself from doing this change
Code:
int largest(int *array, int pntrA, int pntrB)
to
Code:
int largest(const int *array, int pntrA, int pntrB)
I think you were on the right track but it seems that you are assuming some type of sorting.