doubts intercalation of vectors in C

Asked

Viewed 3,362 times

0

hello, I have a problem here with vectors I would like to know what is going wrong and I am having difficulties, if anyone can help me on this.

#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;
}
  • 1

    Define what you mean by "it’s going wrong". That’s very vague. Doesn’t the code compile? (what’s the bug? ) The result isn’t what you expected? (what was the program’s response? what was expected? what was the input?)

  • Leonardo, the people on this site help because they want to. The least you can do is be polite to others. Asking "read my code and find out all the problems" is not a good idea.

2 answers

7


Friend, found some mistakes in its code, some make the algorithm invalid, other errors smaller and one that saves memory in the case of programming for embedded devices (which is 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 an error in the code but it’s not right. If you don’t want to, you don’t 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 are i, j and k. That’s because you always Zera the variable used, as in for(i=0; i<=4; i++){ ... }.

Translation: you can use i in all the for that you will use, changing the necessary locations. And this is only not true if you need the value of 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 large application code - which you probably will develop or help to develop - accumulate and cause horrific memory expenditure!

3 - Algorithm error (and syntax):

In the code section where you insert 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 on an equality, you must use the sign ==. Already the sign =, you will use for assignment.

To avoid these errors in the future, use spaces where possible and allowed, for example if(k % 2 == 0). Helps to better visualize the code. Even better would be using if( (k % 2) == 0 ).

4 - Extras

  • Still on the value intercalation: you don’t need to use two if in this algorithm. Just use a else and the reason is simple: if a number is not even, it can only be odd! So, you can exchange if(k%2!=0) for else.
  • When you put your doubt in Stack Overflow or anywhere else, always try to report error messages and post-build warnings. They help you understand the error. And in time you will realize that they are great allies of programmers!
  • Our certainty I send but even Warning had Mr. Joseph, thanks for the reply. I wanted to keep in touch if you have how.

  • I forgot to warn I had already changed the program and it is working I tried to use your tips however I did not agree but I will discuss with my teacher today.

  • 1

    Well, no compiler will generate warnings on how best to write code for easy maintenance. Good programming practices always require you to write a code that is easy to read by others and by you, of course. :)

3

There are some errors 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++];
  • Abriel I have a doubt, why the i=j=0, the rest I understood and it worked out thank you very much =)

  • 1

    Why will he use later in Operation Ternary (link). In this case, it needs to initially access the 0 position of each of the vectors - it is also a good short example of a program that uses i, j and k wisely. Note: this syntax i = j = 0 is not wrong but is not very indicated as good programming practice (where your code will be read by others). It is better to separate into different assignments.

Browser other questions tagged

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