Problem with vector in C

0

I do not know where it is wrong, if I put the value 7 in the matrizA and 7 in the matrizB and 14 in the matrizC that will accumulate the value of A and B, of course but if I put above 14 of the error.

  

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

     

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

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]);
    
asked by anonymous 26.09.2015 / 04:33

3 answers

1

It seems to me that the problem is more in understanding the problem than in programming. I have not seen any errors in the program, but in what you want to do. Anyway, did you choose one of the most difficult languages to learn? I think it's great how the person starts by C, it means that she will learn the basics, will know what few know. But if you do not have a natural talent for the thing, you'll end up giving up because you have too many problems to solve at hand.

I've said it before and I'll say again that programming is detail. If you do not want to worry about them, actually scheduling will be difficult. Vector is easy. It seems like you're getting into other things.

The problem description 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]);
    return 0;
}

See running on ideone .

I created #define because the numbers will be used a few times, this way it is easier to change. For example, I did with low numbers so I did not have to be typing a lot, but what the statement asks for is another amount. In your code you use other numbers. Okay, but you know what you're doing?

There are several ways to throw values from the first vectors to the third. The way you choose is smart. But it has to do right. The biggest mistake I saw in the code was that the second vector was being filled all wrong. The code tried to fill in the elements from 8 to 15. But if the vector has only 8 elements of capacity, that is, if they only go from 0 to 7, it is trying to write where it should not. This was because he tried to solve another problem in 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 in another way.

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

    
26.09.2015 / 04:59
0

@ user8470

When we are trying to learn to program we have a view that everything we read will become code at the time, but this is not true, most of the codes you will see anywhere will have a part of trial and error, always try take the problem and break it into parts to try to solve, this way you minimize the difficulty and focus on one issue per part, I gave an example of how I would elaborate this solution.

Basically if the problem is quite complex I would already be getting my pen and paper 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 vector-type arrays,   being A with 20 elements and B with 30 elements.   Construct a matrix C, this being the junction of the two other matrices.   In this way C must 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;
}
    
26.09.2015 / 06:21
0

Your answer is not wrong, you can continue to program !! Below is your code I just inserted some instructions to print the index so you can keep track of the number of elements typed !! Your Solution works great for both arrays with 10,20, 50 or 1000 elements, the 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;
}
    
26.09.2015 / 13:22