Use C language programming to adjust the array so that all the odd numbers are in the front of the even number,
Function requirements: Adjust the array so that all the odd numbers are in front of the even number. Requirement: enter an integer array and implement a function to adjust the order of numbers in the array so that all the odd numbers in the array are located in the first half of the array, and all the even numbers are located in the second half of the array. Think about it. To implement this function, first traverse the Function Array, starting from the two ends, one is to check whether the array element is an odd number from the front to the back, and one is to check whether the array element is an even number from the back to the back, if the forward is an even, but the last is an odd one, change it! The procedure is as follows:
/*** 2. Adjust the array so that all the odd numbers are in front of the even number. ** Subject: ** enter an integer array to implement a function. ** adjust the order of numbers in the array so that all odd numbers in the array are located in the first half of the array, ** all even numbers are located in the second half of the array. */# Include <stdio. h> # include <stdlib. h> void adjust_arr (int arr [], int size) {int temp; int I, j;/* place the odd digits in front, place the double digits behind * // if (size % 2 = 1) // {// I = 1; j = size-1; //} // else // {// I = 1; j = size-2; //} // for (; I <= j; I + = 2, j-= 2) // {// temp = arr [I]; // arr [I] = arr [j]; // arr [j] = temp; ///}/* place the odd number in front and the even number in the back */for (I = 0, j = size-1; I <j ;) {if (arr [I] % 2 = 0) & (arr [j] % 2 = 1) // if an odd number is in front of an even number, then {temp = arr [I]; arr [I] = arr [j]; arr [j] = temp ;} if (arr [I] % 2 = 1) // if arr [I] is an odd number, I ++; if (arr [j] % 2 = 0) // if arr [j] is an even number, j -- ;}} int main () {int arr [] = {2, 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 15, 16, 8, 9, 17,18, 19,20}; int I; int size = sizeof (arr)/sizeof (arr [0]); adjust_arr (arr, size); // function call, sort the team array, for (I = 0; I <size; I ++) printf ("% d", arr [I]); printf ("\ n"); system ("pause"); return 0 ;}