Insert data into a vector to then print and multiply

1
  

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

I wanted to show the values you entered first, and then start the multiplication step by step. Is this how it 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;
}
    
asked by anonymous 14.03.2017 / 14:12

3 answers

1

You have several errors in this code, you are 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]);
    }
}

See running on ideone . And at Coding Ground . Also put it on GitHub for future reference .

This is the right start, now to print is the same algorithm, only changes the scanf() to printf() . The multiplication is done in the same way as it does without being a vector, except that the variable always has the index [] , will do another loop , or do along with loop printing.

    
14.03.2017 / 14:26
1

You are initializing your vectors with only one value {0} instead of having 10 values: {0,11,22,31,48,54,62,71,84,99}.

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

To "show" you must concatenate "Vector B: \ n" with vectorB [b], see that the "b" in square brackets is to indicate the position of the vector, ie: printf("Vetor B: \n" + vetor[b]) ;

To do multiplication you make 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]);
}
    
14.03.2017 / 14:26
0

To print only the values on the screen for now .. would it be this way?

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;
}
    
14.03.2017 / 15:20