Function that reverses the values of the vector into another vector

Asked

Viewed 239 times

-1

I have the following code:

import java.util.Scanner;

            public class ativ15 {

                public static void main (String[] args) {

                Scanner sc = new Scanner (System.in);

                int i;
                int A[] = new int [10];
                int B[] = new int [A.length];

                System.out.println ("Digite os elementos do vetor: ");

                for (i=0; i<10; i++){               
                A[i] = sc.nextInt();
            }

                GeraInverso (A, B);

        }

                static void GeraInverso (int A[], int B[]) {

                int i, temp;

                for (i=0; i<A.length; i++){
                B[i] = A[i];
            }

                for (i=0; i<10; i++) {

                if (i<5) {
                temp = B[i];
                B[i] = B[10-i-1];
                B[10-i-1] = temp;
                }
            }                                           

       }

}

I need the main function to print the value of the vector B (the inverse Generating function reverses the values of the vector A and copies to the vector B). For this what should the Generationinverse function return? How should I proceed?

1 answer

1


There is no need to return anything. Vectors in Java are passed by reference, so changes occurred in GeraInverso are reflected in the main.

  • then how do I print the vector in the main function? because I’ve tried it in some ways and give different errors

  • Do as you did to read it: a loop. And print B[i]

  • 1

    was missing a } that generated several errors. Thank you!

  • @Leko’s previous answer, which you apparently did not like, shows all that you want to know here, which was not answered and you liked: http://answall.com/a/164296/101

Browser other questions tagged

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