I am creating an array that will take in 24 numbers and display them in a table. I have used "arrayname".Max(); to determine the highest number but I need to display the array slot with the highest number
e.g. hour 15 had the highest number so 15 will displayed in a message rather than the number assigned to 15.
My code is as follows:
public void busiest(int[] A)
{
int busy;
busy = A.Max(); //Displays the highest values in a given set i.e. an array
Console.WriteLine("\nThe busiest time of day was hour " + busy);
}
Could anyone say if i'm missing something simple to display the slot rather than the assigned number?
Thanks
That you need to call is Array.IndexOf
:
Array.IndexOf(A, A.Max());
For further info regarding this method, please have a look here.
Beware that if there are more than one elements in the array with the same value, the index of the first of them would be returned from this method. For instance if the maximum value is 10 and there are two elements one at position with index 2 and one at position with index 3, then this method would return the value of 2.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With