0

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!

0

2 Answers 2

2

For looping from n to 0 use

 for(i=n-1;i>=0;i--)

Note that n-1 cause arrays start from 0

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much for your answer, it is very useful :) !
1

The issue you're having has to do with variable scoping! You're using the variable i in two loops where one is nested inside the other (I'm talking about the 2nd and 3rd FOR loops, as listed from top to bottom).

This means that both loops are incrementing the value of i (by executing separate i++ commands), so at the end of the first iteration of your 3rd FOR loop, the variable i has a value of 10, and therefore the program won't run the second iteration of the 2nd FOR loop.

I've changed the inner loop to work with a separate variable, called j. I also had to change some of the conditions of the FOR loops to make them work correctly (you were off by 1):

#include <stdio.h>

float temp;
float x[10];
int i,j,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<9;i++)
        {

            temp=x[i];
            x[i]=x[(i+1)];
            x[(i+1)]=temp; 

            printf("\nResultant Array...#%d\n",i);
            for (j=0;j<10;j++)
                {
                    printf("X[%d] = %f\n", j, x[j]);
                }

        }
}/*main*/

It gave these results:

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...#0
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

Resultant Array...#1
X[0] = 10.000000
X[1] = 15.000000
X[2] = 5.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...#2
X[0] = 10.000000
X[1] = 15.000000
X[2] = 20.000000
X[3] = 5.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...#3
X[0] = 10.000000
X[1] = 15.000000
X[2] = 20.000000
X[3] = 25.000000
X[4] = 5.000000
X[5] = 30.000000
X[6] = 35.000000
X[7] = 40.000000
X[8] = 45.000000
X[9] = 50.000000

Resultant Array...#4
X[0] = 10.000000
X[1] = 15.000000
X[2] = 20.000000
X[3] = 25.000000
X[4] = 30.000000
X[5] = 5.000000
X[6] = 35.000000
X[7] = 40.000000
X[8] = 45.000000
X[9] = 50.000000

Resultant Array...#5
X[0] = 10.000000
X[1] = 15.000000
X[2] = 20.000000
X[3] = 25.000000
X[4] = 30.000000
X[5] = 35.000000
X[6] = 5.000000
X[7] = 40.000000
X[8] = 45.000000
X[9] = 50.000000

Resultant Array...#6
X[0] = 10.000000
X[1] = 15.000000
X[2] = 20.000000
X[3] = 25.000000
X[4] = 30.000000
X[5] = 35.000000
X[6] = 40.000000
X[7] = 5.000000
X[8] = 45.000000
X[9] = 50.000000

Resultant Array...#7
X[0] = 10.000000
X[1] = 15.000000
X[2] = 20.000000
X[3] = 25.000000
X[4] = 30.000000
X[5] = 35.000000
X[6] = 40.000000
X[7] = 45.000000
X[8] = 5.000000
X[9] = 50.000000

Resultant Array...#8
X[0] = 10.000000
X[1] = 15.000000
X[2] = 20.000000
X[3] = 25.000000
X[4] = 30.000000
X[5] = 35.000000
X[6] = 40.000000
X[7] = 45.000000
X[8] = 50.000000
X[9] = 5.000000

I'll leave you to figure out how to go back down :) Good luck!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.