📖 Scenario: You are working on a simple program that helps rotate elements in an array. This is useful in many real-life situations like shifting seats in a queue or rotating items in a circular list.
🎯 Goal: Build a program that rotates an array of integers to the right by a given number of steps.
📋 What You'll Learn
Create an array with exact elements
Create a variable for rotation steps
Write a function to rotate the array to the right
Print the rotated array in the specified format
💡 Why This Matters
🌍 Real World
Array rotation is useful in scheduling, gaming, and data buffering where elements need to be shifted cyclically.
💼 Career
Understanding array manipulation is fundamental for software development, especially in embedded systems and performance-critical applications.
Progress0 / 4 steps
1
Create the initial array
Create an integer array called arr with these exact elements: 1, 2, 3, 4, 5.
DSA C
Hint
Use curly braces to list the elements inside the array.
2
Add rotation steps variable
Create an integer variable called k and set it to 2 to represent the number of rotation steps.
DSA C
Hint
Declare k as an integer and assign it the value 2.
3
Write the rotation function
Write a function called rotateRight that takes three parameters: an integer array arr, its size n, and the number of steps k. The function should rotate the array to the right by k steps using a temporary array.
DSA C
Hint
Use a temporary array to store rotated elements. Use modulo operator to wrap around the array.
4
Call the function and print the rotated array
In main, call rotateRight with arr, its size 5, and k. Then print the rotated array elements separated by spaces on one line.
DSA C
Hint
Use a for loop to print each element followed by a space except after the last element.
Practice
(1/5)
1. What does a left rotation by 1 position do to the array [1, 2, 3, 4, 5]?
easy
A. Moves the last element to the front, resulting in [5, 1, 2, 3, 4]
B. Moves the first element to the end, resulting in [2, 3, 4, 5, 1]
C. Reverses the array, resulting in [5, 4, 3, 2, 1]
D. Leaves the array unchanged
Solution
Step 1: Understand left rotation by 1
Left rotation by 1 means the first element moves to the end, and all others shift left by one.
Step 2: Apply rotation to the array
Starting with [1, 2, 3, 4, 5], after rotation it becomes [2, 3, 4, 5, 1].
Final Answer:
Moves the first element to the end, resulting in [2, 3, 4, 5, 1] -> Option B
Quick Check:
Left rotate by 1 shifts left, first element goes last [OK]
Hint: Left rotate by 1 moves first element to end [OK]
Common Mistakes:
Confusing left rotation with right rotation
Thinking rotation reverses the array
Assuming array stays the same
2. Which of the following is the correct C syntax to rotate an array arr of size n to the right by 1 position using a temporary variable temp?
easy
A. temp = arr[n-1]; for (int i = n-1; i > 0; i--) arr[i] = arr[i-1]; arr[0] = temp;
B. temp = arr[0]; for (int i = 0; i < n-1; i++) arr[i] = arr[i+1]; arr[n-1] = temp;
C. temp = arr[n]; for (int i = n; i > 0; i--) arr[i] = arr[i-1]; arr[0] = temp;
D. temp = arr[1]; for (int i = 1; i < n; i++) arr[i] = arr[i-1]; arr[0] = temp;
Solution
Step 1: Understand right rotation by 1
Right rotation by 1 moves the last element to the front, shifting others right.
Step 2: Check syntax correctness
temp = arr[n-1]; for (int i = n-1; i > 0; i--) arr[i] = arr[i-1]; arr[0] = temp; saves last element arr[n-1], shifts elements from right to left, then sets arr[0] = temp. This matches right rotation logic.
Final Answer:
temp = arr[n-1]; for (int i = n-1; i > 0; i--) arr[i] = arr[i-1]; arr[0] = temp; -> Option A
Quick Check:
Right rotate saves last, shifts right, places last at front [OK]
Hint: Right rotate saves last element, shifts right, places last front [OK]
Common Mistakes:
Using wrong index for temp (like arr[n])
Loop direction incorrect (should go backward for right rotate)
Shifting elements in wrong direction
3. What is the output of the following C code snippet?
int arr[] = {10, 20, 30, 40, 50};
int n = 5, d = 2;
for (int i = 0; i < d; i++) {
int temp = arr[0];
for (int j = 0; j < n-1; j++) {
arr[j] = arr[j+1];
}
arr[n-1] = temp;
}
for (int i = 0; i < n; i++) printf("%d ", arr[i]);
medium
A. 30 40 50 10 20
B. 40 50 10 20 30
C. 20 30 40 50 10
D. 50 10 20 30 40
Solution
Step 1: Understand the code logic
The code performs left rotation by 1, two times (d=2). Each rotation moves first element to end.