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!