participate


New To Java - circular shift array problem [Locked]
This topic is locked
<<   Back to Forum  |   Give us Feedback
10 Duke Stars available
This topic has 25 replies on 2 pages.    1 | 2 | Next »
dukefan444
Posts:42
Registered: 2/12/07
circular shift array problem   
Mar 19, 2007 10:13 PM

 
Hey all! My program is supposed to take an array of 10 integers and shift them by as many as the user wants (which can be up to 9 because of you shift the 10th integer in the array you would get the same array you started with back).

So lets say this is your array: 1,2,3,4, 5, 6, 7, 8, 9,10

And the user enters 3 as the number to shift by: then you should get an output of 8, 9, 10, 1, 2, 3, 4, 5, 6, 7

Here is my code:
import javax.swing.JOptionPane;
public class shiftarray {
public static void main(String[] args) {
int[] a = new int[10];
int[] b = new int[10];
for (int j = 0; j < a.length; j++) {
int n = Integer.parseInt(JOptionPane.showInputDialog(null,
"Enter an integer: "));
}
for (int i=0; i < n; i++){
b[i+1] = a[i];
if(a[i+1] > a.length-1)
b[0]=a[i];
}
}
}

It doesn't work. It runs and lets you enter a number but it stops after you enter a number. Any ideas as to why this is and what I need to do to get this program running correctly?
 
dmbdmb
Posts:4,208
Registered: 26/05/00
Re: circular shift array problem   
Mar 19, 2007 10:20 PM (reply 1 of 25)  (In reply to original post )

 
b[i+1] = a;


how does this even compile?
 
lethalwire
Posts:355
Registered: 1/23/06
Re: circular shift array problem   
Mar 19, 2007 10:23 PM (reply 2 of 25)  (In reply to #1 )

 
Also, the variable 'n' is out of scope on your last for loop.
It doesn't compile, nor does it execute. You think you posted the wrong code?
 
benubach
Posts:256
Registered: 3/6/07
Re: circular shift array problem   
Mar 19, 2007 10:25 PM (reply 3 of 25)  (In reply to original post )

 
Please put your code between code tags.

How are you filling the array? Does it even compiles? (The code you provided will not compile BTW) Are you getting an error?
 
flounder
Posts:14,943
Registered: 2/03/05
Re: circular shift array problem   
Mar 19, 2007 10:37 PM (reply 4 of 25)  (In reply to #1 )

 
b[i+1] = a;


how does this even compile?

It should be
b[i+1] = a[i];

Due to the unformatted code the i is treated as the italics tag.
 
lethalwire
Posts:355
Registered: 1/23/06
Re: circular shift array problem   
Mar 19, 2007 10:48 PM (reply 5 of 25)  (In reply to #4 )

 
Assuming that you take out and initialize 'n' so that it is in the scope of the last for loop.

You still have a major chance of the the second for loop going out of bounds.
For instance, if the last value in 'n' was 200; then the for loop runs until i < 200.

And of course, you're array is not 200 slots big.

Now, if you replace the second for loop with

for(int i = 0; i < a.length; i++)


you will see you will still get OutOfBounds errors.
For example, when i becomes 9, the next statement:
b[i+1] = a[i];


is the same as

b[10] = a[9]



And by the given code, the highest spot for b is b[9]
 
benubach
Posts:256
Registered: 3/6/07
Re: circular shift array problem   
Mar 19, 2007 10:58 PM (reply 6 of 25)  (In reply to #5 )

 
Also, you're shifting the two arrays without initializing them, so they're basically {0,0,0,0,0,0,0,0,0,0}
It doesn't matter how much you rotate them, they'll always be the same ;-)
 
lethalwire
Posts:355
Registered: 1/23/06
Re: circular shift array problem   
Mar 19, 2007 11:01 PM (reply 7 of 25)  (In reply to #6 )

 
Yes, there is obviously quite a few errors in the given code.

I think in the first for loop you mean to initialize the array with the values of n.
In that case you would need:

for(int i = 0; i < a.length; i++) {
    n = Integer.parseInt(JOptionPane.showInputDialog(
                                         null, "Enter an Integer");
 
    a[i] = n;
}


null
 
Peetzore
Posts:682
Registered: 11/12/06
Re: circular shift array problem   
Mar 19, 2007 11:38 PM (reply 8 of 25)  (In reply to #7 )

 
From what I understand of the first question, the initialisation loop should be:
for (int i=0; i < a.length; ++i) {
    a[i] = i+1;
} 


your shifting loop should not loop from 0 to n, cause that will only shift n elements 1 spot. You have to loop from 0 to a.length-1 and set
b[ i ] = a[(i + n) % a.length]


It might also be a good idea to print something after you shift the values, otherwise it might look like the code just stops...

Message was edited by:
Peetzore
 
baskarpr
Posts:53
Registered: 1/24/07
Re: circular shift array problem   
Mar 19, 2007 11:39 PM (reply 9 of 25)  (In reply to #8 )

 
The simple logic here goes:
if array size is 10 and n is 3,
1. Start from 7 th position. Move 7th element to 0th of new array, 8th to 1st and so on.
Now you got shifted elements in base position of new array.
2. Now take the 1st element of source array and move it to n'th position of new array, 2nd to n+1 etc.

Does it sound clear ?
 
Peetzore
Posts:682
Registered: 11/12/06
Re: circular shift array problem   
Mar 19, 2007 11:53 PM (reply 10 of 25)  (In reply to #8 )

 
b[ i ] = a[(i + n) % a.length]

Of course this should be
b[(i + n) % a.length] = a[ i ]
 
JosAH
Posts:13,022
Registered: 4/6/04
Re: circular shift array problem   
Mar 20, 2007 12:27 AM (reply 11 of 25)  (In reply to #10 )

 
You can even do that rotate in place, i.e. without a additional array. All
that is needed is a 'reverse' method that reverses (part of) an array:
int[] reverse(int[] a, int i, int j) { // reverse elements i, i+1 ... j-1
   for (; --j > i; i++) {
      int t= a[i]; a[i]= a[j]; a[j]= t;
   }
   return a; // convenience.
}

Here's the shiftRight method in terms of the reverse method:
int[] shiftRight(int[] a, int i) {
   return reverse(reverse(reverse(a, 0, a.length), 0, i), i, a.length);
}

I'm sure you can do a shiftLeft method yourself now.

kind regards,

Jos
 
Peetzore
Posts:682
Registered: 11/12/06
Re: circular shift array problem   
Mar 20, 2007 12:38 AM (reply 12 of 25)  (In reply to #11 )

 
Nice one, Jos!
 
JosAH
Posts:13,022
Registered: 4/6/04
Re: circular shift array problem   
Mar 20, 2007 12:51 AM (reply 13 of 25)  (In reply to #12 )

 
Nice one, Jos!

Thanks; it's an ancient trick from the times when memory was measured
in <everybody goes: aaahhhhh!>Kilo bytes</aaahhh> You can move
an arbitrary block around an array without using an additional array.
It's mostly forgotten nowadays ;-)

kind regards,

Jos
 
Peetzore
Posts:682
Registered: 11/12/06
Re: circular shift array problem   
Mar 20, 2007 1:13 AM (reply 14 of 25)  (In reply to #13 )

 
Ahh, the good ol' days... Wasn't that the time when animals used to talk? Guess I'm one of those young luxorious little puppies that can't imagine what that time could have been like!
 
This topic has 25 replies on 2 pages.    1 | 2 | Next »
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 28
  • Guests : 129

About Sun forums
  • Oracle Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Oracle Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums