0

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");
    }
2
  • 6
    You're only calling DisplayArray after swapping - did you intend to call it beforehand as well? Just add DisplayArray(A); between your first two Console.WriteLine calls. Commented Mar 3, 2014 at 18:11
  • 1
    Please, do not include information about a language used in a question title unless it wouldn't make sense without it. Tags serve this purpose. Commented Mar 3, 2014 at 18:55

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

"...and professional programmers" I don't agree. :)
The mistake is more apparent for some :p
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.