Advertisement
dmilicev

segregate_even_and_odd_elements_on_an_array_v2.c

Nov 7th, 2023
986
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.40 KB | None | 0 0
  1. /*
  2.  
  3.     segregate_even_and_odd_elements_on_an_array_v2.c
  4.  
  5. https://www.w3resource.com/c-programming-exercises/array/c-array-exercise-96.php
  6.  
  7.  
  8.     You can find all my C programs at Dragan Milicev's pastebin:
  9.  
  10.     https://pastebin.com/u/dmilicev
  11.  
  12. */
  13.  
  14. #include<stdio.h>
  15.  
  16. // Displays the array arr[n] of n integers with text before and after the array
  17. void print_array(char *text_before, int arr[], int n, char *text_after){
  18.     printf("%s", text_before);
  19.     for(int i=0;i<n;i++)
  20.         printf("%3d",arr[i]);
  21.     printf("%s", text_after);
  22. }
  23.  
  24. // Displays one element of the array arr[n] of n integers on it's place
  25. void print_one_element_of_array_on_its_place(char *text_before, int arr[], int n,
  26.                                             int index, char *text_after){
  27.     print_array("\n",arr,n,"\n");
  28.     printf("%s", text_before);
  29.     for(int i=0;i<n;i++)
  30.         if(i==index)
  31.             printf("%3d",arr[i]);
  32.         else
  33.             printf("%3s"," ");
  34.     printf("%s", text_after);
  35. }
  36.  
  37. // Displays two elements of the array arr[n] of n integers on theirs place
  38. void print_two_elements_of_array_on_theirs_place(char *text_before, int arr[], int n,
  39.                                                 int index1, int index2, char *text_after){
  40.     print_array("\n",arr,n,"\n");
  41.     printf("%s", text_before);
  42.     for(int i=0;i<n;i++)
  43.         if(i==index1 || i==index2)
  44.             printf("%3d",arr[i]);
  45.         else
  46.             printf("%3s"," ");
  47.     printf("%s", text_after);
  48. }
  49.  
  50. void changePlace(int *ar, int *br){
  51.     int temp = *ar;
  52.     *ar = *br;
  53.     *br = temp;
  54. }
  55.  
  56. void EvenOddSegre(int arr[], int n){
  57.     int l_index = 0, r_index = n-1;
  58.  
  59.     while (l_index < r_index){
  60.         while (arr[l_index]%2 == 0 && l_index < r_index){
  61.             printf("\n left index: %2d , right index: %2d \n",l_index,r_index);
  62.             print_one_element_of_array_on_its_place("",arr,n,l_index,"\n");
  63.             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]);
  64.             l_index++;
  65.         }
  66.  
  67.         while (arr[r_index]%2 == 1 && l_index < r_index){
  68.             printf("\n left index: %2d , right index: %2d \n",l_index,r_index);
  69.             print_one_element_of_array_on_its_place("",arr,n,r_index,"\n");
  70.             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]);
  71.             r_index--;
  72.         }
  73.  
  74.         if (l_index < r_index){
  75.             printf("\n left index: %2d , right index: %2d \n",l_index,r_index);
  76.             print_two_elements_of_array_on_theirs_place("",arr,n,l_index,r_index,"\n");
  77.             printf("\n We exchange elements  arr[%d] = %d  and  arr[%d] = %d  with each other. \n",
  78.                     l_index, arr[l_index],  r_index, arr[r_index] );
  79.             changePlace(&arr[l_index], &arr[r_index]);
  80.             print_array("\n",arr,n,"\n");
  81.             l_index++;
  82.             r_index--;
  83.         }
  84.     }
  85. }
  86.  
  87.  
  88. int main()
  89. {
  90.     int arr[] = {17, 42, 12, 19, 7, 27, 24, 30, 54, 73};
  91.     int n = sizeof(arr)/sizeof(arr[0]);
  92.  
  93.     print_array("\n The given array is: \n\n",arr,n,"\n");
  94.  
  95. //  print_one_element_of_array_on_its_place("",arr,n,1,"\n");
  96.  
  97. //  print_two_elements_of_array_on_theirs_place("",arr,n,2,5,"\n");
  98.  
  99.     EvenOddSegre(arr, n);
  100.  
  101.     print_array("\n The array after segregation is: \n\n",arr,n,"\n\n");
  102.  
  103.     return 0;
  104. }
  105.  
Advertisement
Comments
  • winboxmalaysia
    182 days
    # text 0.14 KB | 0 0
    1. https://winboxgg.com/ [URL=https://winboxgg.com/] https://winboxgg.com/[/URL]
    2. https://winboxgg.com/.../win.../]https://winboxgg.com/[/URL]
Add Comment
Please, Sign In to add comment
Advertisement