Question

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");
    }
Was it helpful?

Solution

As Jon said, you need to call DisplayArray(A); before mutating int[] A = { 3, -12, 6, 9, -7, -13, 19, 35, -8, -11, 15, 27,-1 };.

Like this:

int[] A = { 3, -12, 6, 9, -7, -13, 19, 35, -8, -11, 15, 27,-1 };
Console.WriteLine("The array I want to change:");
DisplayArray(A);
Console.WriteLine("\n=====================\n");
Console.WriteLine("Swapping first and last element");
SwapFirstAndLast(A);
DisplayArray(A);
//pause
Console.ReadLine();

Common mistake for all beginner and professional programmers :). Next time, just step through the main method line by line and say to yourself what this particular line of code does. If it's inconsistent to what you want to do, then now you notice there is an issue :).

Alternatively you can use two arrays, say A which is your input array, assign A as B, and use SwapFirstAndLast(B), so you have both the mutated and non-mutated array for use.

OTHER TIPS

I recommend you a couple of improvements too:

static void Main(string[] args)
    {  
        int[] A = { 3, -12, 6, 9, -7, -13, 19, 35, -8, -11, 15, 27,-1 };    
        DisplayArray(A);
        Console.WriteLine("\n=====================\n");
        Console.WriteLine("Swapping first and last element");
        SwapFirstAndLast(A);
        DisplayArray(A);
        //pause
        Console.ReadLine();

    }

    static void SwapFirstAndLast(int[] array)
       {
           //Equal than yours
       }

    //method to display array
    static void DisplayArray(int[] array)
    {
        Console.WriteLine("\n===========================\n");
        Console.WriteLine(string.Join(",", array);
        Console.WriteLine("\n===========================\n");
    }

Console.WriteLine(string.Join(" ", array); will make that your output will be something like:

3, -12, 6, 9, -7, -13, 19, 35, -8, -11, 15, 27,-1

Then after swapping:

-1, -12, 6, 9, -7, -13, 19, 35, -8, -11, 15, 27, 3

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top