Multiplication between two vectors for a final sum in Java

Asked

Viewed 164 times

0

I would like to perform a multiplication between two vectors, each with its corresponding, as I can illustrate below:

i = [a1, a2, a3, ...]
j = [b1, b2, b3, ...]

I could end up with a third vector, like this one:

i x j = [a1*b1, a2*b2, a3*b3, ...]

I explain to you why this, because at the end of this operation, I would like to add all the components of this vector.

The application of the code will be for the user to provide sizes (millimeters) in one vector, and quantity (pieces) in another vector, so need there to be multiplication to obtain the total linear quantity of the product and at the end the linear quantity of everything that was provided.

The code I thought goes below:

    int n = 5; 
    int comprimento_vetor[] = new int[n]; 
    int i = 0;

    int s = 5;
    int qtd_vetor[] = new int[s];
    int j = 0;

while (i <= 4) {
            System.out.printf("Inserir o comprimento da telha T%d: ", (i + 1));
            comprimento_vetor[i] = sc.nextInt();
            if (i > 4) {
                break;
            } else {
            System.out.printf("Gostaria de inserir nova telha? S / N: ");
            String confirmacao_telha = sc.next();
            char letra = confirmacao_telha.charAt(0);
            if (letra == 'S') {
                i++;
            } else {
                System.out.printf(
                            "--------%n" 
                            + "Comprimentos:%n" 
                            + "T1: %d%n" 
                            + "T2: %d%n" 
                            + "T3: %d%n" 
                            + "T4: %d%n"
                            + "T5: %d%n" 
                            + "--------%n",
                        comprimento_vetor[0], comprimento_vetor[1], 
                        comprimento_vetor[2], comprimento_vetor[3],
                        comprimento_vetor[4]);

                while (j <= 4) {
                    System.out.printf("Inserir a quantidade da telha T%d: ", (p + 1));
                    qtd_vetor[p] = sc.nextInt();
                    System.out.printf("Gostaria de inserir nova quantidade? S / N: ");
                    String confirmacao_qtd = sc.next(); 
                    char letra1 = confirmacao_qtd.charAt(0);
                    if (letra1 == 'S') {
                        j++;
                    } else {
                        System.out.printf(
                                "--------%n" 
                                + "Quantidades:%n" 
                                + "T1: %d%n" 
                                + "T2: %d%n" 
                                + "T3: %d%n" 
                                + "T4: %d%n"
                                + "T5: %d%n" 
                                + "--------%n",
                            qtd_vetor[0], qtd_vetor[1], 
                            qtd_vetor[2], qtd_vetor[3],
                            qtd_vetor[4]);
                        break;
                    }       
                }
  • And what is the doubt / the problem?

  • I would like to know how to implement the multiplication code of the positions of the vectors I commented at the beginning of the text, I tried to contextualize too much I believe what I was implementing.

1 answer

0


Vector multiplication:

int ixj[] = new int[5]; //Declaração do vetor para armazenar multiplicação dos vetores.

for (int k = 0; k < 5; k++){ //5 é o tamanho da alocação do vetor.
    ixj[k] = i[k] * j[k]; //Multiplicação dos vetores, k é a posição dos vetores, fazendo com que faça a operação nas posições 0, 1, 2, 3 e 4.
}

Sum of vectors:

int ij = 0; //Declaração do inteiro para armazenar somatório dos vetores. Lembre-se de declarar 0 como valor inicial.

for (int k = 0; k < 5; k++){ //5 é o tamanho da alocação do vetor.
    ij = ij + ixj[k];
}

Another way of doing:

for (int k = 0; k < 5; k++){ //5 é o tamanho da alocação do vetor.
    ij += ixj[k];
}

Multiplication and summation in the same structure:

int ixj[] = new int[5], ij = 0, k;

for (k = 0; k < 5; k++){
    ixj[k] = i[k] * j[k];
    ij += ixj[k];
}
  • Thank you so much for your help! I believe I will be able to implement this, it was basically what I was looking for. I thank you!

  • Is there any way to solve this problem? When implementing: Main.java:80: error: array required, but int found ixj[k] = i[k] * j[k]; Main.java:80: error: array required, but int found ixj[k] = i[k] * j[k];

  • The problem is in the array object declaration. It has been fixed in the reply, where the error is missing brackets ([]).

Browser other questions tagged

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