Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Remove zero's (empty spaces) from arrays

Ive written a console application (.NET 5.0) in c# in visual studio. Wich 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 happing? I currently filtered out all the zero’s by not printing any zero.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 + " ");
                }
            }

        }
    }
}

>Solution :

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 + " ");
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading