Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- segregate_even_and_odd_elements_on_an_array_v2.c
- https://www.w3resource.com/c-programming-exercises/array/c-array-exercise-96.php
- You can find all my C programs at Dragan Milicev's pastebin:
- https://pastebin.com/u/dmilicev
- */
- #include<stdio.h>
- // Displays the array arr[n] of n integers with text before and after the array
- void print_array(char *text_before, int arr[], int n, char *text_after){
- printf("%s", text_before);
- for(int i=0;i<n;i++)
- printf("%3d",arr[i]);
- printf("%s", text_after);
- }
- // Displays one element of the array arr[n] of n integers on it's place
- void print_one_element_of_array_on_its_place(char *text_before, int arr[], int n,
- int index, char *text_after){
- print_array("\n",arr,n,"\n");
- printf("%s", text_before);
- for(int i=0;i<n;i++)
- if(i==index)
- printf("%3d",arr[i]);
- else
- printf("%3s"," ");
- printf("%s", text_after);
- }
- // Displays two elements of the array arr[n] of n integers on theirs place
- void print_two_elements_of_array_on_theirs_place(char *text_before, int arr[], int n,
- int index1, int index2, char *text_after){
- print_array("\n",arr,n,"\n");
- printf("%s", text_before);
- for(int i=0;i<n;i++)
- if(i==index1 || i==index2)
- printf("%3d",arr[i]);
- else
- printf("%3s"," ");
- printf("%s", text_after);
- }
- void changePlace(int *ar, int *br){
- int temp = *ar;
- *ar = *br;
- *br = temp;
- }
- void EvenOddSegre(int arr[], int n){
- int l_index = 0, r_index = n-1;
- while (l_index < r_index){
- while (arr[l_index]%2 == 0 && l_index < r_index){
- printf("\n left index: %2d , right index: %2d \n",l_index,r_index);
- print_one_element_of_array_on_its_place("",arr,n,l_index,"\n");
- printf("\n arr[%d] = %d is even, so we go to the right to the next element of the array. \n",l_index,arr[l_index]);
- l_index++;
- }
- while (arr[r_index]%2 == 1 && l_index < r_index){
- printf("\n left index: %2d , right index: %2d \n",l_index,r_index);
- print_one_element_of_array_on_its_place("",arr,n,r_index,"\n");
- printf("\n arr[%d] = %d is odd, so we go to the left to the previous element of the array. \n",r_index,arr[r_index]);
- r_index--;
- }
- if (l_index < r_index){
- printf("\n left index: %2d , right index: %2d \n",l_index,r_index);
- print_two_elements_of_array_on_theirs_place("",arr,n,l_index,r_index,"\n");
- printf("\n We exchange elements arr[%d] = %d and arr[%d] = %d with each other. \n",
- l_index, arr[l_index], r_index, arr[r_index] );
- changePlace(&arr[l_index], &arr[r_index]);
- print_array("\n",arr,n,"\n");
- l_index++;
- r_index--;
- }
- }
- }
- int main()
- {
- int arr[] = {17, 42, 12, 19, 7, 27, 24, 30, 54, 73};
- int n = sizeof(arr)/sizeof(arr[0]);
- print_array("\n The given array is: \n\n",arr,n,"\n");
- // print_one_element_of_array_on_its_place("",arr,n,1,"\n");
- // print_two_elements_of_array_on_theirs_place("",arr,n,2,5,"\n");
- EvenOddSegre(arr, n);
- print_array("\n The array after segregation is: \n\n",arr,n,"\n\n");
- return 0;
- }
Advertisement
Comments
-
- https://winboxgg.com/ [URL=https://winboxgg.com/] https://winboxgg.com/[/URL]
- https://winboxgg.com/.../win.../]https://winboxgg.com/[/URL]
Add Comment
Please, Sign In to add comment
Advertisement