I wrote a code which swap the first and last value of my array. And I got it to work but for some reason it doesn't display the original value of the array. It shows only the swapped values. I want it to show the original values and at the bottom the swap values. What did I do wrong? please keep it simple since I am still new to coding thanks.
static void Main(string[] args)
{
int[] A = { 3, -12, 6, 9, -7, -13, 19, 35, -8, -11, 15, 27,-1 };
Console.WriteLine("\n=====================\n");
Console.WriteLine("Swapping first and last element");
SwapFirstAndLast(A);
DisplayArray(A);
//pause
Console.ReadLine();
}
static void SwapFirstAndLast(int[] array)
{
int temp = array[0];
array[0] = array[array.Length -1];
array[array.Length - 1] =temp;
}
//method to display array
static void DisplayArray(int[] array)
{
Console.WriteLine("\n===========================\n");
for (int i = 0; i < array.Length; i++)
{
Console.Write("{0} ",array[i]);
}
Console.WriteLine("\n===========================\n");
}
DisplayArrayafter swapping - did you intend to call it beforehand as well? Just addDisplayArray(A);between your first twoConsole.WriteLinecalls.