Why does the program expect me to return array[c] in the below code when c is the length of array minus 1? I can't seem to follow how my program still manages to execute just well.
P.S I arrived at the last line of code by Trial and Error and it worked as expected. I just don't know how!
public static int arrayReverse(int[] array)
{
int c = array.Length - 1, b;
for (int i = 0; i < (array.Length / 2); i++)
{
b = array[i];
array[i] = array[c];
array[c] = b;
c--;
}
return array[c];
}
My main function looks like this:
static void Main(string[] args)
{
int[] array = new int[] { 1, 5, 3, 4, 2 };
//Reverse an Array
arrayReverse(array);
//Display Array Elements
foreach (int x in array)
Console.WriteLine(x);
Console.ReadLine();
}
int[] array = new int[] { 1, 5, 3, 4, 2 }; Array.Reverse(array);