Posted: Sun Apr 20, 2008 12:41 am Post subject: Re: ARRAYS!!- Shift Down...
yup, i'll just copy and paste then!
thanks a lot again!
Sponsor Sponsor
r691175002
Posted: Sun Apr 20, 2008 2:14 am Post subject: Re: ARRAYS!!- Shift Down...
Ah, sorry I came back a little late.
The easiest solution is just to copy and paste the loop like you have done (Make sure you reuse the counter variable though if you think the teacher will count each one separately). If you absolutely have to use a single loop thats where the % operator comes into play:
code:
for (int i = 0; i < LOOP_TIMES*2; i++)
System.out.println (i % LOOP_TIMES);
// % Will wrap i around so that it is always within LOOP_TIMES
// 0 % 3 = 0
// 1 % 3 = 1
// 2 % 3 = 2
// 3 % 3 = 0 // Wrapped around
// 4 % 3 = 1
// 5 % 3 = 2
// 6 % 3 = 0 // Wrapped around again
// 7 % 3 = 1 ...
This will output 0 to LOOP_TIMES-1 twice.
Your code:
code:
for (int i=0; i<MAX; i++) {
temp1 = num [i];
temp2 = num [MAX-1];
num[i]=temp2;
num[MAX-1] = temp1;
}
Becomes:
code:
for (int i=0; i<(MAX*2); i++) {
temp1 = num [i%MAX];
temp2 = num [MAX%MAX-1];
num[i%MAX]=temp2;
num[MAX%MAX-1] = temp1;
}
(Untested, it may have a few off by one errors)
Picking up patterns like this is something that comes with time. I basically played around a bit and finally ended up with something that looked like it would work.
Shivu
Posted: Sun Apr 20, 2008 2:45 pm Post subject: RE:ARRAYS!!- Shift Down...
hi! thanks for that....
but for the % operator, i'm a bit confused on what it does... meaning:
what does it mean to "wrap" i around?, does it reset i?
thanks
jernst
Posted: Sun Apr 20, 2008 3:15 pm Post subject: Re: RE:ARRAYS!!- Shift Down...
Shivu @ Sun Apr 20, 2008 2:45 pm wrote:
hi! thanks for that....
but for the % operator, i'm a bit confused on what it does... meaning:
what does it mean to "wrap" i around?, does it reset i?
thanks
Like the people above have said it gives a remainder from a divison. So it "wraps around" everytime the remainder returns to 0. For your example with 5 elements:
5 % 5 = 0
10 % 5 = 0
15 % 5 = 0
Similarly,
6 % 5 = 1
11 % 5 = 1 ..etc
So instead of trying to figure out when a counter should return back to zero to wrap the array back to the start you can use the modulo (%) to turn the 5 into a 0. I dont know if that made it any more clear or worse but I gave it a try
Shivu
Posted: Sun Apr 20, 2008 3:40 pm Post subject: RE:ARRAYS!!- Shift Down...
no no that was great!
ok... so, regarding the code above (before the above post),
num [i%MAX]; the i%MAX makes sure that i stays less than MAX (which is 5 for example) ??
so like:
code:
for (int i=0; i<MAX*2; i++) {
temp1 = num[i%MAX];
//the index # (of num) will be 0, then it'll be 1, then 2, then 3, then 4, then 5, and then again 0,1,2,3,4,5 =>that'll be when i will be less than MAX *2 (in this case 10). so the loop will be run two times!?
}
cuz if the condition would be i <MAX, then the loop would only run till i = 4...
oh!! i get it:) (or i think i get it...)
if someone could let me know if i'm understanding it properly, that'd be great! -lol
thanks a lot to everyone!!!!
jernst
Posted: Sun Apr 20, 2008 3:43 pm Post subject: Re: ARRAYS!!- Shift Down...