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;
  }
}

This is where you went wrong.

In your program pointers min and max are there but are not pointing to any memory space at all.So when you try to access some memory as *min or *max they try to refer a memory not been allocated hence segmentation fault core dumped.

Try this:

int main( int argc, char *argv[] ){

int *numberlist, i, n, *min, *max ,minimum,maximum;

min=&minimum;
max=&maximum;
//Followed by your code

By this you will have allocated the memory and then accessing it so no problem. And ya your max and min values will be in maximum and minimum.

Purely for the sake of simplicity, I would go with this route (doesn't use a second set of variables):

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 );

Think about another approach:

struct IntRange { int minval, maxval; };

struct IntRange MinMax(const int* arr, int n)
{
    struct IntRange range = { 0, 0 };
    if (arr && 0 < n) {
        int i;
        range.minval = range.maxval = arr[0];
        for (i = 1; i < n; ++i) {
            if (arr[i] < range.minval)
                range.minval = arr[i];
            else if (range.maxval < arr[i])
                range.maxval = arr[i];
        }
    }
    return range;
};

int main()
{
    int* numberlist;
    struct Range range;
    ...
    range = MinMax(numberlist,n);
    printf("Min %d\nMax %d\n",range.minval.range.maxval);
	return 0;
}

i have to use that function minmax because that is the one that we were assigned to use for the class, and i made the changed death oclock gave, but it just prints out the min and max values are 0... any ideas?

Ark's approach is much neater and most likely works. Your solution is very hard to read; the looping approach makes more sense.

heres my iterative method, the full assigment is to test the recursive method, and iterative method to see when the recursive method begins to be faster then the iterative method. (time both with array sizes of 500, 5000, 50000, 500000 numbers). And at the moment this one also only prints out the min and max values are 0.

/*********************************************************************/
/* iterminmax.c                                                       */
/*********************************************************************/

#include <stdio.h>
#include <stdlib.h>

void minmax( int a[], 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( "\nThe minimum value in your array is: %d", min );
  printf( "\nThe maximum value in your array is: %d\n", max );


  return 0;
}

void minmax( int a[], int n, int *p_min, int *p_max ){

  int i;

  *p_max = *p_min = a[0];

  for( i = 1; i < n; i++ ){

    if( a[i] > *p_max )
      *p_max = a[i];

    if( a[i] < *p_min )
      *p_min = a[i];
  }
}

Look at csurfer's post again!
It was the true solution of your problems (in other words, it was a correction of your mistake ;))...

lol it doesnt matter whether i use csurfers advide or death_oclocks advise, both of those take away the segmentation fault. my issue getting a 0 is because in teh for statement i had a boolean expression instead of just an equal sign... oops. thanks u guys

edit: thanks mitrmakr i guess we both found that out at the same time

This question has already been answered. Start a new discussion instead.