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.
Did any help you more? You need something to be improved?
– Maniero