0% found this document useful (0 votes)
4 views1 page

Include

The document contains a C program that rotates an array to the right by a specified number of steps. It defines a function to reverse parts of the array and utilizes this function to achieve the rotation. The main function demonstrates the rotation of an example array by 3 steps and prints the result.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views1 page

Include

The document contains a C program that rotates an array to the right by a specified number of steps. It defines a function to reverse parts of the array and utilizes this function to achieve the rotation. The main function demonstrates the rotation of an example array by 3 steps and prints the result.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#include <stdio.

h>

void reverse(int arr[], int start, int end) {

while (start < end) {

int temp = arr[start];

arr[start] = arr[end];

arr[end] = temp;

start++;

end--;

void rotateRight(int arr[], int n, int k) {

k = k % n;

reverse(arr, 0, n - 1);

reverse(arr, k, n - 1);

int main() {

int arr[] = {1, 2, 3, 4, 5, 6, 7};

int n = sizeof(arr) / sizeof(arr[0]);

int k = 3;

rotateRight(arr, n, k);

printf("Rotated array by %d steps: ", k);

for (int i = 0; i < n; i++) {

printf("%d ", arr[i]);

}}

You might also like