943,696 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 1067
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 30th, 2009
0

minmax

Expand Post »
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.
Similar Threads
Reputation Points: 17
Solved Threads: 3
Junior Poster
Dewey1040 is offline Offline
126 posts
since Dec 2008
Mar 30th, 2009
0

Re: minmax

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.
Reputation Points: 485
Solved Threads: 88
Posting Pro
csurfer is offline Offline
564 posts
since Jan 2009
Mar 30th, 2009
0

Re: minmax

thanks csurfer, ill try that as soon as putty chooses to work again
Reputation Points: 17
Solved Threads: 3
Junior Poster
Dewey1040 is offline Offline
126 posts
since Dec 2008
Mar 30th, 2009
0

Re: minmax

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 );
Reputation Points: 128
Solved Threads: 43
Posting Whiz
death_oclock is offline Offline
389 posts
since Apr 2006
Mar 30th, 2009
0

Re: minmax

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. }
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Mar 30th, 2009
0

Re: minmax

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?
Reputation Points: 17
Solved Threads: 3
Junior Poster
Dewey1040 is offline Offline
126 posts
since Dec 2008
Mar 30th, 2009
0

Re: minmax

Ark's approach is much neater and most likely works. Your solution is very hard to read; the looping approach makes more sense.
Reputation Points: 128
Solved Threads: 43
Posting Whiz
death_oclock is offline Offline
389 posts
since Apr 2006
Mar 31st, 2009
0

Re: minmax

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. }
Reputation Points: 17
Solved Threads: 3
Junior Poster
Dewey1040 is offline Offline
126 posts
since Dec 2008
Mar 31st, 2009
0

Re: minmax

Look at csurfer's post again!
It was the true solution of your problems (in other words, it was a correction of your mistake )...
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Mar 31st, 2009
0

Re: minmax

There is an initialization failure ...
  for( i = 0; i < n; i++ ) numberlist[i] == rand() % n ;
Reputation Points: 1105
Solved Threads: 389
Posting Virtuoso
mitrmkar is offline Offline
1,714 posts
since Nov 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Need pointer version of this
Next Thread in C Forum Timeline: clearing the text file contents using C





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC