469,304 Members | 1,888 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,304 developers. It's quick & easy.

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 10261
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
thank you for reply.....

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
thank you for reply.....

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

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Glen Able | last post: by
388 posts views Thread by maniac | last post: by
43 posts views Thread by Mehta Shailendrakumar | last post: by
11 posts views Thread by Kenneth Lantrip | last post: by
5 posts views Thread by Schüle Daniel | last post: by
12 posts views Thread by Mick_fae_Glesga | last post: by
4 posts views Thread by sandhya | last post: by
7 posts views Thread by Bint | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.