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!
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?)
– Guilherme Bernal
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.
– bfavaretto