Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The goal of the program is to display the 3 smallest numbers from a list supplied from the user.

The problem I am facing:
On execution, when I enter a comma-separated list e.g. 5,1,9,2,10. I get back 1, 10 and 2. Instead of getting back 1, 2 and 5.

What I have tried:

public void MinimumThree()
        {
            var min = 100;
            Console.WriteLine("Enter a list of numbers (seperated by ,): ");

            string input = Console.ReadLine();

            string errorMessage = "Invalid List. Try Again: ";

            bool loopContinue = true;

            while (loopContinue == true)
            {
                string[] list = input.Split(',');
                if (input == " " || list.Count() < 5)
                {
                    Console.Write(errorMessage);
                    input = Console.ReadLine();
                }
                else
                {
                    loopContinue = false;
                    Console.WriteLine();
                    
                    // Convert list elements to int
                    for (var i = 0; i < list.Length; i++)
                    {
                        int.Parse(list[i]);
                    }

                    foreach (var x in list)
                    {
                        Console.WriteLine(x);
                    }

                    // Sort list
                    Array.Sort(list);

                    // Print 3 smallest 
                    Console.WriteLine("Three smallest numbers: ");
                    for (var i = 0; i < 3; i++)
                    {
                        Console.WriteLine(list[i]);
                    }
                }
            }
        }
    }
Posted
Updated 4-Dec-17 10:03am

Im pretty sure int.Parse is a function and not a sub. You might want to go for int.TryParse instead, to check that your input actually is a int.

Looks like you sort sees 10 as 1 and sorts it accordingly to its char value, like the dictionary.

Try this:
string aa = "1";
string bb = "10";
string cc = "5";

int[] IntList = new int[3];
IntList[0] = int.Parse(aa);
IntList[1] = int.Parse(bb);
IntList[2] = int.Parse(cc);

Array.Sort(IntList);
 
Share this answer
 
v2
You're getting results sorted as character strings.

When you enter values from the console, they are of type char, not type int.
You need to take your list and convert it's members to an array of int values which need to be stored in their own array and sorted - or - create an alternative sort function for the sort that internally compares them as int values.

 
Share this answer
 
This
C++
int.Parse(list[i]);

translates each string to an int, but what are you doing with the resulting ints ?

About the algorithm: in order to find the 3 smallest numbers, you don't need to store the whole list in an array and sort it.
You just need 3 variables or an array of size 3 and only store the 3 smallest numbers as you read numbers from the list.
If it looks too complicated to you, start with a program to get the smallest number, then build on it to get the 2 smallest numbers, and end with the 3 smallest numbers.
Removing the need to sort is a big time saver for large list.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900