Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

odd/even numbers - sort array

Tags:

c#

Can anyone help. I have to write a program that lets the user input 10 numbers. And sort those numbers in odd(ascending order) and even(descending order).

for example: input: 2, 8, 7, 13, 23, 9, 34, 11, 1, 16 output: 1, 7, 9, 11, 13, 23, 34, 16, 8, 2

And I have to do this without any methods, such as Array.Sort.

This is my code so far:

Console.WriteLine("Input 10 numbers: ");
        int[] number = new int[10];
        for (int i = 0; i < number.Length; i++) {
            number[i] = int.Parse(Console.ReadLine());

            if (number[i] == 0) {
                Console.WriteLine("input of 0 is not alllowed!");
                break;
            }
        }

        //EVEN NUMBERS
        for (int i = 0; i < number.Length; i++) {
            if (number[i] % 2 == 0) {
                Console.Write("{0} ", number[i]);
            }
        }

        //ODD NUMBERS
        for (int i = 0; i < number.Length; i++) {
            if (number[i] % 2 != 0) {
                Console.Write("{0} ", number[i]);
            }
        }

I just need a general idea of how to do it.

like image 767
Alli Avatar asked Jun 24 '26 18:06

Alli


1 Answers

There are a variety of good sorting algorithms that you can implement, which I won't get into since that is somewhat off topic to your question, but here's a link to a page that describes many of them.

The key to solving your problem with any sorting algorithm will be having a good comparison function that can tell you what order the values should show up in. The important rules for that comparison in your case are: 1. Odd should always come before even 2. Lower Odd numbers should come before Higher Odd numbers 3. Higher Even numbers should come before Lower Even numbers

Therefore, your comparison function could look something like the following (note: I wouldn't name the function or parameters this way normally, but it makes it somewhat more clear for this example)

public bool AComesBeforeB(int a, int b) 
{
    if(IsOdd(a))
    {
        if(IsOdd(b))
        {
            return a < b;
        }
        else
        {
            return true;
        }
    }
    else
    {
        if(isOdd(b))
        {
            return false;
        }
        else
        {
            return a > b;
        }
    }
}

Alternatively you could do it as yyttr3 has recommended, and split the even and odd numbers into different arrays and then sorting them from there. I prefer not to do it that way because it takes more code and more memory, but it works basically just as well.

A couple more notes not directly related to your question:

  1. Look into what happens if someone does enter a 0 (hint, it doesn't run correctly)

  2. Based on your code and your sample input you are assuming that the input will always be a series of integers, what happens if the user puts in a number with a decimal? Or an alphabetical string? (hint: an exception would crash your program)

  3. Make sure your program handles negative numbers, as it stands right now I don't see why it wouldn't, but that is always a good test when dealing with number inputs

  4. This looks like a homework assignment, and while it is fine to ask for advice and help on specific parts of a problem, don't try to use SO to do your homework for you, to be clear I don't think you're doing that, but Rariolu's answer is basically giving you the code wholesale, which could be considered plagerism if submitted as your own work

  5. When you say you want to do this "without any methods", that betrays a misunderstanding of what a method is. This code will inherently be running inside a method, and in order to write this code in a clear and understandable way you will want to write your own methods that you can call from the main method. What I think you really mean to say is "I have to do with without using an of the C# library methods that are normally used for sorting".

like image 164
Kevin Avatar answered Jun 27 '26 09:06

Kevin