Revitalizing Your Arrays: A Guide to Rotating in C Language

Revitalizing Your Arrays: A Guide to Rotating in C Language

Rotating an array means to shift its elements to the left or right, by a specified number of positions. The operation is commonly used in computer programming and can be helpful for tasks such as sorting, searching, and data manipulation. In this article, we will explore how to rotate an array in C, using a simple algorithm implemented in code.

The code we will be analyzing consists of two functions: main(), which defines the initial position of the array and rotate(), which makes the shift happen. Additionally, there are some declarations and printing statements, which we will discuss in more detail later on.

First, let's look at the syntax of the rotating function. The “rotate" function defined in the code takes an array as an argument and rotates it one position to the left. The method achieves this by shifting each element of the array one space to the left, with exception of the first indices that gets placed to the end of the array.

void rotate(int arr[])
{
  int i = 0;
  int temp = arr[0];
  for (i = 0; i < SIZE - 1; i++)
    {
      arr[i] = arr[i + 1];
    }
  arr[SIZE - 1] = temp;

}

The core of the 'rotate' function lies in the for loop block that traverses each element of the array, whilst performing the shift operation. Inside the for loop, the next element is copied to the current element in the iteration, making the shift look seamless.

Once the loops have executed the desired shift, the last step calls for the original 'temp' variable to be set into the last cell of the new array position at the end, leaving the rest of the array shifted to the left accordingly.

Now let's move on to the rest of the code, which uses a 'for' loop to fill the array with user-specified values passed through the 'scanf'. The scanf function is used to read the values that the user enters and store them in the corresponding array cells, hence allowing the program to know what to rotate.

int main()
{
  int i = 0;
  int arr[SIZE];
  for (i = 0; i < SIZE; i++)
    {
      scanf("%d", &arr[i]);
    }

  for (i = 0; i < SIZE; i++)
    {
      printf("%d", arr[i]);
    }

  printf("\n");

  rotate(arr);

  for (i = 0; i < SIZE; i++)
    {
      printf("%d", arr[i]);
    }

  printf("\n");
}

Following, the code will then print out the initial array using a 'printf' statement followed by a newline character. The next line then calls the 'rotate' function on the array, invoking the shift operation.

Finally, the program ends by printing the modified array once again with a sequence of 'printf' statements.

Here is the whole code

#include <stdio.h>

#define SIZE 10

void rotate(int arr[])
{
  int i = 0;
  int temp = arr[0];
  for (i = 0; i < SIZE - 1; i++)
    {
      arr[i] = arr[i + 1];
    }
  arr[SIZE - 1] = temp;

}

int main()
{
  int i = 0;
  int arr[SIZE];
  for (i = 0; i < 10; i++)
    {
      scanf("%d", &arr[i]);
    }

  for (i = 0; i < SIZE; i++)
    {
      printf("%d", arr[i]);
    }

  printf("\n");

  rotate(arr);

  for (i = 0; i < SIZE; i++)
    {
      printf("%d", arr[i]);
    }

  printf("\n");
}

In conclusion, rotation of an array is illustrated in the code by using a simple loop to shift each element one position to the left, while the first index is placed at the end of the array. By using this algorithm, C programmers can achieve efficient sorting, search algorithms and data manipulation tasks without having to worry about any unnecessary complexity.