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 " ");
}
}
}
}
}
CodePudding user response:
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 " ");
}
CodePudding user response:
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.
CodePudding user response:
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);