So i want to switch the elements of an array with 32 integers "n" times.
All elements should be on the next position and the last should be at the first.
I tried something like this:
while(scanf("%d", &n) != EOF)
{
for(int j=0; j<n; j++)
for(int i=1; i<31; i++)
{
t[0]=t[31];
tmp=t[i];
t[i]=t[i+1];
}
}
Im not sure how could I use the tmp variable to solve this problem.
this is the array's element:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
and it should look like this if the n=1:
32 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
Your swap is wrong, it should look like this:
char temp = t[i];
t[i] = t[i + 1];
t[i + 1] = temp;
Also, if you want an infinite loop like this, I recommend skipping whitespace in the scanf
, like this:
while (scanf(" %d", &n) == 1) // note the space before %d
All in all, this is how it could look like:
int main(int argc, char** argv) {
char t[33] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456"; // 33 elements to leave space for '\0'
int n;
while (scanf(" %d", &n) == 1)
{
for (int j = 0; j < n; j++)
for (int i = 0; i < 31; i++)
{
char temp = t[i];
t[i] = t[i + 1];
t[i + 1] = temp;
}
printf("string: %s\n", t);
}
return 0;
}