user5379214
user5379214

Reputation:

Problems with displaying information from an array

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.

Upvotes: 0

Views: 85

Answers (4)

Guffa
Guffa

Reputation: 700322

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).

Upvotes: 1

DidIReallyWriteThat
DidIReallyWriteThat

Reputation: 1033

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]);
   }

Upvotes: 0

Viru
Viru

Reputation: 2246

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]);
 }

Upvotes: 0

Mike Anderson
Mike Anderson

Reputation: 768

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.

Upvotes: 1

Related Questions