ok so im creating a program minmax to find the minimum and maximum numbers of an array. heres my code but i get segmentation fault where do you think im going wrong?
#include <stdio.h>
#include <stdlib.h>
void minmax( int numberlist[], int n, int *p_min, int *p_max );
int main( int argc, char *argv[] ){
int *numberlist, i, n, *min, *max ;
n = atoi( argv[1] );
numberlist = ( int * ) malloc( n * sizeof( int ) );
for( i = 0; i < n; i++ ) numberlist[i] == rand() % n ;
minmax( numberlist, n, min, max );
printf( "The minimum value in your array is: %d", *min );
printf( "The maximum value in your array is: %d", *max );
return 0;
}
void minmax( int numberlist[], int n, int *p_min, int *p_max ){
int min2, max2, i;
if( n == 1 ) *p_min = *p_max = numberlist[0];
else if( n == 2 ) {
if( numberlist[0] < numberlist[i] ) {
*p_min = numberlist[0];
*p_max = numberlist[1];
} else {
*p_min = numberlist[1];
*p_max = numberlist[0];
}
} else {
minmax( numberlist, n / 2, p_min, p_max );
minmax( numberlist + n / 2, n - ( n / 2 ), &min2, &max2 );
if( min2 < *p_min )
*p_min = min2;
if( max2 > *p_max )
*p_max = max2;
}
}