minmax

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Dec 2008
Posts: 96
Reputation: Dewey1040 is an unknown quantity at this point 
Solved Threads: 3
Dewey1040 Dewey1040 is offline Offline
Junior Poster in Training

minmax

 
0
  #1
Mar 30th, 2009
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?

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. void minmax( int numberlist[], int n, int *p_min, int *p_max );
  5.  
  6. int main( int argc, char *argv[] ){
  7.  
  8. int *numberlist, i, n, *min, *max ;
  9.  
  10. n = atoi( argv[1] );
  11.  
  12. numberlist = ( int * ) malloc( n * sizeof( int ) );
  13.  
  14. for( i = 0; i < n; i++ ) numberlist[i] == rand() % n ;
  15.  
  16. minmax( numberlist, n, min, max );
  17.  
  18. printf( "The minimum value in your array is: %d", *min );
  19. printf( "The maximum value in your array is: %d", *max );
  20. return 0;
  21. }
  22.  
  23. void minmax( int numberlist[], int n, int *p_min, int *p_max ){
  24.  
  25. int min2, max2, i;
  26.  
  27. if( n == 1 ) *p_min = *p_max = numberlist[0];
  28.  
  29. else if( n == 2 ) {
  30.  
  31. if( numberlist[0] < numberlist[i] ) {
  32. *p_min = numberlist[0];
  33. *p_max = numberlist[1];
  34.  
  35. } else {
  36. *p_min = numberlist[1];
  37. *p_max = numberlist[0];
  38. }
  39.  
  40. } else {
  41. minmax( numberlist, n / 2, p_min, p_max );
  42. minmax( numberlist + n / 2, n - ( n / 2 ), &min2, &max2 );
  43.  
  44. if( min2 < *p_min )
  45. *p_min = min2;
  46.  
  47. if( max2 > *p_max )
  48. *p_max = max2;
  49. }
  50. }
Last edited by Dewey1040; Mar 30th, 2009 at 11:03 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 476
Reputation: csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice 
Solved Threads: 76
csurfer's Avatar
csurfer csurfer is offline Offline
Posting Pro in Training

Re: minmax

 
0
  #2
Mar 30th, 2009
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:
  1. int main( int argc, char *argv[] ){
  2.  
  3. int *numberlist, i, n, *min, *max ,minimum,maximum;
  4.  
  5. min=&minimum;
  6. max=&maximum;
  7. //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.
Last edited by csurfer; Mar 30th, 2009 at 1:50 pm.
I Surf in "C"....
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 96
Reputation: Dewey1040 is an unknown quantity at this point 
Solved Threads: 3
Dewey1040 Dewey1040 is offline Offline
Junior Poster in Training

Re: minmax

 
0
  #3
Mar 30th, 2009
thanks csurfer, ill try that as soon as putty chooses to work again
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 357
Reputation: death_oclock will become famous soon enough death_oclock will become famous soon enough 
Solved Threads: 37
death_oclock's Avatar
death_oclock death_oclock is offline Offline
Posting Whiz

Re: minmax

 
0
  #4
Mar 30th, 2009
Purely for the sake of simplicity, I would go with this route (doesn't use a second set of variables):

  1. int main( int argc, char *argv[] ){
  2. int *numberlist, i, n, min, max ;
  3.  
  4. n = atoi( argv[1] );
  5. numberlist = ( int * ) malloc( n * sizeof( int ) );
  6. for( i = 0; i < n; i++ ) numberlist[i] == rand() % n ;
  7.  
  8. minmax( numberlist, n, &min, &max );
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: minmax

 
0
  #5
Mar 30th, 2009
Think about another approach:
  1. struct IntRange { int minval, maxval; };
  2.  
  3. struct IntRange MinMax(const int* arr, int n)
  4. {
  5. struct IntRange range = { 0, 0 };
  6. if (arr && 0 < n) {
  7. int i;
  8. range.minval = range.maxval = arr[0];
  9. for (i = 1; i < n; ++i) {
  10. if (arr[i] < range.minval)
  11. range.minval = arr[i];
  12. else if (range.maxval < arr[i])
  13. range.maxval = arr[i];
  14. }
  15. }
  16. return range;
  17. };
  18.  
  19. int main()
  20. {
  21. int* numberlist;
  22. struct Range range;
  23. ...
  24. range = MinMax(numberlist,n);
  25. printf("Min %d\nMax %d\n",range.minval.range.maxval);
  26. return 0;
  27. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 96
Reputation: Dewey1040 is an unknown quantity at this point 
Solved Threads: 3
Dewey1040 Dewey1040 is offline Offline
Junior Poster in Training

Re: minmax

 
0
  #6
Mar 30th, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 357
Reputation: death_oclock will become famous soon enough death_oclock will become famous soon enough 
Solved Threads: 37
death_oclock's Avatar
death_oclock death_oclock is offline Offline
Posting Whiz

Re: minmax

 
0
  #7
Mar 30th, 2009
Ark's approach is much neater and most likely works. Your solution is very hard to read; the looping approach makes more sense.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 96
Reputation: Dewey1040 is an unknown quantity at this point 
Solved Threads: 3
Dewey1040 Dewey1040 is offline Offline
Junior Poster in Training

Re: minmax

 
0
  #8
Mar 31st, 2009
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.

  1. /*********************************************************************/
  2. /* iterminmax.c */
  3. /*********************************************************************/
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. void minmax( int a[], int n, int *p_min, int *p_max );
  9.  
  10. int main( int argc, char *argv[] ){
  11.  
  12. int *numberlist, i, n, min, max;
  13.  
  14. n = atoi( argv[1] );
  15.  
  16. numberlist = ( int * ) malloc( n * sizeof( int ) );
  17.  
  18. for( i = 0; i < n; i++ ) numberlist[i] == rand() % n ;
  19.  
  20. minmax( numberlist, n, &min, &max );
  21.  
  22. printf( "\nThe minimum value in your array is: %d", min );
  23. printf( "\nThe maximum value in your array is: %d\n", max );
  24.  
  25.  
  26. return 0;
  27. }
  28.  
  29. void minmax( int a[], int n, int *p_min, int *p_max ){
  30.  
  31. int i;
  32.  
  33. *p_max = *p_min = a[0];
  34.  
  35. for( i = 1; i < n; i++ ){
  36.  
  37. if( a[i] > *p_max )
  38. *p_max = a[i];
  39.  
  40. if( a[i] < *p_min )
  41. *p_min = a[i];
  42. }
  43. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: minmax

 
0
  #9
Mar 31st, 2009
Look at csurfer's post again!
It was the true solution of your problems (in other words, it was a correction of your mistake )...
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 978
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 208
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: minmax

 
0
  #10
Mar 31st, 2009
There is an initialization failure ...
  for( i = 0; i < n; i++ ) numberlist[i] == rand() % n ;
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC