0

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();
        }
3
  • the return value has no use. int[] is reference type and can be modified without return. Commented Apr 21, 2017 at 6:33
  • 2
    why reinventing the wheel? int[] array = new int[] { 1, 5, 3, 4, 2 }; Array.Reverse(array); Commented Apr 21, 2017 at 6:36
  • Didn't want to use inbuilt functions Commented Apr 21, 2017 at 6:41

1 Answer 1

3

Because you are returning an int not an int[]. Here, try this out:

 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;
 }

Also, you are not assigning back the returned array in main

array = arrayReverse(array);

Now, you do NOT need to return the array at all, since you are writing elements in place and not allocating new array.

Here, please find the code for In-Place and new array implementations. Debug it - write code on paper try to analyze what is happening.

static void Main(string[] args)
{
    try
    {
        int[] array = new int[] { 1, 5, 3, 4, 2 };

        //Reverse copy of Array
        var newarr = arrayReverseNewArray(array);

        //Display New Array Elements
        foreach (int x in newarr)
            Console.Write(x + ",");

        Console.WriteLine("");

        //Reverse original Array
        arrayReverseinPlace(array);

        //Display original Array Elements
        foreach (int x in array)
            Console.Write(x + ",");
        Console.ReadLine();

    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}


private static void arrayReverseinPlace(int[] array)
{
    for (int i = 0; i < array.Length / 2; i++)
    {
        int tempvar = array[i];
        array[i] = array[array.Length - i - 1];
        array[array.Length - i - 1] = tempvar;
    }
}

private static int[] arrayReverseNewArray(int[] array)
{
    int[] arr = new int[array.Length];
    int j = 0;
    for (int i = array.Length - 1; i >=0 ; i--)
    {
        arr[j] = array[i];
        j++;
    }
    return arr;
}

Here is the Running Sample Code.

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

10 Comments

He also ignores the returned value when calling the function. Probably he needs to read a good tutorial on C#.
Thanks! I am wondering why array[c] is okay, but not array[array.length] or array[] ?
array[array.length] should work if you return int.array[] is a declaration
@UweKeim Sorry about that, I am a novice. Learning by practising. I agree to you that I still need a lot of reading to do. Answering to your question, I made changes to the array directly in the function. I could have just ignored the return type and made the function call as well.
@Sadique Well, it gave me a System.IndexOutOfRangeException: 'Index was outside the bounds of the array.' during runtime. I reverted back to ` public static int arrayReverse(int[] array)` because with return type as int[], it does not allow me to return array[array.length] (it then tells me: cannot implicitly convert type int to int[]
|

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.