Rotating the elements of an array data structure by k positions to the right.

#include <stdio.h>
// K is no.of positions an array to be rotated
int main()
{
    int k=2 , n=4,a[4]={1,2,3,4}, b[4],i;
for(i=0;i<n;i++)
{
    b[(i+k)%n]=a[i];
}
for(i=0;i<n;i++)
{
    printf("%d\t",b[i]);
}
    return 0;
}

Comments

Popular posts from this blog

Finding the second largest in an array