doubts interleaving vectors in C

0

Hello, I have a problem here with vectors I would like to understand what is going wrong and I am having difficulties, if anyone can help me about it.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int vet1[5], vet2[5], rvet[10];
    int i, j, k, n1=0, n2=0, aux=0;


    for(i=0; i<=4; i++)//Lê o vet1
    {

        printf("Digite para posicao %d do vet1:\n", n1++);
        scanf("%i", &vet1[i]);
    }

    printf("Obrigado, agora:\n");

    for(j=0; j<=4; j++)//Lê vet2
    {
        printf("Digite para posicao %d do vet1:\n", n2++);
        scanf("%d", &vet2[j]);

    }


    for(k=0;k<=9;k++)//Intercala os vetores
    {
        if(k%2=0)//separa valor de k em par
        {
            rvet[k]=vet1[i];
        }

        if(k%2!=0)//separa valor de k em impar
        {
            rvet[k]=vet2[j];
        }
    }

    for(k=0; k<=9; k++)
    {
        printf("vet[%d]:%d\n", k, rvet[k]);
    }

    return 0;
}
    
asked by anonymous 06.08.2014 / 04:38

2 answers

7

Friend , I found some errors in your code, some that make the algorithm invalid, other minor errors, and one that saves memory in the case of programming for embedded devices. probably not your case) or in gigantic programs. So let's see:

1 - Minor Errors:

In the second for , the printf() informs vet1 and not vet2 . It's not a code bug but it's not right. If you do not want to, you do not need to fix it, but you should.

2 - Memory Saving

There is no need to have a variable for each for , which in this case is i , j and k . This is because you always reset the variable used, such as for(i=0; i<=4; i++){ ... } .

Translating: you can use i in all for you are going to use, changing the required places. And this is only true if you need to value those variables later for some other task. Then you will create them - which is not the case.

I say this, because in embedded systems, every 'piece' of memory is important. And actions like this in the large application code - which you will probably develop or will help develop - accumulate and cause a horrendous expenditure of memory!

3 - Algorithm error (and syntax):

In the code section where you intersperse the values of the vectors, more precisely in the first if , there is a syntax error that causes an error in the algorithm; see:

    if(k%2=0)//separa valor de k em par
    {
        rvet[k]=vet1[i];
    }

When you want to compare two values in an equality, you must use the == sign. The = sign will be used for assignment.

To avoid these errors in the future, use spaces wherever possible and allowed, such as if(k % 2 == 0) . Helps you better visualize the code. Better still use if( (k % 2) == 0 ) .

4 - Extras

  • Still about value collation: you do not have to use two if in this algorithm. Just use else and the reason is simple: if a number is not even, it can only be odd! So you can change if(k%2!=0) by else .
  • When placing your question in Stack Overflow or elsewhere, always try to report error messages and post-compilation warnings. They help you understand the error. And over time you will realize that they are great allies of programmers!
06.08.2014 / 05:28
3

You have some errors in your code, try something like this:

i = j = 0;
for(k= 0; k < 10; k++)//Intercala os vetores
  rvet[k] = k % 2 == 0 ? vet1[i++] : vet2[j++];
    
06.08.2014 / 04:45