3

I am trying to make a method to reverse an array, I don't know why it is not working?

When I said int[] arr = array, I want to do this so it will not be affected so I can use the elements for the second for loop and it should have elements {1,2,3,4,5,6} but when I use

for (int i=array.Length-1;i>array.Length/2;i--)
{
    array[i] = arr[array.Length - 1 - i];
}

In this case I have 6 elements so array.Length is 6 and since I started from array.Length-1 it must start from the last element and it must be array[5]=arr[6-1-5] which must be array[5]=arr[0] and arr[0] is 1 but I think it is getting it as 6, why?

Here is the complete code:

// ReverseArray method
static int [] ReverseArray(int [] array)
{
    int[] arr = array;
    for (int i=0; i<array.Length/2;i++)
    {
        array[i] = array[array.Length-1  - i];
    }
    for (int i=array.Length-1;i>array.Length/2;i--)
    {
        array[i] = arr[array.Length - 1 - i];
    }
    return array;
}

// Method for displaying elements of Array
static void DisplayArray(int [] array)
{
    int i;
    Console.Write("{");
    for (i = 0; i < array.Length-1; i++)
    {
        Console.Write(array[i] + ",");
    }
    Console.WriteLine(array[i] + "}");
}

static void Main(string[] args)
{
    int[] array = { 1, 2, 3, 4, 5 ,6};
    ReverseArray(array);
    DisplayArray(array);
    Console.ReadKey();
}
12
  • Might be an idea to tag the language? Looks like C# to me, but I'm not betting my job on it. Commented Jan 23, 2018 at 13:40
  • 2
    Try to run your code in a debugger and see what is happening on each line. Commented Jan 23, 2018 at 13:40
  • 8
    Hint: by the time you've made the first half of the array look like the second half in reverse order, how do you expect to get at the data that was in the first half? Commented Jan 23, 2018 at 13:42
  • 1
    I have to ask, are you aware there is a BCL function Array.Reverse()? Commented Jan 23, 2018 at 13:55
  • 1
    @NightOwl888 ah I know it exist but I just wanted to make my own Commented Jan 23, 2018 at 14:16

2 Answers 2

7

Your reversal approach is invalid: rather than swapping elements at indexes i and array.Length-1 - i, your code copies the tail portion of the array onto the initial one, effectively "folding" the array onto itself.

You need to remove the second loop, and replace the assignment with a swap: make a temporary variable, store array[i], copy array[array.Length-1 - i], then write the temporary in its place.

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

Comments

6

I couldn't help myself but start to write a piece of code trying to solve it.

    int[] arr = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    arr = Reverse(arr).ToArray();

Here is the shorter way to achieve that.

    public IEnumerable<int> Reverse(int[] arr)
    {
        for (int i = arr.Length-1; i >= 0; i--)
        {
            yield return arr[i];
        }
    }

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.