Click here to Skip to main content
15,905,912 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Need to fix this piece of code for use with n Number of some finite array.
swapping with next one like this code outputs "456123" next three are swap with other next three elements. 


What I have tried:

Java
<pre> int[] array = {1, 2, 3, 4, 5, 6};
    int c;
    for(int i=0;i<=2;i++)
    {
        c = array[i];
        array[i] = array[i+3];
        array[i+3] = c;         
    }       
    for(int i=0;i<=5;i++)
        System.out.println(array[i]);
Posted
Updated 23-Jan-19 1:31am
Comments
Patrice T 23-Jan-19 11:59am    
Show an example with another n.

This sound like homework, so we will not be doing it for you: we expect you to try and do this for yourself, as it's the only way you will learn how to do it properly.

Give it a try: see how far you can get. We aren't here to do it for you!
 
Share this answer
 
Look at what your code already does: it iterates until the middle of the array, swapping the item at current index with the item at (current index + middle of the array).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900