# Circular right shift of array elements in C language and its solution

Time：2021-9-16

C language is a general, process oriented computer programming language. In 1972, in order to transplant and develop UNIX operating system, Dennis Ritchie designed and developed c language in Bell Telephone laboratory.

C language is a widely used computer language. It is as popular as Java programming language. Both of them are widely used among modern software programmers.

Title Content: Problem solving ideas

Method 1:

This is a method I found out by myself. I can achieve the effect of loop through multiple inversions of the array. I have inversed the array three times, so I need to construct a function,

This function can invert an array or part of an array.

1. First invert the entire array [0, n);

2. Invert the first interval [0, m) of the array;

3. Invert the last interval [M, n) of the array;

Assuming n = 2 and M = 2, the specific array is num  = {1,2,3,4,5,6}; The reverse process is shown in the figure below: The code is as follows:

``````#include<stdio.h>
void printArray(int nums[],int len);
void reverseArrayPro(int nums[],int begin,int end);
int main()
{
int len,move;
int i = 0;
scanf("%d%d",&len,&move);
int nums[len];
for(i = 0;i < len;i++){
scanf("%d",&nums[i]);
}
move = move%len;
reverseArrayPro(nums,0,len);
reverseArrayPro(nums,0,move);
reverseArrayPro(nums,move,len);
printArray(nums,len);
return 0;
}
//Array printing
void printArray(int nums[],int len){
int i=0;
for(i = 0; i < len; i++){
if(i==len-1)
printf("%d",nums[i]);
else
printf("%d ",nums[i]);

}
}
//Improved array in place inversion
void reverseArrayPro(int nums[],int begin,int end){
int i=0;
int j=1;
for(i=begin;i<(end-begin)/2+begin;i++){
int temp = nums[i];
nums[i]=nums[end-j];
nums[end-j]=temp;
j++;
}
}`````` Method 2:

This is a method I read online. This method is much simpler than my method. In fact, it does not move the elements in the array, but simply changes the traversal of the elements in the array

Sequence, first traverse the elements N-M ~ n-1 in the array, and then traverse the elements 0 ~ n-m-1.

The code is as follows:

``````
#include<stdio.h>
int main()
{
int len,move;
int i = 0;
scanf("%d%d",&len,&move);
int nums[len];
for(i = 0;i < len;i++){
scanf("%d",&nums[i]);
}
move = move%len;
for(i = len-move;i<len;i++){
printf("%d ",nums[i]);
}
for(i = 0;i<len-move;i++){
if(i==len-move-1)
printf("%d",nums[i]);
else
printf("%d ",nums[i]);

}
return 0;
}`````` summary

This is the end of this article about the circular right shift of array elements in C language and its solutions. For more information about the circular right shift of array elements in C language, please search the previous articles of developeppaer or continue to browse the relevant articles below. I hope you will support developeppaer in the future!

## Example of method for converting Lua table to c# dictionary

Table property Table is an “associative array”. The index of the array can be a number or a string. All index values need to be enclosed by “[” and “]”; If it is a string, you can also remove quotation marks and brackets; That is, if it is not enclosed by [], it is considered […]