Insert data into a vector to then print and multiply

Asked

Viewed 730 times

1

Write an algorithm that reads two vectors of 10 positions and make the multiplication of the elements of the same index, placing the result in a third vector. Show the resulting vector.

I wanted to first show the typed values and then start to elaborate the step by step multiplication. That’s how I would start?

int main(void) {

    int vetorA[10] = {0};
    int vetorB[10] = {0};

    int a,b;

    for(a = 0; a < 10; a++){
        printf("Vetor A: \n");
        a=getchar();


    for(b = 0; b < 10; b++){
        printf("Vetor B: \n");
        b=getchar();





        }
    }

    return 0;
}
  • 3

    What language?

  • Language c, grateful if you can help

  • 1º The way you are getting the data is wrong Wikipedia - Simple Input and Output 2º You can show the values in it for where to use to sum the vectors a and b ( Then you do another to show the sum value next to the Dice if you want ) 3º You should start the counters with 0 in your homes.

3 answers

1

Vc is initiating its vectors with only one value {0} instead of having 10 values: {0,11,22,31,48,54,62,71,84,99}.

You better put the length of the array instead of 10: for(b = 0; b < vetorA.length; b++).

To "show" vc must concatenate "Vector B: n" with vectorB[b], see that the "b" between brackets is to indicate the position of the vector, i.e.: printf("Vetor B: \n" + vetor[b]);

To do the multiplication vc does a for to traverse the positions of the two vectors using the "Count" to indicate the position of the vectors.

for(i = 0; i < 10; i++)
{
    printf("Valor multiplicado: \n" + (vetorA[i] * vetorB[i]);
}
  • You are initiating with 0 in all elements.

1


There are several errors in this code, it’s not even close to doing something useful. So at least it starts right:

#include <stdio.h>

int main(void) {
    int vetorA[10] = { 0 };
    int vetorB[10] = { 0 };
    for (int i = 0; i < 10; i++) {
        printf("Vetor A: \n");
        scanf("%d", &vetorA[i]);
    }
    for (int i = 0; i < 10; i++){
        printf("Vetor B: \n");
        scanf("%d", &vetorB[i]);
    }
}

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

This is the right start, now to print is the same algorithm, only changes the scanf() for printf(). Multiplication is done in the same way as it does without being a vector, but the variable always has the index [], will make another loop, or together with the loop printing.

  • to print only the values on the screen for now.. would that be? int main(void) { int vector[10] = { 0 }; int vetorB[10] = { 0 }; int i; for(i = 0; i < 10; i++){ printf("Vector A: n"); scanf("%d", &vector A[i]); for(i = 0; i < 10; i++){ printf("Vector B: n"); scanf("%d", &vetorB[i]); } } for(i = 0; i < 10; i++){ printf("Vector A: %d n ",vector A[i]); printf("Vector B: %d n ", vector B[i]); } system("pause"); Return 0; }

  • It may be so.

  • Because I need to give the space in this part (between the keys that are the "0") : Vector[10] = { 0 }

  • @Rodrigomuniz to be more readable.

  • "You’re nowhere near doing anything useful." let’s face it - -these artificial exercises to learn C are bad - this one won’t even be close to doing something useful even if 100%.

  • @jsbueno ah, almost everyone is like this, there is not much way, but at least it is known that all these people will never program in C professionally.

Show 1 more comment

0

to print only the values on the screen for now.. would be so?

int main(void) {

    int vetorA[10] = { 0 };
    int vetorB[10] = { 0 };

    int i;

    for(i = 0; i < 10; i++){
        printf("Vetor A: \n");
        scanf("%d", &vetorA[i]);

    for(i = 0; i < 10; i++){
        printf("Vetor B: \n");
        scanf("%d", &vetorB[i]);

        }   

    }

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

    printf("Vetor A: %d\n ",vetorA[i]);

    printf("Vetor B: %d\n ", vetorB[i]);

    }

    system("pause");
    return 0;
}
  • Question/feedback of the OP in the answers area Source: You used the answers area to add clarifications or ask a question. Instead, it is better to include this content in the question itself. To do this, just click on the [Edit] link, which is just below the question. Thus, the content is all gathered in one place, and those who arrive here need not be looking for information in various responses and comments to understand the problem.

Browser other questions tagged

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