Home > Back-end >  C language array elements cycle shift to the left
C language array elements cycle shift to the left

Time:10-02

A C language array element circulation problems left, I write the code, but the results is wrong, the requirement is input two integers, one is the number of elements, the other one is the number of elements in the left, for example input 5 2
12345, the output is 34512, don't know where the code is out of question, hope someone can see,
#include
Int main () {
Int m, n, I, temp, a, [100].
The scanf (" % d % d ", & amp; N, & amp; m);
for(i=0; iThe scanf (" % d ", & amp; A [I]);
}
//m items before to exchange, such as 123 to 321
for(i=0; iTemp=a, [I].
A [I]=a, [m - I - 1].
A [m - I - 1)=temp.
}
//will be behind all item exchange, such as 45678 for 87654
For (I=m; iTemp=a, [I].
A [I]=a [n - 1 - I + m);
A [n - 1 -i + m]=temp;
}
A [n]='\ 0';
//array elements should be 32187654 at this time, and then reverse the output would be 45678123
For (I=n - 1; I>=0; I -) {
printf("%d ",a[i]);
}
return 0;
}

CodePudding user response:

 
//m items before to exchange, such as 123 to 321
for(i=0; iTemp=a, [I].
A [I]=a, [m - I - 1].
A [m - I - 1)=temp.
}


  • Related