3

I'm following along in a book and trying the 'challenges'. Here I'm running into a problem with properly returning and passing an array between methods. Something goes wrong with returning the array, especially from the second method, and then passing it to third to print.

I understand the stack, heap, values and references on the conceptual level, but something clearly goes wrong. Each method appears to work otherwise.

using System;

namespace PracticeArray
{
    class Program
    {
        static int[] GenerateNumbers()
        {
            Console.WriteLine("Enter a number to generate array from:");
            string input = Console.ReadLine();
            int inputNumber = Convert.ToInt32(input);

            int[] generatedArray = new int[inputNumber];
            for (int i = 0; i < generatedArray.Length; i++)
            {
                generatedArray[i] = i;
            }

            return generatedArray;
        }

        static int[] Reverse(int[] arrayToReverse)
        {
            int count = arrayToReverse.Length;

            int[] arrayToReturn = new int[count];

            count--;

            for (int i = 0; i < arrayToReturn.Length; i++)
            {
                arrayToReturn[i] = arrayToReverse[count--];

            }

           return arrayToReturn;
        }

        static void PrintNumbers(int[] numbersToPrint)
        {

            foreach (int singleNumber in numbersToPrint)
            {
                Console.Write(singleNumber + ", ");
            }
        }

        static void Main(string[] args)
        {

            int[] numbers = GenerateNumbers();
            Reverse(numbers);
            PrintNumbers(numbers);

        }
    }
}
4
  • 3
    return does not overwrite the input parameter. Try int[] newNumbers = Reverse(numbers); PrintNumbers(newNumbers); Commented Mar 5, 2020 at 17:57
  • 2
    Hi! you just need to assign back to the numbers variable! numbers = Reverse(numbers); Commented Mar 5, 2020 at 17:57
  • 2
    With Reverse(numbers);, you're doing the equivalent of dropping the new array on the floor. You need to receive it somehow (e.g. assign it to a variable, or pass it directly into PrintNumbers). This has little to do with arrays and more to do with the basics of returning values from methods. Commented Mar 5, 2020 at 17:58
  • Why not use Array.Reverse ? Commented Mar 5, 2020 at 18:50

1 Answer 1

3

The problem

The array you return is a new array:

Take a look in your Reverse function. You create a new array called arrayToReturn and return that array. You also never modify the original input array arrayToReverse.

        static int[] Reverse(int[] arrayToReverse) // this input array is never modified
        {
            int count = arrayToReverse.Length;
            // ...
            arrayToReturn = new int[count]; // Here is the new array
            // ...
            return arrayToReturn; // you return the new array
        }

Now look at where you called Reverse you passed in some generated numbers, but you ignore the return value of the function so you never find out what the new array is. Remember we already established above that you don't modify the elements of the input array. So numbers will remain unchanged.

        static void Main(string[] args)
        {
            int[] numbers = GenerateNumbers();
            Reverse(numbers); // you ignored the return value of `Reverse`
            PrintNumbers(numbers); // These are the original unreversed numbers
        }

To Fix

Option #1 - New variable to store the result

To fix this, store the array returned from Reverse in a variable and print those numbers.

        static void Main(string[] args)
        {
            int[] numbers = GenerateNumbers();
            int[] reversedNumbers = Reverse(numbers);
            PrintNumbers(reversedNumbers);
        }

Option #2 - Reuse the same variable to store the result

In this option you simply discard the original array by taking the new reversed numbers array returned from Reverse and assigning it to the numbers variable. After that assignment, numbers will refer to the reversed array that was returned from Reverse. The original array is orphaned and forgotten - eventually garbage collected.

        static void Main(string[] args)
        {
            int[] numbers = GenerateNumbers();
            // repurpose the `numbers` variable to store the result.
            numbers = Reverse(numbers);
            PrintNumbers(numbers);
        }
Sign up to request clarification or add additional context in comments.

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.