1

I've written a console application (.NET 5.0) in C# in Visual Studio which prints out all the even and odd numbers you give as input.

It works as intended but there is a problem that I will encounter in the future when making similar applications.

Whenever a number isn't odd the respective place of that array (Number_odd) will have a zero added to it. How do I stop that from happening? I currently filtered out all the zero's by not printing any zero.

The output currently looks like "odd: 5 9 7 1 3" with all the zero's filtered. Without filtering the output looks like "odd: 0 5 9 7 1 3 0 0 0"

If I (for instance) say that "0" is an odd number I cannot print it because it gets filtered. How do I fix that?

using System; namespace ConsoleApp1 { class Program { static void Main(string[] args) { int[] numbers = Array.ConvertAll(Console.ReadLine().Split(" "), Convert.ToInt32); int uitkomst = 0; int[] numbers_odd = new int[numbers.Length]; int[] numbers_even = new int[numbers.Length]; for (int repeat = 0; repeat < numbers.Length; repeat++) { uitkomst = numbers[repeat] % 2; //Console.WriteLine(uitkomst); if(uitkomst == 1) // ODD { numbers_odd[repeat] = numbers[repeat]; //Console.WriteLine(numbers_odd[repeat]); } if (uitkomst == 0) // Even { numbers_even[repeat] = numbers[repeat]; //Console.WriteLine(numbers_even[repeat]); } } Console.Write("even: "); foreach (int item in numbers_even) { if(item == 0) { } else { Console.Write(item + " "); } } Console.WriteLine(" "); Console.Write("odd: "); foreach (int item in numbers_odd) { if (item == 0) { } else { Console.Write(item + " "); } } } } } 
2
  • 1
    First recommendation: Use lists instead of arrays, that way you can add items to it without worrying about how many elements you need in the list. Then only add the numbers you need, and ignore all the zeros. Commented Nov 10, 2021 at 13:26
  • an int [] is initialized with zeros. You actually only sets some indexes, not all, that's why you have zeroes at some places Commented Nov 10, 2021 at 13:29

3 Answers 3

4

Make these two lines

int[] numbers_odd = new int[numbers.Length]; int[] numbers_even = new int[numbers.Length]; 

use List<int> instead

var numbers_odd = new List<int>(); var numbers_even = new List<int>(); 

and then Add values to the correct one

if(uitkomst == 1) // ODD { numbers_odd.Add(numbers[repeat]); //Console.WriteLine(numbers_odd[repeat]); } if (uitkomst == 0) // Even { numbers_even.Add(numbers[repeat]); //Console.WriteLine(numbers_even[repeat]); } 

Now your lists only contain the odd or even numbers and no zeros

Console.Write("even: "); foreach (int item in numbers_even) { Console.Write(item + " "); } 
Sign up to request clarification or add additional context in comments.

5 Comments

Isn't a list<int> also a fixed array under the hood?
@DurgaPrasad yes, with the advantage that it will grow itself as needed saving you from having to do so manually.
Agreed. My concern was if OP uses list as mentioned in answer and iterates over capacity instead of count with a for loop isn't the problem still present?
@DurgaPrasad no, a for loop will iterate the actual items of the list not its underlying array.
2

It isn't that a zero is being added; an array is a fixed size, and is initialized with all default values - zero in this case. You then loop over numbers.Length and only write to one of the two arrays - leaving the other array zero at that position.

Perhaps consider using a list instead of an array for the two parts, and .Add(...) as you choose.

1 Comment

Thanks, I don't know what lists are but I will look into it. Thank you all for your answers!
1

The answer posted is already a good solution. Just wanted to add that, If you are learning C# and working with collections (arrays, lists etc.) consider Linq as one of useful tools.

Here is how you can leverage them in your example.

var numbers = Array.ConvertAll(Console.ReadLine().Split(" "), Convert.ToInt32); var numbers_odd = numbers.Where(n => (n % 2)==1); var numbers_even = numbers.Where(n =>(n % 2) == 0); 

1 Comment

Im currently learning c#. so im trying to find multiple possibilities but thank you for the recommendation :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.