Problem with vector in C

Asked

Viewed 1,132 times

0

I don’t know where it’s wrong, if I put the value 7 in matrizA and 7 in matrizB and 14 in matrizC that will accumulate the value of A and B, for sure but if I place above 14 error.

Make a program to read two matrices of type vector, being A with 20 elements and B with 30 elements.

Construct a matrix C, this being the junction of the two other matrices. This way C should have the capacity to store 50 elements. Display the matrix C.

int matriza[8], matrizb[8], matrizc[16], indice;

for(indice = 0; indice < 8; indice++)
    {
        printf("\n digite o  valor da matriz A: ");
        scanf("%d",&matriza[indice]);
        
        matrizc[indice] = matriza[indice];
    }

for(indice = 8; indice < 16; indice++)
    {
        printf("\n digite o  valor da matriz B: ");
        scanf("%d",&matrizb[indice]);
        
        matrizc[indice] = matrizb[indice];
    }

for(indice = 0; indice < 16; indice++)        
        printf("\n Os valores das matrizes A e B sao: %d \n", matrizc[indice]);
  • Did any help you more? You need something to be improved?

3 answers

1

It seems to me that the problem lies more in the understanding of the problem than in the programming. I saw no error in the program but in what is intended to be done. Anyway, have you chosen one of the hardest languages to learn? I think it’s great how a person starts with C, which means she’ll learn the basics, she’ll know what a few people know. But if you don’t have a natural talent for it you’ll give up because you have too many problems to solve on hand.

I have spoken before and I will say again that programming is detail. If you don’t want to worry about them, really programming will be difficult. Vector is easy. It looks like it’s wrapping itself in other things.

The description of the problem does not seem to make sense.

#include <stdio.h>
#define MaxA 2
#define MaxB 3

int main(void) {
    int matriza[MaxA], matrizb[MaxB], matrizc[MaxA + MaxB], indice;
    for (indice = 0; indice < MaxA; indice++) {
        printf("\n digite o valor da matriz A: ");
        scanf("%d", &matriza[indice]);
        matrizc[indice] = matriza[indice];
    }
    for (indice = 0; indice < MaxB; indice++) {
        printf("\n digite o valor da matriz B: ");
        scanf("%d", &matrizb[indice]);
        matrizc[indice + MaxA] = matrizb[indice];
    }
    for (indice = 0; indice < MaxA + MaxB; indice++) printf("\n Os valores das matrizes A e B sao: %d \n", matrizc[indice]);
}

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

I created #define because the numbers will be used sometimes, so it’s easier to change. For example, I did it with low numbers so I don’t have to keep typing too much, but what the statement asks for is another amount. In your code you use other numbers. Ok, but you know why you’re doing?

There are several ways to play the values of the first vectors to the third. The chosen way is smart. But you have to do it right. The biggest error I saw in the code was that the second vector was being filled in all wrong. The code tried to fill in the elements from 8 to 15. But if the vector has only 8 capacity elements, that is, if they only go from 0 to 7, it is trying to record where it should not. This happened because he tried to solve another problem the wrong way. Solve the loop (note that the problem was not in the vector but in the loop) in the traditional way and solve the vector problem otherwise.

So what I did was shift the vector index matrizc. I changed this value only. I used the size of the matriza as offset and let the index vary normal.

0

@user8470

When we’re trying to learn how to program we have a vision that everything we read will become code on the fly,it in parts to try to solve, in this way you minimize the difficulty and focus on a question on the part, I made an example of how I would elaborate this solution.

Basically if the problem is quite complex I would already take my paper and pen to draw, but I see that this is not the case, come on.

detail, this example is done in C++:

/* Make a program to read two matrices of vector type, where A has 20 elements and B has 30 elements. Construct a C matrix, this being the junction of the two other matrices. This way C should have the capacity to store 50 elements. Display matrix C. */

#include <iostream>
using namespace std;

/*
    Sabendo que são 3 matrizes e a 3 e a soma das duas primeiras
    eu ja consigo definir quanto sera cada uma.
*/

#define maxA 20
#define maxB 30


int main (){

    /*
        Declaro as matrizes com a quantidade de indices pre-determinada.
    */

    string A[maxA],B[maxB],C[maxA + maxB];

    /*
        Percorro todos os Indices da Matriz A inserindo os respectivos valores.
        Faço a Matriz C receber todos os valores da Matriz A.
    */

    for(int i = 0; i <  maxA;i++){
        cout << "Digite o  valor da matriz A: ";
        cin >> A[i];
        C[i] = A[i];
    }

    /*
        Percorro todos os Indices da Matriz B inserindo os respectivos valores.
        Faço a Matriz C receber todos os valores da Matriz B.
        Detalhe, aqui eu sei que o indice da Matriz C deve começar depois de todos
        os valores da Matriz A, faça um teste removendo o "maxA" da Matriz C, se você
        fizer isso, você estara substituindo os valores que você tinha acabado de colocar na etapa anterior.
    */

    for(int i = 0; i < maxB;i++){
        cout << "Digite o  valor da matriz B: ";
        cin >> B[i];
        C[maxA+i]  = B[i];
    }

    /*
        Aqui eu já tenho toda Matriz C montada com os valores da Matriz A e Matriz B,
        agora basta percorrer a Matriz C e mostrar os valores que ela possui.
    */

    for(int i = 0; i < maxA + maxB;i++){
        cout << "Os valores da matrix C são: " <<  C[i] << endl;
    }

    return 0;
}

0

Your answer is not wrong you can continue programming!! Below is your code I just entered some instructions to print the index so you can follow the number of typed elements!! Your solution works very well for matrices with 10,20, 50 or 1000 elements, logic is the same! Congratulations!

int main(void){

int matriza[8], matrizb[8], matrizc[16], indice;

for(indice = 0; indice < 8; indice++)
    {
        printf("\n digite o  %d valor da matriz A: ",indice);
        scanf("%d",&matriza[indice]);

        matrizc[indice] = matriza[indice];
    }

for(indice = 8; indice < 16; indice++)
    {
        printf("\n digite o  %d valor da matriz B: ",indice);
        scanf("%d",&matrizb[indice]);

        matrizc[indice] = matrizb[indice];
    }

for(indice = 0; indice < 16; indice++)
        printf("\nValor %d Os valores das matrizes A e B sao: %d \n",indice ,matrizc[indice]);

    return 0;
}

Browser other questions tagged

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