954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

minmax

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;
  }
}
Dewey1040
Junior Poster
133 posts since Dec 2008
Reputation Points: 17
Solved Threads: 3
 

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.

csurfer
Posting Pro
568 posts since Jan 2009
Reputation Points: 485
Solved Threads: 88
 

thanks csurfer, ill try that as soon as putty chooses to work again

Dewey1040
Junior Poster
133 posts since Dec 2008
Reputation Points: 17
Solved Threads: 3
 

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 );
death_oclock
Posting Whiz
393 posts since Apr 2006
Reputation Points: 129
Solved Threads: 45
 

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;
}
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
 

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?

Dewey1040
Junior Poster
133 posts since Dec 2008
Reputation Points: 17
Solved Threads: 3
 

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

death_oclock
Posting Whiz
393 posts since Apr 2006
Reputation Points: 129
Solved Threads: 45
 

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];
  }
}
Dewey1040
Junior Poster
133 posts since Dec 2008
Reputation Points: 17
Solved Threads: 3
 

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

ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
 

There is an initialization failure ...

for( i = 0; i < n; i++ ) numberlist[i] == rand() % n ;
mitrmkar
Posting Virtuoso
1,809 posts since Nov 2007
Reputation Points: 1,105
Solved Threads: 395
 

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

Dewey1040
Junior Poster
133 posts since Dec 2008
Reputation Points: 17
Solved Threads: 3
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You