Home > OS >  separate odd and even integers in separate arrays
separate odd and even integers in separate arrays

Time:11-05

arr1 is filled with elements but it only outputs 0 on both arr2 and arr3. Any solutions without extensions such as LINQ

int[] arr1 = new int[100];
int[] arr3 = new int[100];
int[] arr2 = new int[100];
int n, i;
int e = 0;
int o = 0;

Console.Write("\n\n");
Console.Write("Separate odd and even integers in separate arrays\n");
Console.Write("-------------------------------------------------");
Console.Write("\n\n");

Console.Write("Input number of elements in array: ");
n = Convert.ToInt32(Console.ReadLine());

Console.Write("Input {0} elements\n", n);
for(i = 0; i < n; i  )
{
    Console.Write("Element-{0}: ", i);
    arr1[i] = Convert.ToInt32(Console.ReadLine());
}

for(i = 0; i < n; i  )
{
    if ((arr1[i] % 2) == 0)
    {       
        Array.Copy(arr1, i, arr2, e, 1);
        e  ;
    }
}

for (i = 0; i < n; i  )
{
    if ((arr1[i] % 2) != 0)
    {       
        Array.Copy(arr1, i, arr3, o, 1);
        o  ;
    }
}

Console.Write("Even numbers in array: {0}\n", arr2[i]);
Console.Write("Odd numbers in array: {0}", arr3[i]);

the code outputs 0 for both odd and even arrays if someone could point out why that would be great

CodePudding user response:

How is this:

            int n, i;
            int e = 0;
            int o = 0;

            Console.Write("\n\n");
            Console.Write("Separate odd and even integers in separate arrays\n");
            Console.Write("-------------------------------------------------");
            Console.Write("\n\n");

            Console.Write("Input number of elements in array: ");
            n = Convert.ToInt32(Console.ReadLine());
            int[] arr1 = new int[n];
            Console.Write("Input {0} elements\n", n);
            for (i = 0; i < n; i  )
            {
                Console.Write("Element-{0}: ", i);
                arr1[i] = Convert.ToInt32(Console.ReadLine());
            }

            for (i = 0; i < n; i  )
            {
                if ((arr1[i] % 2) == 0)
                {
                    
                    e  ;
                }
            }

            for (i = 0; i < n; i  )
            {
                if ((arr1[i] % 2) != 0)
                {
                   
                    o  ;
                }
            }
            
            Console.Write("Even numbers in array: {0}\n", e.ToString());
            Console.Write("Odd numbers in array: {0}", o.ToString());
            Console.ReadLine();

Or if you need to display numbers:

        int n, i;
        int e = 0;
        int o = 0;
        List<int> one = new List<int>();
        List<int> two = new List<int>();

        Console.Write("\n\n");
        Console.Write("Separate odd and even integers in separate arrays\n");
        Console.Write("-------------------------------------------------");
        Console.Write("\n\n");

        Console.Write("Input number of elements in array: ");
        n = Convert.ToInt32(Console.ReadLine());
        int[] arr1 = new int[n];
        Console.Write("Input {0} elements\n", n);
        for (i = 0; i < n; i  )
        {
            Console.Write("Element-{0}: ", i);
            arr1[i] = Convert.ToInt32(Console.ReadLine());
        }

        for (i = 0; i < n; i  )
        {
            if ((arr1[i] % 2) == 0)
            {
                one.Add(arr1[i]);
                e  ;
            }
        }

        for (i = 0; i < n; i  )
        {
            if ((arr1[i] % 2) != 0)
            {
                two.Add(arr1[i]);
                o  ;
            }
        }
        
        Console.Write("Even numbers in array: {0}\n", string.Join<int>(",", one));
        Console.Write("Odd numbers in array: {0}", string.Join<int>(",", two));
        Console.ReadLine();
enter code here

Also you should replace your \n with Environment.NewLine. And really it is built in with the Console.WriteLine anyways.

CodePudding user response:

When you check the value of i, it is greeater that length of arr2 and arr3 But to display values of arr2 and arr3:

for (i = 0; i < arr2.lenth; i  )
{
    Console.Write("Even numbers in array: {0}\n", arr2[i]);
}

for (i = 0; i < arr3.lenth; i  )
{
    Console.Write("Even numbers in array: {0}\n", arr2[i]);
}
  •  Tags:  
  • c#
  • Related