Within c programming how can I swap elements of an array such that x[1,2,3,4,...,i,i+1,...,n-1,n] becomes x[2,1,3,4,...i,i+1,...,n-1,n] then x[2,3,1,4,...,i,i+1,...,n-1,n] then x[2,3,4,1,...,i,i+1,...,n-1,n] all the way to n and then back down to 1 again?
I so far have the following code where I have printed the un-shuffled array and then created a loop that should swap the elements around, however it only does this for the first two elements.
#include <stdio.h>
float temp;
float x[10];
int i,n;
double delta;
int main(int argc, char *argv[])
{/*main*/
for (i=0;i<10;i++)
{
delta=5;
x[0]=5;
x[i]=x[i-1]+delta;
printf("X[%d] = %f\n", i, x[i]);
}
for(i=0;i<=10;i++)
{
temp=x[i];
x[i]=x[(i+1)];
x[(i+1)]=temp;
printf("\nResultant Array...\n");
for (i=0;i<10;i++)
{
printf("X[%d] = %f\n", i, x[i]);
}
}
}/*main*/
This prints;
X[0] = 5.000000
X[1] = 10.000000
X[2] = 15.000000
X[3] = 20.000000
X[4] = 25.000000
X[5] = 30.000000
X[6] = 35.000000
X[7] = 40.000000
X[8] = 45.000000
X[9] = 50.000000
Resultant Array...
X[0] = 10.000000
X[1] = 5.000000
X[2] = 15.000000
X[3] = 20.000000
X[4] = 25.000000
X[5] = 30.000000
X[6] = 35.000000
X[7] = 40.000000
X[8] = 45.000000
X[9] = 50.000000
If this had worked I was intending to write;
if(i+1==n)
{
temp=x[i+1];
x[i+1]=x[i];
x[i]=temp;
}
All within the for loop, in an attempt to cycle back down through the indices from n to 1.
I am new to programming and I really don't know where to go from here, so any help is much appreciated!