0

Suppose we are given an array a[10] such that it contains both even and odd numbers. We have to move the even elements to another array say b[10] and odd elements(numbers) to array c[10]. the numbers in array are to be entered by the user which means we do not know the exact number of even and odd elements present in a. Let us assume that there are 5 even numbers and 5 odd numbers but the size of array b and c is 10. what should we do so that they don''t print garbage value

3
Contributors
2
Replies
17
Views
2 Years
Discussion Span
Last Post by rubberman
0

Set the last element plus one to a value that won't be entered. For instance, if the values will all be positive, -1 will work. When you loop through the values to display them, exit the loop when you reach this value.

0

Also, use instances of the std::vector class, and use the push_back() method to put the data into the correct vector (odd vs. even). Then, when done with the input, you can use a loop with an iterator to output the data from each vector.

If you know the size of the array in advance, then you can use std::array, but the std::vector class is better for dynamically sized arrays. It will resize as necessary as you add members.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.