Left Rotation of Array in C Language – Left Rotate Array N Times

program-to-understand-Left-Rotation-of-Array-in-C-language

Program Description:

Write a Program to understand the Left Rotation of Array in C Programming Language. The program should accept an array from the user and the number of positions array need to be rotated in the left side direction. Then the program should Left Rotate the array in the specified number of positions and provide the results.

📢This Program is part of the Array Practice Programs Series

Here is the example input and output of the program.

Excepted Input and Output:

Input:

Enter desired size for array(1-100): 6

Please enter array elements(6) : 12 23 34 45 56 67

Enter the number of position to rotate the array: 2

Output:

Original Array: 12 23 34 45 56 67

Array After Left Rotating 2 Positions : 56 67 12 23 34 45

If we observe the above output, The Original Array is 12 23 34 45 56 67 and the user wants to Left rotate array by 2 positions in the left direction, So the resultant array is 56 67 12 23 34 45

Here is the graphical representation of the above-left rotation operation of Array.

Logic-to-left-rotation-of-array-in-c-language

Prerequisites:

Let’s look at the algorithm to left rotate the array elements.

Algorithm for Left Rotation of Array in C Programming:

Let’s say the input array is numbers array and the total number of the array elements are equal to size. and The variable poscontains the number of elements that need to be rotated in the left direction.

  • Array is numbers
  • The size of the array is size
  • The number of positions to be rotated is pos

To Rotate the array by pos elements,

  • Create a Temporary Array( tempArr) with the same size of the input array numbers
  • Now, We need to iterate over the input array ( numbers) and insert the elements in the temporary array at the correct rotated position.
  • The correct position after rotating the array by pos position can be calculated using the (i+pos) % size.
    • Where, i is the present element index ( for loop index)
    • pos is the number of positions to be shifted in the left direction
    • size is the total size of the input array.
  • Once the above loop is completed, The temporary array ( tempArr) contains the Array which is rotated by pos positions.
  • Replace the Input array ( numbers) elements with the Temporary array ( tempArr) elements.
  • Now, The numbers array contains the Left Rotated array.

Left Rotation of Array in C Program Explanation:

Here is the step-by-step explanation of the Left Rotation of the Array program in the C Programming Language.

  1. Start the program by declaring two arrays. Input Array which is numbers and Temporary Array which is tempArr. The max size of both arrays is equal to 100. ( Change the 100 max size, if you want to use the large arrays).
  2. Take the desired array size from the user and store it in the size variable.
  3. Check if the size is going beyond the array bounds using – (size >= 100 || size <= 0) condition. Display an error message if the array is going out of bounds.
  4. We define two helper functions named read() function and display() function to read and print array elements
    • The read() function is used to read the input elements from the user and update the numbers array
    • The display() function is used to print the array elements on the Standard Output
  5. Now Call the read() function with numbers array and size as the parameter to Take user input and update the numbers array. – read(numbers, size);
  6. Make another function call to display() function to print the array elements on the console. – display(numbers, size);
  7. To Left Rotate the array of elements by positionnumber of elements,
    • Iterate over the original array from i=0 to i<size using a For Loop
    • Copy the numbers[i] to tempArr array ((i+position)%size) index
    • tempArr[((i+position) %size)] = numbers[i];
    • Increment the i by 1.
  8. Once the above loop is completed, The tempArr contains the Left Rotated Array.
  9. Replace the original array elements with the tempArr elements.
    • Use another For loop start from i=0 to i<size
    • Copy tempArr[i] to the numbers[i] – numbers[i] = tempArr[i];
  10. Display the Left rotated array on the console by calling the display() function from the main() function. – display(numbers, size);
  11. Stop the program.

Program to understand Left Rotation of Array in C Language:

Here is the example Program for Left Rotation of Array in C Programming Langauge.

Note that, we have defined two functions read() and display() in the above program.

Program Output:

Let’s Compile and Run the program and Observe the program output.

To compile the C Program use the following command.

$ gcc left-rotate-arr.c

The above command generated an executable file named a.out. Run the executable file.

Test Case 1: Positive test cases:

Let’s try free more examples and see the output

Left-Rotation-of-Array-in-C-Program-output

Test Case 2: Rotating more than the size of the array

If you observe the above array size is 4 but we Left Rotated by 5 elements, which is equal to 5-4 = 1. As After rotating 4 elements, We will end up with the original array, So technically we rotated only once.

Test Case 3: Negative tests – Array out of bounds.

As we can see from the above outputs, The program is generating the desired results. And displaying the error message if the array is going out of bounds.

Related Array Programs:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

Leave a Reply