0

To start this is a homework assignment and I'm having a bit of trouble and would like some suggestions. So far I've created this application to take 10 inputted values from a user and store them into an array. I pass the array to the SmallAndLarge method where it displays derermines the smallest and largest of the 10 values using Sort() but now i have to display the smallest and largest of the 10 values the user entered and am having a trouble. Any help at all would be great!!

Also one other problem ive noticed that if the values 1 through 10 are entered 10 will be before 2 and after one when the array is sorted and displayed. Why is this?

namespace SmallAndLarge
{
    class Program
    {
        static void Main()
        {
            int found = 0;
            string[] numbers = new string[10];
            for (int i = 0; i < numbers.Length; i++)
            {
                Console.Write("Enter ten numbers --> ");
                numbers[i] = Console.ReadLine();
            }
            SmallestAndLargest(numbers);



        }
        public static int SmallestAndLargest(string[] numbers1)
        {

            int x;
            Array.Sort(numbers1);
            for (x = 0; x < numbers1.Length; ++x)
                Console.Write("{0} ", numbers1[x]);
            return x;
        }

    }
}
1
  • 6
    Many of your problems come from you using an array of strings rather than an array of integers. In string ordering 10 comes before 2 since it orders based on the first char. Commented Mar 15, 2013 at 21:05

4 Answers 4

2

You can use Linq to cast and return the Largest/Smallest using Min/Max

string[] numbers = new string[] {"1","2","3","15","30","7" };

var converted = numbers.Select(int.Parse);
int largest = converted.Max();
int smallest = converted.Min();
Sign up to request clarification or add additional context in comments.

3 Comments

Oh god, the guy barely knows how to code, don't throw Linq at him :P
yeah, this is much better :) I just write it, because people like to learn to wrong habbits, and a ctrl+c and ctrl+v is not well teacher when somebody don't understand an example :)
Also if this is one his homework assignment I seriously doubt that his teachers will expect him to use Linq... probably they are more interested in the logic behind finding the Min and Max. Is like an interview question, you can't go and tell the interviewer... ohh I would sort this array by calling .Sort()
1

You have the numbers as strings. That explains why "10" comes before "2", it's for the same reason that "am" comes before "i" in an alphabetic sort. The first character of "10" is '1' which comes before '2'.

So before you sort the array, or search for the max og min value, you need to convert the strings into some numeric type. It could be the int type, for example. You would hold them in an array of type int[], not string[].

To convert from the string given by Console.ReadLine into int, use an overload of int.Parse or int.TryParse methods, for example.

If the user should be allowed to type non-integral numbers like 3.14, you could use decimal instead of int.

Comments

1

It looks like you are iterating over the array. I would do the following:

  1. Create two int variables (small and large, respectively)
  2. Set them both equal to the first element of the array
  3. Loop through the elements;
  4. For each element[i], (convert the element to int using int.Parse() or equivalent).
    if element[i] > large, large = element[i]; if element[i] < small, small = element[i]

There is no need for sort if you only need min and max. And I would store the ints into and int array rather than string array, make the conversion when the user inputs the value. That way, when an invalid value is input, your program fails right then.

1 Comment

you are right, sorting is n^2 (in better way nlogn), if he just want min and max, this is simple n.
0

Here's a very simple, though long winded answer. Add the values to a List and use Min/Max returns. Your already using a for loop to iterate through the list, no reason to do it twice.

    static void Main(string[] args)
    {
        string[] StringValues = new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10","Not Number" };
        List<int> ConvertedStrings = new List<int>();

        foreach (string S in StringValues)
        {
            ConvertedStrings = ParseStringToInt(S, ConvertedStrings);
        }
        Console.WriteLine();
        Console.WriteLine("Max Value: " + ConvertedStrings.Max().ToString());
        Console.WriteLine("Min Value: " + ConvertedStrings.Min().ToString());
        Console.ReadLine();
    }

    static private List<int> ParseStringToInt(string Input, List<int> ConvertedStrings)
    {
        int ConvertedValue = 0;

        if (int.TryParse(Input, out ConvertedValue))
        {
            Console.WriteLine(Input + " sucessfully parsed and added...");
            ConvertedStrings.Add(ConvertedValue);
            return ConvertedStrings;
        }
        else
        {
        Console.WriteLine(Input + " failed to parse...");
        }

        return ConvertedStrings;
    }

sa_ddam213 has got the killer answer though :P

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.