Home > Net >  How can I move all elements in an array with a K number and make the numbers rotate when it comes to
How can I move all elements in an array with a K number and make the numbers rotate when it comes to

Time:04-16

#include <stdio.h>
#include <stdlib.h>

int main()
{
   int N, K, i;

   printf("Enter size of array: ");
   scanf("%d", &N);
   printf("Please enter value of K: ");
   scanf("%d", &K);

   int arr[N];

   for (i = 0; i < N; i  )
   {
       scanf("%d", &arr[i]);
   }

   for (i = 0; i < N; i  )
   {
       int temp = arr[i];
       arr[i   K] = arr[i];
       arr[i] = temp;
   }

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

   return 0;
}

I know the current code is totally wrong It was just another test.

Basically what I need to do is something like this:

the array before: arr[N]={1,2,3,4,5,6,7,8,9,10}

the array after if K is 2: arr[N]={10,9,1,2,3,4,5,6,7,8}

CodePudding user response:

Like @whozcraig pointed out for in-place rotation of array members.

  1. Define a function to reverse(in-place) array members in a range :
static inline void
arr_reverse (const int start, const int end, int arr[]) {
    for (int ai = start, zi = end; ai < zi;   ai, --zi) {
        int tmp = arr[ai];
        arr[ai] = arr[zi];
        arr[zi] = tmp;
    }
}
  1. Then you call it like :
    K %= N;
    if (K != 0) {
        arr_reverse (0, N-1, arr);
        arr_reverse (0, K-1, arr);
        arr_reverse (K, N-1, arr);
    }

CodePudding user response:

I write something like this but it creates new array instead of modifying current one but it works.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int N, K, i;

    printf("Enter size of array: ");
    scanf("%d", &N);
    printf("Please enter value of K: ");
    scanf("%d", &K);

    int arr[N], newArr[N];

    for (i = 0; i < N; i  )
    {
        scanf("%d", &arr[i]);
    }

    for (i = 0; i < K; i  )
    {
        newArr[i] = arr[N-1-i];
    }

    for (int x = 0; i < N; i  )
    {
        newArr[i] = arr[x];
        x  ;
    }

    for (i = 0; i < N; i  )
    {
        printf("%d ", newArr[i]);
    }

    return 0;
 }

CodePudding user response:

Thank you guys, for all the comments and answers. I've tried them and they worked.

I have found a way to do it as well. It's bit different. I did it with a function which moves all the elements with 1 position and then repeat the func as much as needed (K times).

@Cheatah helped me come up with it. I will post it in case somebody likes this solution in the future.

Here it is:

#include <stdio.h>
#include <stdlib.h>

int moveOnePos(int num, int array[num])
{
    int temp = array[num - 1];
    for (int b = num - 1; b > 0; b--)
    {
        array[b] = array[b - 1];
    }
    array[0] = temp;
    return 0;
}

int main()
{
    int N, K, i;

    printf("Please enter size of array: ");
    scanf("%d", &N);
    printf("Please enter K: ");
    scanf("%d", &K);

    int arr[N];

    for (i = 0; i < N; i  )
    {
        scanf("%d", &arr[i]);
    }

    for (i = 0; i < K; i  )
    {
        moveOnePos(N, arr);
    }

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

    return 0;
}
  • Related