I would like to know how to write code for a right circular shift aka lazy susan. Its an array with 10 elements let me show you and example

first time throw array it prints
1,2,3,4,5,6,7,8,9,10
second time throw after right circular shift of 3 places for example it prints
8,9,10,1,2,3,4,5,6,7
and so on. if there is anyone that can help that be just great.

Recommended Answers

All 40 Replies

You want to print out and leave the original array unchanged? Use the modulus operator. for (i=0; i<10; i++) { print(array[i+offset] mod 10;}

Sorry, that's a left shift. Modify appropriately for right shift - should be easy enough to do.

Can you explain the problem a bit more.
Is it: You have an array of x items and want to move the items left or right n slots, wrapping around from the end back to the start of the array.

Have you tried working out the logic for how to do this yet? Take a piece of paper and a pencil, draw the array and figure out how to move items and not lose any of them.

The main program will display the array contents to the user and ask how many positions he would like to shift off the right side of the array, and replace onto the left side. This is right circular shift. If the user inputs a negative value, implement a left circular shift instead. You are not merely printing the values in the array differently, you are physically changing the order in which the values are stored in the array.

Stay in a loop, repeating the same process until the user enters something non-numeric as a value.
Array contents: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Shift how many positions? 5

After the array ran throw
Array contents: 11 12 13 14 15 1 2 3 4 5 6 7 8 9 10


This algorithm can be used as the basis for implementing a "lazy susan" type of application, such as cycling forwards or backwards through songs in a song list.

Is that the full text of your assignment?
How is your design for the program coming?

Is that the full text of your assignment?
How is your design for the program coming?

That is the full text just missing a couple of more examples. Also the program is not coming good can you help me with it some how?

Array contents: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Shift how many positions? 5

Array contents: 11 12 13 14 15 1 2 3 4 5 6 7 8 9 10
Shift how many positions? 2

Array contents: 9 10 11 12 13 14 15 1 2 3 4 5 6 7 8
Shift how many positions? 0

Array contents: 9 10 11 12 13 14 15 1 2 3 4 5 6 7 8
Shift how many positions? -8

Array contents: 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1
Shift how many positions? 15

Array contents: 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1
Shift how many positions? 17

Array contents: 15 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Shift how many positions? q

Yes, we can help you but we have nothing to work on yet.
You'll have to post your design ideas and the code you have.

Got the basic not sure how to move the array elements to the front then shift the others over. I was thinking create a temp variable and then copy one value from the array to the variable.and then copy it back to the array unless there is an easy way. I am very new to java so you guys got to keep it simple .

[import java.util.Scanner;
public class Shifter {

    public int [] data=new int[15];

     public Shifter()
     {
         int size=0;


     }

 public int shifter (int size)
  {

    size = size <0 ? 15:size;
    data=new int[size];
   int n=3;

    for(int x=0;x<data.length;x++)
    {
      data[x]=x+1;
    }

    return size;
  }

 public int shift(int pos)
 {
     Scanner sc = new Scanner(System.in);
     System.out.println("Shift how many positions ");
     pos=sc.nextInt( );





     return pos;
 }
 public void display()
 {
     

     for(int y=0;y<data.length-1;y++)
     {
      //   data[y]=y;
       System.out.printf(" %d ", data[y]);

     }

     System.out.println();


 }

Ok that's some code. It could use some comments describing what each line of code is supposed to do.

What are your design ideas? How do you plan to do the shifting?

Okay, there's the code*. Now, walk us through it. Tell us what how it works. That might be enough for you to see the problems, and then you'll have solved it yourself.

So: Where do we begin? What happens first?


* Nice cross-post here!

Ok that's some code. It could use some comments describing what each line of code is supposed to do.

What are your design ideas? How do you plan to do the shifting?

I don't have any idea how to do shifting was thinking copy each element of array to a temp variable then copy it back to array but i think that might not work not sure sorry

can you guys give me some simple ideas to work with on shifting throw an array ?

Take a piece of paper and work out the logic.
Start with a simple case like 4 letters: ABCD to shift 1 to DABC
Start at the first element. It's to move to the second spot, but you need to save the B first, then store the A. Move to next and use same logic: save C, store B. Continue. when you get to end, restart at the beginning.

You're on the right track there. You're missing one step. Let's say you have your array - {0,1,2,3,4,5,6,7,8,9} and you want to shift it to the right by 3 places. You can do it, as you say, with one temp variable. What are you going to do?

I am i missing the step copy from temp back to the array ?

Array contents: 1 2 3 4 5 6 7 8 9 10
Shift how many positions? 5

Take the simplest case, swapping the contents of two variables.
A = "cat", B = "dog", and temp is empty.

Step 1: temp = A (A and temp == "cat", B == "dog")
Step 2: A = B (A and B == "dog", temp == "cat")
Step 3: B = temp (A=="dog", B =="cat", and temp still == cat, but you don't care about it anymore, beecause you're done)

That's a standard, basic swap. You should be able to implement that for your array.

Thanks i will try that so basically it is a basic swap and it should do the trick. I going to try that in a bit.

basically its

array[1,2,3,4,5,6,7,8,9,10];


int temp=0;
for(int i=1,j=0;i<array.length-1;j++,i++)
{
    array[i]=i;  //initialize array from 0=0-10
    temp = array[i] // dump all
    arra[i]=array[j]
    temp=array[j]
 
    
   
}

i am not getting the out put i want i am getting output of
12345678910
instead of 89101234567 // that is if i am going 3 places.
i copied the array in to a temp then copied the array in to in to array[j] and then temp in to array[j]. what am i doing wrong can some one help me out on this please. how do you copy a last element of an array in to a temp variable i tried
temp=array; and got out put of temp :12345678910. when all i wanted was the 10 to go in to temp. I also tried data.length-1 but that did not work. Not to sure what to do. i am not using pointers.

temp = array[i] // dump all
arra[i]=array[j]
temp=array[j]

Remember, you don't care what ends up in temp - that's why it's called temp. So if your last assignment is to temp, you're doing something wrong.

i typed it incorrect my last assignment is
array[j]=temp still get same result.


but it i look at the first part
int temp=0;
for(i=1;j=0;i<data.length;i++,j++)
temp=array; // its copying the entire contents of the array in to temp instead of just the last number which would be 10 for example

What is your code supposed to do? Can you explain what you want those 3 lines of code to do?

i want my code to do is print the last element of the array which is a 10 and copy it just in to temp so that the temp file has the 10 in it

print the last element of the array

You can Use the print() method to print an element of an array.
Find the last element in an array by indexing the array at the last element whose location is equal to the length of the array minus one.

What your code does is copy ALL of the elements of the array one after the other (the looping) into the variable: temp. Sort of a waste of time copying all those elements when you only want the last one.

Have you taken a piece of paper and drawn an array and set an index/pointer to an element and walked thru what your code does?

i took a peace of paper and drew out the array but i am not using pointer cuz the assignment does not require pointers

The pointer I was referring to was something that you would draw on the paper to represent the value of the index. It would "point" at the element in the array that the index referencs.

Java doesn't have pointers. (with the exception you can get a NullPointerException)

The pointer I was referring to was something that you would draw on the paper to represent the value of the index. It would "point" at the element in the array that the index referencs.

Java doesn't have pointers. (with the exception you can get a NullPointerException)

Sorry then i understand now will give it another try

Sorry i am not sure how to point to a certain index in an array . I have so far
temp=array[array.length-1] // that points to the last element of the array i also found out you can change the number and it movies down the index of the array so example is temp=array[array.length-x] lets say x=3
here is some code : what i have so far

int x = 3  // user enter this number for example.
code
  for(i=0;i<data.length;i++)
     array[i]=i; 
    array[j]=array[i] //copied original content in to array[j] 
   temp=array[array.length-x]  // takes the last element and copies in to temp
                                // x is to go down the array  
    array[i]=temp; //assignment of temp in to array

geting a out put of :
0 0 0 0 0 0 0 8 9 9

i would like to know how would you just select one number in the index and copy it to a temp ? for example i want to select the number 10 copy it to temp then copy it to an second array then copy it back to the first array but copy it to the front of the array so for example
it would print 10,1,2,3,4,5,6,7,8,9. if i can figure that out rest should be easy i used a peace of paper and figured it out but some how i am confused some were. is there any way you can point me in right direction of give me an idea on how to do this please. If anything can you give me some ideas i can use for google. I know this is something simple.

how would you just select one number in the index and copy it to a temp

temp = anArray[index]; // copy element selected by index to a temp

i want to select the number 10

Is the '10' the contents of the array or the address of/index into the array?

I suggest that you work with an array with at most 4 elements to work out your logic.
What works for that will work for the larger arrays.

Given: ABCD in an array, write down the steps to move it one slot so the array now has: DABC

This moves the contents of the array one notch higher and wraps at the end to the beginning.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.