Well as a thought experiment, you need to make sure your code handles a base case properly.
Code:int data[1] = {0}, num_elements = 1; moveelements(data, 0, num_elements - 1);
Well as a thought experiment, you need to make sure your code handles a base case properly.
Code:int data[1] = {0}, num_elements = 1; moveelements(data, 0, num_elements - 1);
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
the reason i had if i <= end was i < end leaves the 0 at index 1 not start mind you as this is going to be for sorting arrays it seems silly to pass in a single number
as long as i is NEVER greater than end then your fine
"...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson
Here's a more readable way to do your function (with a couple of fail safes):
Code:void moveelements(int data[], int pos, int end) { int i, j; if ( pos >= end ) return; if ( pos < 0 ) pos = 0; for ( i = pos, j = i + 1; i < end; ++i, ++j ) { if ( data[j] == 0 ) break; } if ( i == end ) return; data[j] = data[i]; data[i] = 0; moveelements(data, pos, i - 1); }
Last edited by awsdert; 05-29-2019 at 05:17 PM. Reason: Forgot a failsafe
i have implemented your suggestions and changed the code slightly so i can move the 0 to any position in the array
however i am wondering if this is truly recursive as all the work is done before the recursive call not after im just trying to save some leg workCode:#include <stdio.h> #include <stdlib.h> void moveelements(int data[], int pos, int start, int end); void printelements(const int x, const int data[]); int main() { int data[6] = {2, 3, 4, 5, 6, 0}, num_elements = 6; //int test[1] = {0}; printelements(num_elements, data); moveelements(data, 3, 3, num_elements - 1); printelements(num_elements, data); return 0; } void moveelements(int data[], int pos, int start, int end) { int i, j; if (pos < start || pos >= end) { return; } if (pos < 0) { pos = 0; } if (pos <= end) { for (i = pos, j = i + 1; i < end; i++, j++) { if (data[j] == 0) { break; } } if (i == end) { return; } data[i + 1]= data[i]; data[i] = 0; moveelements(data, i - 1, start, end); } } void printelements(const int x, const int data[]) { int i; for (i = 0; i < x; i++) { printf("%d ", data[i]); } printf("\n"); }
coop
here is the final goal
Code://two arrays data[x] sorteddata[x] //have an index for each array //insert data[0] into sorteddata[0] //increment dataindex //look at data[1] and compare with sorted data[0] //if data[1] less than sorteddata[0] move sorteddata[0] to sorteddata[1] insert data[1] at sorteddata[0] //increment sortedindex by 2 and dataindex by 1 //else data[1] is greater or equal to sorteddata[0] so increment sortedindex by 1 and insert data[1] into sorteddata[1] //loop
Last edited by cooper1200; 05-30-2019 at 03:17 AM.
Then why the recursive function? Simpler to do something like this:
Code:// Dont' need a start positon, pos IS the start position int find_position_of_value( int data[], int pos, int end ) { int i, j; // Avoids unneeded operations if ( pos >= end ) return; // Avoids starting a segfaulting position if ( pos < 0 ) pos = 0; for ( i = pos, j = pos + 1; i < end; ++i, ++j ) { if ( data[j] == val ) return j; } return -1; } int move_backward( int data[], int dst, int pos ) { int i, j, val; // Avoids unneeded operations if ( pos == dst ) return 0; if ( dst < 0 ) return -1; if ( pos < dst ) return -1; val = data[pos]; for ( i = pos, j = i - 1; i > dst; --i, --j ) { data[i] = data[j]; } data[dst] = val; return 0; } int move_foreward( int data[], int pos, int dst ) { int i, j, val; // Avoids unneeded operations if ( pos == dst ) return 0; if ( pos < 0 ) return -1; if ( dst < pos ) return -1; val = data[pos]; for ( i = pos, j = i + 1; i < dst; ++i, ++j ) { data[i] = data[j]; } data[dst] = val; return 0; } int main( int argc, char *argv[] ) { int data[] = { 0, 2, 4, 6, 78 }, count = 5, pos = find_position_of_value( data, 78 ), two = find_position_of_value( data, 2 ); if ( move_backward( data, two, pos ) != 0 ) { return 1; } return 0; }
Made some slip ups in the code there, thought I edited them out but they still there, top function was supposed to have another value ("int val") and I forgot to add the first position and last position when calling it at the bottom
sorry i have been busy today and will be tomorrow please dont think i have just ignored your post
nw, I work a 7 hour shift, 6 days a week so fully understandable, tbh I actually forgot until just now