2

As newbie to C# I try to reverse an array with a method. But it returns not the reversed array. I tried for hours but still don't get it what I am doing wrong. The whole thing is a lesson from "The C# Players guide - 2nd edition" book I have bought. Unfortunately the solutions of the lessons are not included in the book. The Main method ist predefined and should be used like you can see.

If I do the output within the for loop it is 54321 and this would be fine. But the correct output should come from the PrintNumbers method. And there is the problem because the output 12345 is not reversed

Here is the code, please tell me if you see something bad.

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

        Console.ReadKey();
    }

    //Generate the array and return the numbers
    static int[] GenerateNumbers()
    {
        int[] temp;
        temp = new int[] { 1, 2, 3, 4, 5 };
        return temp;
    }

    //Reverse the numbers in the array
    static int[] Reverse(int[] numbersToSwap)
    {
        //Write array length in value to decrement later
        int numbersLenghtMax = numbersToSwap.Length;
        //Minus 1 because an array starts with 0
        numbersLenghtMax--;

        //Create a temp value to store last value of array
        int[] temp = new int[numbersToSwap.Length];

        for (int index = 0; index < numbersToSwap.Length; index++)
        {
            //Store values reversed in the temp value
            temp[index] = numbersToSwap[numbersLenghtMax];
            numbersLenghtMax--;

            //This output is correct
            Console.Write(temp[index]);
        }
        Console.WriteLine();
        //The returned array is not reversed
        return temp;

    }

    //Print out the numbers
    static void PrintNumbers(int[] numbers)
    {
        foreach (int number in numbers)
            Console.Write(number);
    }
}
3
  • 3
    You are not using the return value of the Reverse method. The argument numbers/numbersToSwap is not modified. Commented Jan 11, 2016 at 19:30
  • You could use System.Linq or Array.Reverse() to reverse your array Commented Jan 11, 2016 at 19:38
  • Thanks, I have read about the Array.Reverse() but the lesson is to do it without. Because it is about learning how functions work. Commented Jan 11, 2016 at 19:47

4 Answers 4

6

Your Reverse method returns an int[] (the result of reversing the array), but you are not using such return value.

Use it like this:

int[] numbers = GenerateNumbers();
numbers = Reverse(numbers); //Assign the return value to the numbers variable
PrintNumbers(numbers);

Console.ReadKey();
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks that solved the problem. I thought the function will always return back to the value numbers.
4

It's returning the value:

static int[] Reverse(int[] numbersToSwap)
{
    // other code

   return temp;
}

You're just not doing anything with the returned value:

Reverse(numbers);

You have to assign the returned value to something:

numbers = Reverse(numbers);

1 Comment

I am so blind. Thanks that make sense. I thought the return temp will be renamed to the arguments numbers and the main method don't know the temp value.
2

Your function is returning the reversed array, however you are not using it.

The function returns a new variable (temp) that contains your reversed array, you need to store this return value somewhere.

int[] numbers = GenerateNumbers();
Reverse(numbers);  // You pass in the numbers array & are returned a reversed version of it
PrintNumbers(numbers); // You print the original unmodified numbers array

Your Reverse function does not modify the numbers array at all. Instead it returns a new variable which represents what was stored in temp at the end of the function.

You would want to update your code to use the return value, like this:

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

2 Comments

So the predefined main code is not correct? Or can I store the value in the Reverse function to the numbers array?
You can store the return value of the Reverse function in the numbers array. Just keep in mind that by doing so you will lose the original values and have no way to recover them.
-1

Declare and set array element values:

int[] array = new int[] { 1, 3, 5, 7, 9 };

Reverse elements of array:

Array.Reverse(array);

See: https://msdn.microsoft.com/en-us/library/d3877932(v=vs.110).aspx

4 Comments

The question is not how to reverse an array, but why the specific code isn't working.
and my response says without saying it, the code is already written for this functionality. why rebuild what is already inherent in C#. the whole point of the 13 years of evolution of the .NET Framwork is to provide functionality that does not need to be rebuilt over and over again.
and to punctuate my comment, the accepted answer is in other words, exactly what I posted.
The point is that the OP is learning. When learning you often do things that there are already libraries for. Also the accepted answer does not use Array.Reverse, it shows the OP that they need to assign the results of their Reverse method to the original variable.

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.