How to find the min and max of a set of numbers in C without loops?
Lets say I have 10 numbers (doubles) and I have to find the sma开发者_Go百科llest number and the biggest number without using loops, how would I do so?
In psuedo-code, in case this is homework:
min = arr[0]
max = arr[0]
for n in 1..size(arr)-1:
if arr[n] > max:
max = arr[n]
if arr[n] < min:
min = arr[n]
If, for some reason, you can't use loops (and this certainly marks it as homework - no-one in their right mind would try this without a loop), just unroll the loop:
min = arr[0]
if arr[1] < min min = arr[1]
if arr[2] < min min = arr[2]
if arr[3] < min min = arr[3]
if arr[4] < min min = arr[4]
if arr[5] < min min = arr[5]
if arr[6] < min min = arr[6]
if arr[7] < min min = arr[7]
if arr[8] < min min = arr[8]
if arr[9] < min min = arr[9]
max = arr[0]
if arr[1] > max max = arr[1]
if arr[2] > max max = arr[2]
if arr[3] > max max = arr[3]
if arr[4] > max max = arr[4]
if arr[5] > max max = arr[5]
if arr[6] > max max = arr[6]
if arr[7] > max max = arr[7]
if arr[8] > max max = arr[8]
if arr[9] > max max = arr[9]
That's not too bad for ten entries but it's going to get cumbersome as the number rises.
Or a recursive solution for the more bizarrely-minded of us :-)
def findMax (arr, cur, idx):
if idx < 0:
return cur
if arr[idx] > cur
return findMax (arr, arr[idx], idx-1)
return findMax (arr, cur, idx-1)
def findMin (arr, cur, idx):
if idx < 0:
return cur
if arr[idx] < cur
return findMin (arr, arr[idx], idx-1)
return findMin (arr, cur, idx-1)
max = findMax (arr, arr[9], 8)
min = findMin (arr, arr[9], 8)
But I wouldn't hand that recursive solution in - if you haven't done loops yet, it's probably well beyond the level at which your class is operating.
And, since the recursive solution is a rather nifty, no-loop, solution (and you have a near-zero chance of using it and not being found out as a plagiarist), here it is:
#include <stdio.h>
static int findMin (int *arr, int cur, int idx) {
if (idx < 0)
return cur;
if (arr[idx] < cur)
return findMin (arr, arr[idx], idx-1);
return findMin (arr, cur, idx-1);
}
static int findMax (int *arr, int cur, int idx) {
if (idx < 0)
return cur;
if (arr[idx] > cur)
return findMax (arr, arr[idx], idx-1);
return findMax (arr, cur, idx-1);
}
int main (void) {
int nums[] = {27,18,28,18,28,45,93,14,15,92,65,35,89};
int min, max;
int x = sizeof(nums) / sizeof(*nums) - 1;
max = findMax (nums, nums[x], x-1);
min = findMin (nums, nums[x], x-1);
printf ("min=%d, max=%d\n", min, max);
return 0;
}
This outputs:
min=14, max=93
as expected but don't use it on large lists since you'll probably run out of stack space.
Biggest number recursively:
double fun( double * ar, uint idx, uint size, double max )
{
double rv;
if( idx < size )
{
if( max < ar[idx] ) { rv = fun( ar, 1 + idx, size, ar[idx] ); }
else { rv = fun( ar, 1 + idx, size, max ); }
}
else { rv = max; }
return rv;
}
Invocation:
double ray[] = { .0, -16.0, 2.0, -4.0, 8.0, -1.0, 7.0, -3.0, 5.0, -10.0 };
printf( "max %lf\n", fun( ray, 1, sizeof( ray ) / sizeof( ray[0] ), ray[0] ) );
Smallest is almost the same:
if( ar[idx] < min ) { rv = fun( ar, 1 + idx, size, ar[idx] ); }
精彩评论