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?
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]
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 ;-)
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.
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.
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 ;-)
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 »