1

I need to be able to choose the direction of the shift in an array of integers. However, neither with the if statement nor the switch case could I achieve this - it would always skip the conditions and go straight to "You didn't enter the correct value (L/R)" for example; or it would just execute the code for the left shift (since I wrote it first) regardless of the input.

Before the case statement, I used the if statement but got the same output.

I think the problem should be somewhere in this code but no matter what I try I get a similar, incorrect result:

printf("\nTo left or right (enter L or R)? ");
    scanf("%c",&direction);

    switch (direction){
        case 'l':
        case 'L':
    {
        for (j=1;j<=m;j++){
        temp=a[0];
            for (i=0;i<n-1;i++){
                a[i]=a[i+1];
            }
        a[n-1]=temp;}
        break;
    }

...

    default: 
        printf("\nDirection input wrong! (enter only L/R or l/r)");
        break;

    }

By entering the appropriate character the array should move to the left or right by n.

So, if a[5]={1,2,3,4,5} After choosing it 'moves' to the left by n=2, a should be a[5]={3,4,5,1,2}; but in my case it stays the same...

0 Answers0