0

I thought about it, tried different things for about an hour but couldn't come up with anything. I didn't want to post here because I wanted to figure it out myself but there's just something i'm not getting

class Exercise5
{
public static void main ( String[] args )
{
int[] val = {0, 1, 2, 3}; 
int temp;

System.out.println( "Original Array: " 
    + val[0] + " " + val[1] + " " + val[2] + " " + val[3] );

// reverse the order of the numbers in the array
for( temp = 0; temp < val.length; temp++) {

    val[temp] = val[3 - temp];


}



System.out.println( "Reversed Array: " 
    + val[0] + " " + val[1] + " " + val[2] + " " + val[3] );
 }
}

It' prints out 3 2 2 3 with every different strategy i've tried

user3473451
  • 53
  • 2
  • 8

8 Answers8

3

Problem:

temp = 0

val[0] =val[3];

temp =1

val[1] =val[2];

temp =2

val[2] =val[1];

temp =3

val[3] =val[0];

Solution:

Use another array to hold reversed values or swap two values at a time using a temporary variable (0 & 3, 1&2)

jmj
  • 237,923
  • 42
  • 401
  • 438
2
for( temp = 0; temp < val.length; temp++) {
    val[temp] = val[3 - temp];
}

What this does is this;

val = [0, 1, 2, 3]

val[0] = val[3]
=> val = [3, 1, 2, 3]

val[1] = val[2]
=> val = [3, 2, 2, 3]

val[2] = val[1]
=> val = [3, 2, 2, 3]

val[3] = val[0]
=> val = [3, 2, 2, 3]

What you want to do is loop the index through half the array and swap the low and high value.

Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
0

Try this

// reverse the order of the numbers in the array
for( temp = 0; temp < val.length/2; temp++) {
    int tmp = val[temp];
    val[temp] = val[3 - temp];
    val[3 - temp] = tmp;

}
CMPS
  • 7,733
  • 4
  • 28
  • 53
0

You have to use another array. You are changing the value of the original before you want to. Or if the length is even you can swap them

chad martin
  • 31
  • 1
  • 7
  • You can swap them in place without another array, even if the length is odd. Just don't swap the middle item (or swap it with itself, which is a no-op). – David Conrad Apr 07 '14 at 18:50
0

Firstly,

val[temp] = val[3 - temp];

only assigns the value of val[3-temp] into val[temp], it does not swap them. If you want to swap them, you need to use a temporary variable.

Second,

You only need to iterate halfway through the array, not all the way through it.

Renan
  • 827
  • 8
  • 16
0

val[temp] = val[3 - temp];

when temp =0

val[0]= val[3-0] val[0] = val[3]= 3

when temp =1

val[1]= val[3-1] val[1] = val[2]= 2

when temp =2

val[2]= val[3-2] val[2] = val[1]= 2 (previous step you can see val[1]=2)

when temp =3

val[3]= val[3-3] val[3] = val[0]= 3

result 3 2 2 3

Divyam shukla
  • 2,038
  • 14
  • 18
0

This line is culprit inside the for loop:

val[temp] = val[3 - temp];

Let's go over the iteration to see what it's doing.

  1. val[0] = val[3] -> 3,1,2,3
  2. val[1] = val[2] -> 3,2,2,3
  3. val[2] = val[1] -> 3,2,2,3
  4. val[3] = val[0] -> 3,2,2,3
Pankaj Gadge
  • 2,748
  • 3
  • 18
  • 25
0

Most of the people have given you the analysis. This is what you can do to resolve.

Declare one more int array

int[] val = {0, 1, 2, 3}; 
int temp;
int[] reverseVal = new int[val.length];

Add to reversed values to this array

for( temp = 0; temp < val.length; temp++) {

    reverseVal [temp] = val[val.length - temp];

}
Crickcoder
  • 2,135
  • 4
  • 22
  • 36