# array_of_even_and_odd_integers_v1.c

Nov 17th, 2019
275
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. /*
2.
3.     array_of_even_and_odd_integers_v1.c
4.
8.
9. Write a C program that asks the user to enter 15 integer numbers
10. from the keyboard and stores them in an array.
11. Then separate even and odd numbers of the array.
12. Put all even numbers first, and then odd numbers.
13. Print the new array.
14.
15. Example:
16.
17. Original array: 1 3 4 5 6 7 8 10 5 13 50 43 44 70 33
18.
19. Output: 4 6 8 10 50 44 70 1 3 5 7 5 13 43 33
20.
21.
22. Solution from:
23. https://www.geeksforgeeks.org/segregate-even-and-odd-numbers/
24.
25.
26.     You can find all my C programs at Dragan Milicev's pastebin:
27.
28.     https://pastebin.com/u/dmilicev
29.
30. */
31.
32. #include <stdio.h>
33.
34. // Displays an array of n integers
35. void display_array_of_integers( char text[], int array[], int n ) {
36.
37.     int i;
38.
39.     printf("\n\n%s ( size = %d )\n\n", text, n );
40.
41.     for( i=0; i<n ; i++ )               // display of array items
42.         printf("%4d", array[i] );
43.
44.     printf("\n");
45. }
46.
47. // Function to swap two integers, with pointers
48. // https://www.geeksforgeeks.org/c-program-swap-two-numbers/
49. void swap(int *xp, int *yp)
50. {
51.     int temp = *xp;
52.     *xp = *yp;
53.     *yp = temp;
54. }
55.
56. // return 1 if x is even, otherwise return 0
57. int IsEven( int x )
58. {
59.     if( x%2 == 0 )  // if x even number
60.         return 1;
61.     else
62.         return 0;
63. }
64.
65. // Puts all even numbers first, and then odd numbers.
66. void first_even_then_odd_numbers( int arr[], int n )
67. {
68.     int left=0, right=n-1;                          // Initialize left and right indexes
69.                                                     // to first left and last right element
70.     while ( left<right)                             // until they meet
71.     {
72.         while ( IsEven(arr[left]) && left<right )   // Increment left index while is even arr[left]
73.             left++;
74.
75.         while ( !IsEven(arr[right]) && left<right ) // Decrement right index while is odd arr[right]
76.             right--;
77.
78.         if ( left<right )                           // if they did not meet
79.         {
80.             swap( &arr[left], &arr[right] );        // Swap arr[left] and arr[right]
81.             left++;                                 // we reduce the scope
82.             right--;
83.         }
84.     }
85. }
86.
87. // Puts all odd numbers first, and then even numbers.
88. void first_odd_then_even_numbers( int arr[], int n )
89. {
90.     int left=0, right=n-1;                          // Initialize left and right indexes
91.                                                     // to first left and last right element
92.     while ( left<right)                             // until they meet
93.     {
94.         while ( !IsEven(arr[left]) && left<right )  // Increment left index while is odd arr[left]
95.             left++;
96.
97.         while ( IsEven(arr[right]) && left<right )  // Decrement right index while is even arr[right]
98.             right--;
99.
100.         if ( left<right )                           // if they did not meet
101.         {
102.             swap( &arr[left], &arr[right] );        // Swap arr[left] and arr[right]
103.             left++;                                 // we reduce the scope
104.             right--;
105.         }
106.     }
107. }
108.
109.
110. int main(void)
111. {
112.     int array[]={ 1, 3, 4, 5, 6, 7, 8, 10, 5, 13, 50, 43, 44, 70, 33 };
113.     int n = sizeof(array)/sizeof(array[0]);
114.
115.     display_array_of_integers("\n Given array is: ", array, n );
116.
117.     first_odd_then_even_numbers( array, n );
118.     display_array_of_integers("\n first odd then even array is: ", array, n );
119.
120.     first_even_then_odd_numbers( array, n );
121.     display_array_of_integers("\n first even then odd array is: ", array, n );
122.
123.
124.     return 0;
125. }