Rotate An Array Right | C Programming Example

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

thanks alot, been trying to figure this out for my c++ extra credit assignment! makes sense now

michaelshoga
Автор

I'm a little bit confuse sir..
isnt it that on first for loop of rotate_once_right() function when i = 4.. that given array will become {1, 2, 3, 4, 6, 6}?? since u just replace the 5 with 6 and not vice versa?

potatobits
Автор

thank u so much! this helps me understand sm but im just confused at how someone can figure out this problem solving method.. like there is a set trick to problems like these? I tried to do this on my own before searching for help and couldn't figure it out, so now I feel incompetent lol.

six
Автор

Hi sir, can you tell me which theme are you using for your editor? it looks pretty neat with menlo font🙂

hammadahmad
Автор

Did it a bit different to yours
#include<stdio.h>

void rotate(int arr[], int length);

void rotaten(int arr[], int length, int n);

void main()
{
int ar1[]={1, 2, 3, 4, 5, 6};
rotaten(ar1, 6, 2);
for(int i=0;i<6;i++)
{
printf("%d", ar1[i]);
}
printf("\n");

}

void rotaten(int arr[], int length, int n)
{
for(int i=0;i<n;i++)
{
rotate(arr, length);
}
}

void rotate(int arr[], int length)
{
int temp = arr[length-1];
for(int i=length-1;i>=1;i--)
{
arr[i]=arr[i-1];
}
arr[0]=temp;
}

SketchupGuru
Автор

Nice 🙃 :
// 1 bullet 2 birds ---- > JK
void retate_right(int *array, int length, int go_right){
for(int i = 0; i < go_right; i++){
int temp = array[length-1];
for(int j = length-1; j > 0; j--){
array[j] = array[j-1];
}
array[0] = temp;
}
}

justcurious