Hello i'm trying for about 2 hours to create a program which will remove even numbers from a dinamyc allocated array(with malloc)in c.Can somebody help me with some tips or create the code.
p.s. this is my first topic here, so feel free to give me some tips about how to correctly post a qustion.
Let's assume that you already allocated dynamically an array of n elements and initialized it.
In this case the function that removes elements with even values can look the following way
It can be called the following way
where p is the pointer to your dynamically allocated array of n elements.
The function actually removes nothing. It simply moves odd elements to the beginning of the array.
You can then use standard C function
realloc
to delete physically the removed elements.For example
Here is a demonstrative program
Its output is
There are some things which you need to check before you try to code something, but I see that there is no code which you showed use.
SO is not a tutorial site, so this means that you should show us some code which actually does compile and ask here if there are some problems with that code.
Any way until than, this code should give you an Idea about how to check if a Number is
odd
oreven
:The whole story here is not about checking if Numbers inside an Array are
odd
oreven
, is about to find a way to check if a number isodd
oreven
and only then you should check if inside that array there areodd
oreven
numbers. I hope you understand my point.