Advertisement
dmilicev

circular_movement_of_array_elements.c

Dec 6th, 2019
250
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.20 KB | None
  1. /*
  2.  
  3.     circular_movement_of_array_elements.c
  4.  
  5.  
  6.     You can find all my C programs at Dragan Milicev's pastebin:
  7.  
  8.     https://pastebin.com/u/dmilicev
  9.  
  10. */
  11.  
  12. #include <stdio.h>
  13.  
  14.  
  15. // Displays an array of n integers
  16. void display_array_of_integers( char text[], int arr[], int n )
  17. {
  18.     int i;
  19.  
  20.     printf("%s ( n = %d )\n\n", text, n );
  21.  
  22.     for( i=0; i<n ; i++ )       // display of n array items
  23.         printf("%4d", arr[i] );
  24.  
  25.     printf("\n");
  26.  
  27. } // display_array_of_integers()
  28.  
  29.  
  30. // make array of integers from 0 to n-1
  31. void form_array( int arr[], int n )
  32. {
  33.     int i;
  34.  
  35.     for( i=0; i<n ; i++ )
  36.         arr[i] = i;
  37.  
  38. } // form_array()
  39.  
  40.  
  41. // From the end of the array to the left,
  42. // it moves the array elements one place to the right
  43. void circular_move_array_elements_one_place_to_the_right( int arr[], int n )
  44. {
  45.     int mem;                    // place in memory to remember one integer
  46.     int i = n-1;                // i is an index of last one array member
  47.  
  48.     mem = arr[n-1];             // remember last element of array
  49.  
  50.     while( i > 0 )   // go through the array backwards, from the last member to the first
  51.     {
  52.         arr[i] = arr[i-1];      // push one element to the right
  53.         i--;                    // going backwards, we reduce the index
  54.     }
  55.                                 // the first place is now empty
  56.     arr[i] = mem;               // now in first place is last element
  57.  
  58. } // circular_move_array_elements_one_place_to_the_right()
  59.  
  60.  
  61. // From the end of the array to the left,
  62. // it moves the array elements one place to the right
  63. void circular_move_array_elements_k_places_to_the_right( int arr[], int n, int k )
  64. {
  65.     int i;
  66.  
  67.     for( i=0; i<k ; i++ )
  68.         circular_move_array_elements_one_place_to_the_right( arr, n );
  69.  
  70. } // circular_move_array_elements_one_place_to_the_right()
  71.  
  72.  
  73.  
  74. int main(void)
  75. {
  76.     int arr[100];               // array of 100 integers
  77.     int i, n, k;
  78.  
  79.     printf("\n Enter number of array elements, n = ");
  80.     scanf("%d",&n);
  81.  
  82.     printf("\n How many places to the right circularly move the elements, k = ");
  83.     scanf("%d",&k);
  84.  
  85.     form_array( arr, n );
  86.  
  87.     display_array_of_integers( "\n Before:  ", arr, n );
  88.  
  89.     circular_move_array_elements_k_places_to_the_right( arr, n, k );
  90.  
  91.     display_array_of_integers( "\n After:  ", arr, n );
  92.  
  93.  
  94.     return 0;
  95.  
  96. } // main()
Advertisement
RAW Paste Data Copied
Advertisement