Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- circular_movement_of_array_elements.c
- You can find all my C programs at Dragan Milicev's pastebin:
- https://pastebin.com/u/dmilicev
- */
- #include <stdio.h>
- // Displays an array of n integers
- void display_array_of_integers( char text[], int arr[], int n )
- {
- int i;
- printf("%s ( n = %d )\n\n", text, n );
- for( i=0; i<n ; i++ ) // display of n array items
- printf("%4d", arr[i] );
- printf("\n");
- } // display_array_of_integers()
- // make array of integers from 0 to n-1
- void form_array( int arr[], int n )
- {
- int i;
- for( i=0; i<n ; i++ )
- arr[i] = i;
- } // form_array()
- // From the end of the array to the left,
- // it moves the array elements one place to the right
- void circular_move_array_elements_one_place_to_the_right( int arr[], int n )
- {
- int mem; // place in memory to remember one integer
- int i = n-1; // i is an index of last one array member
- mem = arr[n-1]; // remember last element of array
- while( i > 0 ) // go through the array backwards, from the last member to the first
- {
- arr[i] = arr[i-1]; // push one element to the right
- i--; // going backwards, we reduce the index
- }
- // the first place is now empty
- arr[i] = mem; // now in first place is last element
- } // circular_move_array_elements_one_place_to_the_right()
- // From the end of the array to the left,
- // it moves the array elements one place to the right
- void circular_move_array_elements_k_places_to_the_right( int arr[], int n, int k )
- {
- int i;
- for( i=0; i<k ; i++ )
- circular_move_array_elements_one_place_to_the_right( arr, n );
- } // circular_move_array_elements_one_place_to_the_right()
- int main(void)
- {
- int arr[100]; // array of 100 integers
- int i, n, k;
- printf("\n Enter number of array elements, n = ");
- scanf("%d",&n);
- printf("\n How many places to the right circularly move the elements, k = ");
- scanf("%d",&k);
- form_array( arr, n );
- display_array_of_integers( "\n Before: ", arr, n );
- circular_move_array_elements_k_places_to_the_right( arr, n, k );
- display_array_of_integers( "\n After: ", arr, n );
- return 0;
- } // main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement