Only one seat should be allocated to one audience randomly. More than one audience can't have same seat or vice versa. How can this program be completed with these conditions?
using System;
class Program
{
public static void Main()
{
int seats = 10;
int audience = 10;
int[] audArray = new int[audience];
Random r = new Random();
Console.WriteLine("{0 ,4} {1, 5}", "Audience", "Seat#");
for (int i = 1; i < audience; i++)
{
int x = r.Next(1, seats);
audArray[i] = i;
Console.WriteLine("{0, 3} {1, 6}", audArray[i], x);
}
Console.Read();
}
}
Loading
VulpesPosted Oct 10, 2013, 3:37 PM
So try the following:
Posted Oct 11, 2013, 6:58 AM
VulpesPosted Oct 11, 2013, 6:16 AM
However, in later versions of .NET, the generic overload Array.IndexOf
Incidentally, the compiler uses complex rules to determine the best match but these usually result in the 'common-sense' overload being chosen.
You could still force the Array.Index(Array, object) overload to be chosen by changing the line to:
if(Array.IndexOf(audArray, (object)x) == -1)
but this would be less efficient that the generic alternative as the int would need to be boxed on the heap to be accommodated by the object parameter.
You could also call the generic overload with the syntax:
f(Array.IndexOf
but this is unnecessary as the compiler is able to infer the type of T from the types of the arguments.
Posted Oct 11, 2013, 5:57 AM
VulpesPosted Oct 10, 2013, 6:19 PM
http://msdn.microsoft.com/en-us/library/eha9t187.aspx
Notice that the overload we're using here is a generic method where T = int. The compiler infers the type of T from the fact that it's an int array and an int value that we're passing to it.
Posted Oct 10, 2013, 4:55 PM
Posted Oct 10, 2013, 3:48 PM