Java: How do I store a vector in another vector and using library Math

-2

My question is this, I have to read 20 integers in a vector and then I have to calculate the square of that vector and store it in another one and then display the two.

Only at the time of making the square I use a java library which is the Math the code is so at the time of calculating:

for (int i = 0; i<num2.length; i++){
        int num2[i] = Math.sqrt(num[c]);
    }

But it is giving error.

    
asked by anonymous 23.08.2018 / 18:53

3 answers

0

You can make a for that runs through your array and stores n² in the new array like this:

for (int i = 0; i<arrayValores.length, i++){
   int arrayQuadrado[i] = pow(arrayValores[i],2);
}
    
23.08.2018 / 19:03
0
The c variable is only visible on the first for, but since the arrays are the same size you can use the i variable for both arrays.

    public static void main(String[] args) {
        int[] num = new int[5];
        int[] num2 = new int[5];

        Scanner entrada = new Scanner(System.in);

        for (int c = 0; c < num.length; c++) {
            System.out.println("Numero: ");
            num[c] = entrada.nextInt();
        }

        for (int i = 0; i < num2.length; i++) {
            num2[i] = (int) Math.pow(num[i], 2);
        }

        entrada.close();
    }
    
23.08.2018 / 19:05
0

Using Java 8+, you can generate the array with the squares on a single line. Assuming your array is the numeros variable, then you can do this:

int[] quadrados = IntStream.of(numeros).map(x -> x * x).toArray();

To read the 20 numbers, you can also do with two lines:

Scanner entrada = new Scanner(System.in);
int[] numeros = IntStream.generate(entrada::nextInt).limit(20).toArray();

To display the array, you can do in a row too:

System.out.println(Arrays.toString(quadrados));

Here is the resulting complete code. Note that it is very simple, it only needs 5 lines between { and } of method main :

import java.util.Arrays;
import java.util.Scanner;
import java.util.stream.IntStream;

class Teste {
    public static void main(String[] args) {
        Scanner entrada = new Scanner(System.in);
        int[] numeros = IntStream.generate(entrada::nextInt).limit(20).toArray();
        int[] quadrados = IntStream.of(numeros).map(x -> x * x).toArray();
        System.out.println(Arrays.toString(numeros));
        System.out.println(Arrays.toString(quadrados));
    }
}

Given this entry:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

This output is produced:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400]

See here working on ideone.

Ah, Math.sqrt(...) is for calculating the square root. To calculate the square, the simplest way is to multiply each element by itself as in the code above. Note that the code for creating the array with the squares works regardless of the size of the original array.

If you want a message to appear to ask for the number, you can do something like this:

int[] numeros = IntStream.generate(() -> {
    System.out.println("Digite um número: ");
    return entrada.nextInt();
}).limit(20).toArray();
    
23.08.2018 / 20:04