Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

macman2790

macrumors 6502a
Original poster
Sep 4, 2006
716
1
Texas
i'm reviewing for a test and this is about the only thing that's bothering me.
heres the code:
Code:
//Write a code segment that reverses the elements in the integer array, a.
int temp;
for (int i=0;i<a.length/2;i++)
   temp = a[i];
   a[i] = a[a.length - i - 1];
   a[a.length - i - 1] = temp;
 

plinden

macrumors 601
Apr 8, 2004
4,029
142
Is that your own code or from a teacher or book? Since all that does is set temp, and is equivalent to:
Code:
int temp;
for (int i=0;i<a.length/2;i++) {
   temp = a[i];
}
a[i] = a[a.length - i - 1];
a[a.length - i - 1] = temp;

In other words, you (or your teacher or text book) will have to be more careful about defining blocks of code.

The following makes more sense with your question:
Code:
int temp;
for (int i=0;i<a.length/2;i++) {
   temp = a[i];
   a[i] = a[a.length - i - 1];
   a[a.length - i - 1] = temp;
}

If you don't understand how this works, do it on paper, with the array a going from 0 to 9 so you have the loop going from 0 to 5.
 

macman2790

macrumors 6502a
Original poster
Sep 4, 2006
716
1
Texas
Yeah it is intriguing...

Why is it looped up to a.length/2?

no idea, that's one of the things that's killing me, but another part i don't understand is the is the part to the right of it which is that -i -1. yeah it's my teacher, i know, i have to deal with it everyday. i see what you mean about the block
 

macman2790

macrumors 6502a
Original poster
Sep 4, 2006
716
1
Texas
Is that your own code or from a teacher or book? Since all that does is set temp, and is equivalent to:
Code:
int temp;
for (int i=0;i<a.length/2;i++) {
   temp = a[i];
}
a[i] = a[a.length - i - 1];
a[a.length - i - 1] = temp;

In other words, you (or your teacher or text book) will have to be more careful about defining blocks of code.

The following makes more sense with your question:
Code:
int temp;
for (int i=0;i<a.length/2;i++) {
   temp = a[i];
   a[i] = a[a.length - i - 1];
   a[a.length - i - 1] = temp;
}

If you don't understand how this works, do it on paper, with the array a going from 0 to 9 so you have the loop going from 0 to 5.
I'm not quite sure how to do it on paper, the only thing i see is the 0 to 5 thing.
 

plinden

macrumors 601
Apr 8, 2004
4,029
142
Doing a of length 6 for simplicity, write the following on a piece of paper
Code:
        start     i = 0     i = 1
a[0]      0         5         ?
a[1]      1         1
a[2]      2         2
a[3]      3         3
a[4]      4         4
a[5]      5         0

step 0, i = 0, a.length - i - 1 = 5
-------------------------------------
temp set to a[0] = 0
a[i] is a[0], set to a[5], ie. a[0] = 5
a[a.length - i - 1] is a[5], set to temp, ie. a[5] = 0

step 1, i = 1, a.length - i - 1 = 4
-------------------------------------
temp set to a[1] = 1
etc
Do the rest yourself. Continue the loop to a.length/2 (i.e. 3)
 

macman2790

macrumors 6502a
Original poster
Sep 4, 2006
716
1
Texas
Doing a of length 6 for simplicity, write the following on a piece of paper
Code:
        start     i = 0     i = 1
a[0]      0         5         ?
a[1]      1         1
a[2]      2         2
a[3]      3         3
a[4]      4         4
a[5]      5         0

step 0, i = 0, a.length - i - 1 = 5
-------------------------------------
temp set to a[0] = 0
a[i] is a[0], set to a[5], ie. a[0] = 5
a[a.length - i - 1] is a[5], set to temp, ie. a[5] = 0

step 1, i = 1, a.length - i - 1 = 4
-------------------------------------
temp set to a[1] = 1
etc
Do the rest yourself. Continue the loop to a.length/2 (i.e. 3)
thanks, you were a big help.
 

jeremy.king

macrumors 603
Jul 23, 2002
5,479
1
Holly Springs, NC
plinden - you are thorough!

macman, if you haven't noticed, that algorithm walks through the first half of the array swapping elements.

1st pass - swaps 1st and last element
2nd pass - swaps 2nd and 2nd to last element
and so on.

If you traverse the entire array (instead of only half) you would simply end up with the original array.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.