-2

I have written a code. The size and values of the array are specified by the user. I need to display all even numbers in increasing order and all odd numbers in increasing order. But I can't use "if". How can I do this?

using System;

class Pol
{
    static void Main()
    {
        Console.WriteLine("Укажите размерность массива от 1 до 10");
        int size = int.Parse(Console.ReadLine());
        double[] a_massiv = new double[size];
        for (int i = 0; i < a_massiv.Length; i++)
        {
            Console.Write($"Введите {i} значение массива ");
            a_massiv[i] = double.Parse(Console.ReadLine());
        }
        foreach (var v in a_massiv)
        {
            Console.WriteLine(v);
        }
        foreach (var q in a_massiv)
        {
            ...
        }
        ...
    }
}
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
ilushk
  • 19
  • 3

3 Answers3

1

Approach 1

  1. Create an array of Lists say arr. The size of the array will be 2. The list at 0 index will store even numbers, whereas the list at 1 index will store odd numbers.

Initially, both lists will be empty.

  1. Iterate over numbers i.e. (a_massiv). and add them to corresponding lists.

that is: num will be added at arr[num%2].

So, if input is [1,2,3,4,5,6,7,8,9]

After populating the arr, arr will become:

[0]: [2,4,6,8]
[1]: [1,3,5,7,9]
  1. Now you can iterate over the arr[0] and print all numbers (which are even).

Approach 2

The loop contains the condition. So, if the loop is allowed, you can use the loop instead of condition.

The logic will be similar to this:

int i = 0;
while (i < size) {
  while (a_massive[i]%2 == 0 && i < size) {
     Console.Write(a_massive[i]);
     i++;
  }
  // skip odd
  i++;
}
1

Probably the type of a_massiv is int and not double. However, you can use Linq to select even and odd numbers(Enumerable.Where)

Console.WriteLine("Укажите размерность массива от 1 до 10");
int size = int.Parse(Console.ReadLine());
double[] a_massiv = new double[size];
for (int i = 0; i < a_massiv.Length; i++)
{
     Console.Write($"Введите {i} значение массива ");
     a_massiv[i] = double.Parse(Console.ReadLine());
}
List<double> oddNumbers = a_massiv.Where(x => x % 2 != 0);
Console.WriteLine("Odd numbers: " +String.Join(',', oddNumbers));

List<double> evenNumbers = a_massiv.Where(x => x % 2 == 0);
Console.WriteLine("Even numbers: " +String.Join(',', evenNumbers));
Hossein Sabziani
  • 1
  • 2
  • 15
  • 20
  • As a side note: No need to call `ToList` on the results of the `Where` - the `string.Join` method takes in an `IEnumerable` which is the type returned from the `Where` method. – Zohar Peled Mar 12 '23 at 17:00
1

Create an array of delegates. Delegates store the address of methods. Then assign two methods to this array: one printing, the other doing nothing. Then select the appropriate one by using the modulo operation.

Note that you must use integers, for this task. Not doubles possibly having a decimal part.

var print = new Action<string>[] { Console.WriteLine, s => { } };
var array = new int[] { 17, 18, 2, 5, 7, 4, 8, 9, 11 };
Array.Sort(array);
foreach (int i in array) {
    print[i % 2]($"{i} is even");
}
Console.WriteLine();
foreach (int i in array) {
    print[1 - i % 2]($"{i} is odd");
}

The lambda expression s => { } specifies an action accepting one string parameter and doing nothing. Note that we specify Console.WriteLine without the braces () to denote the method itself without calling it.

prints:

2 is even
4 is even
8 is even
18 is even

5 is odd
7 is odd
9 is odd
11 is odd
17 is odd

Note that the modulo operator returns the remainder of an integer division. So, e.g. 8 % 2 = 0 and 9 % 2 = 1.

If x is 0 or 1, then 1 - x is 1 or 0. I.e., it swaps the two values.

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188