Store values in inverted order

Asked

Viewed 490 times

1

The objective is the following: read 10 values of a given vector, after that, create another vector, which has the same values of the first vector, but in inverted order, for example, the first value of the 1st vector is the tenth of the 2nd vector, the second value of the 1st vector is the penultimate of the 2nd vector, and so on. I tried to do but the second vector always has the value of 0. I know I have to use a for for this, but I do not know if I used in the right way.

public static void main(String[] args) {

    int [] firstArray = new int [10];
    int [] secondArray = new int [10];

    for (int i = 0; i < firstArray.length; i++) {
        firstArray[i] =(int)Math.round(Math.random()*50); //Gerando valores aleatórios
    }

    //Passando valores
    for (int i = 9; i <= 0; i--) {
        for (int j = 0; j < secondArray.length; j++) {
            secondArray[j] = firstArray[i];
            break;
        }
    }

    //Apresentar valores
    System.out.println("\nPrimeiro Array:");
    for (int i = 0; i < firstArray.length; i++) {
        System.out.print(firstArray[i]+"; ");
    }

    System.out.println("\nSegundo Array:");
    for (int i = 0; i < secondArray.length; i++) {
        System.out.print(secondArray[i]+"; ");
    }
    System.out.println("");
}
  • This doesn’t make any sense at all. Enter the rest of the code.

  • @bigown I made some changes, see if you can help me :)

  • Wouldn’t it be better to put the code you’re doing instead of posting loose snippets?

  • @I put Bigown, but I guess it hasn’t changed much ...

1 answer

2


How you’ll only touch one array, only needs one loop. To find the other’s element array just use math. Just calculate the index backwards. Could scan the array from the first to the last that the formula works equal. The formula of inversion of the elements is universal. Moreover there was a wrong sign in the first loop preventing its execution. The right one would be:

for (int i = firstArray.length - 1; i >= 0; i--) {
    secondArray[secondArray.length - i - 1] = firstArray[i];
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Ideally it should have a constant with the size of the array to be used in both, so ensure that both have the same size. It does not affect the code at all, but gives more semantics to what is desired. The way it is, if someone tampers with the code, they might not want to create problems.

If there was a need for two ties, it could not be in the form used, the break was closing the loop in the first passage, so the loop did not actually occur.

  • I hadn’t even thought about it, man. In fact, one could do this action in the loop where the values of the first vector are assigned, without needing a second for.

  • 1

    @Danielsantos yes, I gave, I did not do it because I thought it was a requirement of the exercise. Even date to print the data of the first. The code only needed two loops.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.