0

I am writing a small console application in C# using Visual Studio.

I want to write two small programs, both will take six integers entered via the console and then the if statements will compare all six values and the end result will be console outputs printing the six values entered and the values in order from highest to the lowest. I am racking my brains trying to figure out how to do this.

Once I have done this I will write a second program to do this using a list which I think will be neater.

So I think there should be six integers to hold the entered values, then six integers to hold the ordered values, with if statements in between to work out the order of highest to lowest.

I tried to do the nested if's but it got messy I decided to scrap it and start again.

I would appreciate any help with this.

Here is the kind of code I was writing:

  int one;
        int two;
        int three;
        int four;
        int five;
        int six;


        int firsthighest;
        int secondhighest;
        int thirdhighest;
        int fourthhighest;
        int fifthhighest;
        int sixthhighest;



        Console.WriteLine("Enter a value");
        one = int.Parse(Console.ReadLine());

        Console.WriteLine("Enter a value");
        two = int.Parse(Console.ReadLine());

        Console.WriteLine("Enter a value");
        three = int.Parse(Console.ReadLine());

        Console.WriteLine("Enter a value");
        four = int.Parse(Console.ReadLine());

        Console.WriteLine("Enter a value");
        five = int.Parse(Console.ReadLine());

        Console.WriteLine("Enter a value");
        six = int.Parse(Console.ReadLine());



        if (one > two)
        {
            if (one > three)
            {
                if (one > four)
                {

                    if (one > five)
                    {

                        if (one > six)
                        {
                            firsthighest = one;

                        }

                    }

                }
            }

        }

        else if (two > three)
        {
            if (two > four)
            {

                if (two > five)
                {

                    if (two > six)
                    {
                        firsthighest = two;

                    }

                }

            }

        }

        else if (three > four)
        {


            if (three > five)
            {

                if (three > six)
                {
                    firsthighest = three;

                }

            }

        }
7
  • I didn't downvote, but what have you tried so far? Is this homework?
    – keyboardP
    Oct 9, 2012 at 16:34
  • @keyboardP does it matter if it is homework?
    – Magnus
    Oct 9, 2012 at 16:36
  • 3
    @Magnus - It's nice to acknowledge that it's homework, so hints can be given more than just a direct answer. SO isn't intended to answer homework questions, especially when no effort has been shown in the question.
    – keyboardP
    Oct 9, 2012 at 16:38
  • I promise it is not homework, I am trying to make a point to a friend about code examples, how two solutions can be done differently the second more efficient. I don't know why I would be downvoted for asking this?
    – deucalion0
    Oct 9, 2012 at 16:39
  • 1
    @deucalion0 - I didn't downvote but one reason might be because you didn't show any code you've already written. Only the downvoter knows the actual reason :)
    – keyboardP
    Oct 9, 2012 at 16:42

5 Answers 5

4

Is this homework?

I'd suggest skipping the if's and go right to your 2nd program - put all 6 in an array and sort it.

2
  • No homework I promise, I am just annoyed I couldn't do it just using if's, I felt someone here may want to show me. Yeah I knew I could do it in an array, I will be doing that next. Thanks
    – deucalion0
    Oct 9, 2012 at 16:41
  • 1
    I deleted my previous comment after seeing, how lengthy the program can become.
    – Ramesh
    Oct 9, 2012 at 16:41
2

You could use an array, and then use a sort algorithm on it, such as bubble sort.

2

You can use any of the Sort Algorithms. It doesn't matter whether you store this in six variables or in one array. If you are planning to use Enumerable.OrderBy, then it internally just implements one of this sorting algorithms.

2

I'm not sure how you would even plan on implementing a sorting algorithm with a set of nested if statements. Let's think about it for a minute. Let's assume I have six integers entered in this order:

8,3,6,1,9,10

Now, 8 is certainly greater than 3 so if I wrote the first set of if's like this:

if (one > two) { ... }

What does that really tell us? Only that one is greater than two, but it certainly doesn't tell us where in the list one should be yet. So, let's say I nested every possible scenario:

if (one > two) {
    if (one > three) {
        if (one > four) {
            if (one > five) {
                if (one > six) { ... }
            }
        }
    }
}

What will happen? What will this tell us? Well, it will tell us that five is greater than one ... but the problem is that six is greather than five.

This short evaluation should tell you this is not a good way of doing this, so if you're trying to prove this is a bad way of going about it, there you go.

Now, let's move on to a solution to the problem and let's put those integers into a list:

var list = new List<int> { one, two, three, four, five, six };
var result = list.OrderByDescending(x => x);

Internally that is a true sort algorithm that goes through every value in more than one pass comparing it to all other values. However, it's also much more optimized using real-time indexing to help keep iterations down.

4
  • @erikH, wouldn't list.Sort(); perform an ascending sort and so then you would have to read it backwards to write the values out in descending order? Oct 9, 2012 at 18:56
  • list.Sort((x1,x2) => x2.CompareTo(x1)); if it is hard to iterate backwards. I think it is better to sort the data in the existing list, than creating a new array that the linq command does.
    – erikH
    Oct 9, 2012 at 19:02
  • @erikH, so then you're really just stating an opinion surrounding your preferences, not that there is an issue with the solution, is that correct? Oct 9, 2012 at 19:05
  • Your solution is absolutely working, perhaps with the note var result = list.OrderByDescending(x=>x); since the linq command doesn't change list.
    – erikH
    Oct 9, 2012 at 19:16
1

One keyword to check is Sorting Network

For 6 inputs, one needs a minimum of 12 compare & swap operations with constant structure. The optimal structures for 1-8 elements are known. An example of 4-element optimal sorting network is:

 void f(int a, int b) { if (arr[a]>arr[b]) swap(arr,a,b); }
 f(1,2); f(3,4); f(1,3); f(2,4); f(2,3);

These structures do not nest well...

1
  • In fact in your code you are already having 12 comparisons, which should be enough to sort the array. Oct 10, 2012 at 7:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.