tags:

views:

890

answers:

5

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

A: 

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.

Alex Brault
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.
Jason Bunting
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.
Michael Stum
Good to know. The lack of explicit reference was the only thing I could notice with what information I had
Alex Brault
@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.
Jason Lepack
@Jason - thanks for answering Michael's question, I didn't see it.
Jason Bunting
@TheOtherJason - Cheers!
Jason Lepack
A: 

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();
}
Michael Stum
If you like to do homework for people, I am sure you could make some money at it... ;)
Jason Bunting
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.
Michael Stum
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.
Jason Bunting
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.
Michael Stum
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 ;)
Daok
A: 

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
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.
Jason Lepack
+2  A: 

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.

Jason Bunting
+4  A: 

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.
Blair Conrad
thanx for explaning the problems Its working now thanx man