0

I am trying to make a simple program in C# code (Console Application), which prompts the user to enter 10 names and then the 10 names displayed at the end in a random order (not the order the names were entered in)

This is how far I have gotten:-

static void Main(string[] args)
{
    string[] names = new string [10];
    int i;

    for ( i = 0; i < 10 ; i++)
    {
        Console.WriteLine("Give me name " + (i+1) );
        names[i] = Console.ReadLine();
    }

    for (int j = 0; j <= 10; j++)
    {
        Console.WriteLine(names[j]);
    }

    Console.ReadLine();
}

So far I have been able to prompt the user for 10 names, and store those 10 names, and display them at the end, however, once they are displayed I get the error :- "IndexOutOfRangeException was unhandled" and the Console.WriteLine(names[j]); gets highlighted.

Lastly, once these problems are sorted, how do I display the names entered back in random order?

Thanks for reading.

crashmstr
  • 28,043
  • 9
  • 61
  • 79
  • 5
    You're getting out of range exception because of `<=10`, should be `<10` in your second loop. – Arian Motamedi Sep 29 '15 at 19:13
  • @PoweredByOrange, thank you so much –  Sep 29 '15 at 19:15
  • Related: [Randomize a List in C#](http://stackoverflow.com/questions/273313/randomize-a-listt-in-c-sharp) – crashmstr Sep 29 '15 at 19:16
  • Testing your application in debug mode will raise the exception on the line on which it occurred and also gives you the ability to view local variables and their values. This would show you the array of length 10 (indexes 0-9) but j looped up to 10 causing the IndexOutOfRangeException. – Adam Milecki Sep 29 '15 at 19:19

4 Answers4

1

You get from 0 to 9 from input, but try to print from 0 to 10. The 10th item does not exist in the array. Correct it like below:

for (int j = 0; j < 10; j++)
{
    Console.WriteLine(names[j]);
}

Hope this helps.

Adam Milecki
  • 1,738
  • 10
  • 15
Mike Anderson
  • 738
  • 1
  • 8
  • 23
1

The first loop runs from 0 to 9, but the second runs from 0 to 10, so you try to display an item that doesn't exist in the array. Change j <= 10 to j < 10 in the loop (just like in the first loop) to loop to 9 instead of 10.

Better yet, you can use i < names.Length and j < names.Length in the loops. That way you can change the size of the array and the loops will still work without any change.


To display the items in random order you would want to shuffle the array. The best method for that is a Fisher-Yates shuffle. Shuffle the items in the array like this:

Random rnd = new Random();
for (int i = 0; i < names.Length − 1; i++) {
   int j = rnd.Next(i, names.Length);
   string tmp = names[i];
   names[i] = names[j];
   names[j] = tmp;
}

Then you can just show the items from the array the way that you do now (with the correction in the loop).

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
0

The problem is that your index is 0 based, and contains 10 entries, but the last one is Array[9] not Array[10]

{Frank,Paul,John}

So Array[0] is Frank, and Array[3] is... out of range.

Your loop uses

 for (int j = 0; j <= 10; j++)
{
    Console.WriteLine(names[j]);
}

The first time through, j = 0, names[j] would be Frank for us. Your problem is since 10 <= 10 is indeed true, it is looking for j[10], which doesn't exist.

The solution is to change j < 10

   for (int j = 0; j < 10; j++)
   {
       Console.WriteLine(names[j]);
   }
DidIReallyWriteThat
  • 1,033
  • 1
  • 10
  • 39
0

To resolve error, only check for less than 10 (<10) instead of less than or equal to 10 (<=10) and to get random order use Random class. Make sure only one random instance is created and you call Next so that new random number is generated in tight loop

Random random = new Random();
for (int j = 0; j < 10; j++)
 {
    int randomNumber = random.Next(1,10);
 Console.WriteLine(names[randomNumber-1]);
 }
Viru
  • 2,228
  • 2
  • 17
  • 28