2

I have this as Main

int[] M ={ 10, 2, 30, 4, 50, 6, 7, 80 };

MyMath.Reverse(M);

for (int i = 0; i < M.Length; i++)

    Console.WriteLine(M[i].ToString() + ", ");

After I created the class MyMath I made the Reverse method

public int Reverse(Array M)
 {
     int len = M.Length;
       for (int i = 0; i < len / 2; i++) 
       {
           int temp = M[i]; M[i] = M[len - i - 1]; M[len - i - 1] = temp;

       }           
 }

but I'm sure it's wrong because it's not working :-) so do you have a different code to write in the reverse method?

note: I don't want to use the built in Reverse in the Array class


yes guys when i used the built in reverse method i got this error

Process is terminated due to StackOverflowException.

thats after i wrote the method as

public static int Reverse(Array M)
{
    return Reverse(M);
}

So then I tried to create my own reverse method and there i got stuck

7
  • Why can't you use the builtin method in this case? Also, if you gave an indication of how the code isn't working for you, people would be better able to help. As it sits, readers are likely to assume this is a homework question you want us to do for you. Nov 10, 2008 at 20:40
  • It would be very helpful if you elaborated "Does not work". Code can not work in a bazillion and two ways. Does it not compile, does it give wrong results (in this case, show us the test input, the actual output and the expectd output), does it explode or does it make demons fly out of your nose? Furhermore, from what I see from here, you will have fun with arrays with odd length [Edit or not?]
    – Tetha
    Nov 10, 2008 at 20:45
  • when i used the built in method it will show me an error that say process is terminated due to StackOverflowException
    – Eggs McLaren
    Nov 10, 2008 at 20:48
  • As much as I agree with your post, he won't have any problems with odd length arrays. The middle item will be a pivot. He isn't sorting it.
    – LeppyR64
    Nov 10, 2008 at 20:57
  • That's very surprising. Could you show us the code that resulted in the StackOverflowException? I just replicated the experiment using Array.Reverse, and it worked like a charm. Nov 10, 2008 at 20:57

4 Answers 4

4

Working from your

public static int Reverse(Array M)
{
    return Reverse(M);
}

You have 2 problems.

  1. Reverse(M) looks like the same function that you're in, so you're calling your new function, which calls itself, which calls itself, etc., resulting in the stack overflow. Change to return Array.Reverse(M);
  2. Array.Reverse returns a void, so if you need to return an int (not sure what it's supposed to be here) you'll need to supply your own. Or change your Reverse function to be void.
0
2

To fix your problem, change your method to:

// the built-in returns void, so that needed to be changed...
public static void Reverse(Array M)
{
    Array.Reverse(M); // you forgot to reference the Array class in yours
}

There, no Stack Overflow problems.

0

It makes next to no sense to not use Array.Reverse. But if you really want to do it, ONE option could be to duplicate the array into a Stack, although I do not believe that this is very fast (but I have not profiled it either):

private void Whatever()
{
    int[] M = { 10, 2, 30, 4, 50, 6, 7, 80 };
    ReverseArray(ref M);

}

private void ReverseArray(ref int[] input)
{
    Stack<int> tmp = new Stack<int>();
    foreach (int i in input)
    {
        tmp.Push(i);
    }
    input = tmp.ToArray();
}
5
  • If you like to do homework for people, I am sure you could make some money at it... ;) Nov 10, 2008 at 20:58
  • Like, becoming a Teacher? Well, I don't really care if it's a homework question or not, because maybe someone else can benefit from it. Maybe someone does not know about the Stack Class and gets a benefit? Or maybe someone has a better implementation so that I can learn a thing or two. Nov 10, 2008 at 21:02
  • Sorry, I wasn't trying to be a jerk, just some good-natured harassment. You have a good point about seeing beyond the OP's needs. My bad. Nov 10, 2008 at 21:04
  • I did not take it offensive, no worries :) I just remember how I started learning development. So far, I had to teach myself every language myself without formal training/education, and I asked myself way more "stupid" things than that, and thanks to one or two helpful pointers, I could improve. Nov 10, 2008 at 21:09
  • I believe like Micheal, homework or not it's a question. SO is about answering question, If you do not want to answer homework question, you aren't forced too ;) Nov 10, 2008 at 22:01
-3

You need to pass you array as a reference. In C#, you do that by using the keyword 'ref' when declaring your parameters.

What you are currently doing is basically reversing a copy of your array which is never passed back to the caller.

5
  • No - the array is passed by ref already, it is a reference type, thus the value of an array variable is a reference to it. The 'ref' keyword is for explicitly passing value types by reference; an int is a value type, but an array thereof is not. Nov 10, 2008 at 20:56
  • Are you sure about that? In my example below, removing the ref will cause the function NOT to modify M, i have to explicitly pass it as ref. Nov 10, 2008 at 20:59
  • Good to know. The lack of explicit reference was the only thing I could notice with what information I had
    – 3Doubloons
    Nov 10, 2008 at 20:59
  • @Michael - the difference in your case is that you are actually changing the reference and pointing it to a new array, not just modifying the members that are referenced.
    – LeppyR64
    Nov 10, 2008 at 21:01
  • @Jason - thanks for answering Michael's question, I didn't see it. Nov 10, 2008 at 21:12

Your Answer

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