| | |
minmax
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Dec 2008
Posts: 96
Reputation:
Solved Threads: 3
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?
C Syntax (Toggle Plain Text)
#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; } }
Last edited by Dewey1040; Mar 30th, 2009 at 11:03 am.
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:
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.
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:
c Syntax (Toggle Plain Text)
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.
Last edited by csurfer; Mar 30th, 2009 at 1:50 pm.
I Surf in "C"....
Purely for the sake of simplicity, I would go with this route (doesn't use a second set of variables):
C Syntax (Toggle Plain Text)
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:
c Syntax (Toggle Plain Text)
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; }
•
•
Join Date: Dec 2008
Posts: 96
Reputation:
Solved Threads: 3
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.
C Syntax (Toggle Plain Text)
/*********************************************************************/ /* 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]; } }
![]() |
Similar Threads
- How do i load a image on the same page ? (Site Layout and Usability)
- pebble chess in c++ (C++)
- CSV file (C)
- Build Failed Issues.. (Java)
- Overfloat? IE6/win? (HTML and CSS)
- i need a project (C++)
- C++ help (C++)
- TO CHAINSAW - or anybody else who knows C++ (C++)
Other Threads in the C Forum
- Previous Thread: Need pointer version of this
- Next Thread: clearing the text file contents using C
| Thread Tools | Search this Thread |
#include adobe api array arrays asterisks binarysearch calculate char cm copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic feet fflush fgets file fork forloop framework frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o include incrementoperators input interest kernel kilometer linked linkedlist linux linuxsegmentationfault list lists locate logical_drives loopinsideloop. match matrix meter microsoft motherboard mqqueue multi mysql number odf opensource owf pattern pdf performance pointer posix probleminc process program programming pyramidusingturboccodes radix read recursion recv repetition research scanf scheduling scripting segmentationfault sequential shape socket socketprograming stack standard string strings systemcall testing turboc unix user voidmain() wab windows.h






)... 