"Write a program that allows a user to input an integer for the size of an array. Using Malloc is recommended. Randomly generate an integer for each element of the array. Next, creating a funcion to rotate the array. Rotation of the array means that each element is shifted right or left by one index, and the last element of the array is also moved to the first place"
Example: 31 83 91
Which direction to shift and how many times?- Left
How many times would you like to shift? 2
End Result: 91 31 83
Currently my code. It's not shifting.
#include <stdio.h>
#include <stdlib.h>
int main(){
int i, temp, swapped;
int SlotNumber;
int *ptr;
char direction;
int ShiftAmount;
printf("How many slots would you like in your array?\n");
scanf("%d", &SlotNumber);
for(i=0;i<SlotNumber;i++){
printf("%d \t", rand());
}
ptr = (int *)malloc(SlotNumber*sizeof(int));
printf("\nWhich direction would you like to shift it to? R/L?\n");
scanf(" %c", &direction);
printf("\nHow many times would you like to shift?\n");
scanf("%d", &ShiftAmount);
if((direction=='R')||(direction=='r')){
while(1){
swapped = 0;
for(i=0;i<ptr+1;i++){
if(ptr[1]>ptr[i+ShiftAmount]){
int temp = ptr[i];
ptr[i] = ptr[i+ShiftAmount];
ptr[i+ShiftAmount] =temp;
swapped = 1;
}
}
if(swapped == 0);
break;
}
}
printf("\nNewList\n");
for(i=0;i<ptr;i++){
printf("%d \t",ptr[i]);
}
return 0;
}
1element. Yet the example asks the user for "how many times" they want to shift. So essentially this is a shift bynelements. Shifting bynelements is a more generic and more interesting problem than shifting by1. So, what is actually required? Shift by1or shift byn? Or is it required to implement shift bynthrough consecutive shifts by1?