471,852 Members | 913 Online

# Shift right and left method

Hi all,
I will show you the solution of question , Sometime i have problem in implement the code in BlueJ but i understant what they want frome me in question. this is question
1/ Write a method that takes an array of floats and right shift the numbers one step.
public float ShiftR(float N,float[] arr){
int i = 0;
int j = arr.length -1;
while(i<=j){
int finalNomber =(j)+1;//----------->move to first index
if (N==arr[finalNomber])

i++;
j--;}

}
2/ Write a method that takes an array of floats and left shift the numbers one location.
public float ShiftL(float N,float[] arr){
int i = 0;
int j = arr.length -1;
while(i<=j){
int firstNomber =(i)+arr.length -1;//----------->move to Final index
if (N==arr[firstNomber])

i--;
j++;}

}
when i did it in Bluej it say no wrong , please tell me if my work is right and can you plese tell me the step to test this code
Dec 13 '07 #1
5 10354
JosAH
11,448 Expert 8TB
I'm not sure I understand what you mean by 'shift a number one element to the
left/right'. Do you mean something like this:

a,b,c,d,e is the array of floating point numbers; after shifting it to the right, you
want x,a,b,c,d where 'x' is an unknown number. Similar, after shifting to the left:
b,c,d,e,x where 'x' again is an unknown number.

kind regards,

Jos
Dec 13 '07 #2

yes like the example above
and the example i have in my question... for shift right
1-3-4-5-6
6-1-3-4-5
and for shift left
1-2-3-4
2-3-4-1

I hope you understand what i mean ^_^
Dec 14 '07 #3
JosAH
11,448 Expert 8TB

yes like the example above
and the example i have in my question... for shift right
1-3-4-5-6
6-1-3-4-5
and for shift left
1-2-3-4
2-3-4-1

I hope you understand what i mean ^_^
Yep I do; us oldtimers call that a 'rotate' operation, either left or right. Let's do the
rotate right operation: you copy every element one position to the right after you've
saved the rightmost element somewhere else first. At the end you stick that element
back in again at the leftmost position.

Things work similar for the rotate left operation; what's the problem then?

kind regards,

Jos
Dec 14 '07 #4
Yep I do; us oldtimers call that a 'rotate' operation, either left or right. Let's do the
rotate right operation: you copy every element one position to the right after you've
saved the rightmost element somewhere else first. At the end you stick that element
back in again at the leftmost position.

Things work similar for the rotate left operation; what's the problem then?

kind regards,

Jos
Hi Jos,
ok i do like what you mean in these code and when implement in Bluej ,it said that no error ,the problem now how to test it to sure that my code work. tell me if these step is right please
i think i should identify an arr of number then i call Shift right to the that array
Dec 15 '07 #5
JosAH
11,448 Expert 8TB
Hi Jos,
ok i do like what you mean in these code and when implement in Bluej ,it said that no error ,the problem now how to test it to sure that my code work. tell me if these step is right please
i think i should identify an arr of number then i call Shift right to the that array
You need two identical arrays a and b, apply that rotate right method on array b
and after check if a[i] == b[(i+1)%b.length] (for every i) A similar formula applies
for a rotate left operation.

kind regards,

Jos
Dec 15 '07 #6