Theman
Theman

Reputation: 231

Complicated multiple cases for switch statements

In my program I ask the user to enter in the size of an array, then fill the array with numbers entered by the user.

Then the user enters in a number which will check to see if it exists in the array. If it does, it will print a message to the console.

I want to be able to print different messages depending on the position of the number in the array.

If it's the 1st, 21st, 31st, etc... a different message will be printed

If it's the 2nd, 22nd, 32nd, etc... a different message will be printed

If it's the 3rd, 23rd, 33rd, etc... a different message will be printed

Then the default case is if it's one of the "th" numbers, a different message will be printed

Here is a segment of my code, it only works for the first 10 numbers. The problem I have is that 11th, 12th, 13th do not follow normal rules, as for any number that ends in 11, 12 or 13.

Console.Write("Now enter a number to compare: ");
int c = Convert.ToInt32(Console.ReadLine());
int pos = Array.IndexOf(arr, c);

if (pos > -1)
{
    switch (pos)
    {
        case 0:
            Console.WriteLine("{0} is the {1}st number in this list", c, pos + 1);
            break;

        case 1:
            Console.WriteLine("{0} is the {1}nd number in this list", c, pos + 1);
            break;

        case 2:
            Console.WriteLine("{0} is the {1}rd number in this list", c, pos + 1);
            break;

        default:
            Console.WriteLine("{0} is the {1}th number in this list", c, pos + 1);
            break;
    }

}
else
{
    Console.WriteLine("Sorry this number does not appear in the array");
}

I'd rather not enter it manually as eventually I will be using large sized arrays.

I want to either be able to use a wildcard (*) or a comma, but it won't let me. What's an easier way to solve this problem without entering every single number manually?

Upvotes: 0

Views: 371

Answers (2)

Jon
Jon

Reputation: 437376

Simply change switch (pos) to switch(pos % 10) and adjust your switch statement:

switch (pos % 10)
{
    case 1:
        Console.WriteLine("{0} is the {1}st number in this list", c, pos);
        break;

    case 2:
        Console.WriteLine("{0} is the {1}nd number in this list", c, pos);
        break;

    case 3:
        Console.WriteLine("{0} is the {1}rd number in this list", c, pos);
        break;

    default:
        Console.WriteLine("{0} is the {1}th number in this list", c, pos);
        break;
}

The modulo operator % will give you the remainder of the integer division pos / 10 -- that is, an integer always in the range [0, 9]. This is enough help to make the rest of your code almost work, except for the special cases 11, 12 and 13.

To handle these cases without messing with the switch cases themselves, you can expand the solution to

var position = (pos == 11 || pos == 12 || pos == 13) ? 999 : pos % 10;
switch (position)
{
    ...
}

The number 999 above is a completely arbitrary choice that is required to fall into the default case of the switch. I used 999 here to eliminate the possibility of miscommunication; you can select another number if you prefer.

Upvotes: 6

pawel
pawel

Reputation: 131

Use a modulo operator (number % 10).

Upvotes: 3

Related Questions