3

I m trying to create an array wich consists of an input array (length=N) and the same array in reverse order so that the output array is length (2N). Like if input = {1,2,3} then output={1,2,3,3,2,1}

This is what I tried:

public static double[] stack(double[] input)
{
    int N = input.Length;
    var z = new double[2 * N];
    input.CopyTo(z, 0);
    Array.Reverse(input).CopyTo(z, N);

    return z;
}

But this throws an error (Operator cannot be applied to void type). where am I going wrong please? Is this the fastest approach?

4 Answers 4

9

As another answer points it out why it doesn't work, I'll focus on how it can be written easier using Linq.

public static double[] Stack(double[] input)
{
    return input.Concat(input.Reverse())
                 .ToArray();
}

Linq makes life easier :)

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

2 Comments

neat. What do you think which would be faster though?
@nik I'd not worry about performance unless it is very very important. Just use the code which is more readable. btw if you need the performance result benchmark it yourself.
4

The Reverse method modifies the the array that you pass into it and returns void. Try this:

public static double[] stack(double[] input)
{
    int N = input.Length;        // assuming input = { 1, 2, 3 }
    var z = new double[2 * N];   // z = { 0, 0, 0, 0, 0, 0 }
    input.CopyTo(z, 0);          // z = { 1, 2, 3, 0, 0, 0 }
    input.CopyTo(z, N);          // z = { 1, 2, 3, 1, 2, 3 }
    Array.Reverse(z, N, N);      // z = { 1, 2, 3, 3, 2, 1 }

    return z;
}

2 Comments

which one do you think is faster? yours or linq? Thank you!
@nik I would expect that using the array methods would be faster, but why don't you benchmark both methods to see?
2

Here is my simple benchmark of three methods proposed by users:

static void Main(string[] args)
{
    Stopwatch sw = new Stopwatch();

    double[] input = Enumerable.Range(0, 10000000).Select(i => (double)i).ToArray();

    while (true)
    {
        sw.Start();

        LinqStack(input);

        sw.Stop();

        Console.WriteLine("LinqStack(): {0}ms", sw.ElapsedMilliseconds);

        sw.Restart();

        SimpleStack(input);

        sw.Stop();

        Console.WriteLine("SimpleStack(): {0}ms", sw.ElapsedMilliseconds);

        sw.Restart();

        OriginalStack(input);

        sw.Stop();

        Console.WriteLine("OriginalStack(): {0}ms", sw.ElapsedMilliseconds);

        sw.Reset();

        Console.ReadLine();
    }
}

/// <summary>
/// By Sriram Sakthivel
/// </summary>
static double[] LinqStack(params double[] input)
{
    return input.Concat(input.Reverse())
                    .ToArray();
}

static double[] SimpleStack(params double[] input)
{
    int length = input.Length;

    double[] output = new double[length * 2];

    for (int i = 0; i < length; i++)
    {
        output[i] = input[i];
    }

    for (int i = 0; i < length; i++)
    {
        output[i + length] = input[length - i - 1];
    }

    return output;
}

/// <summary>
/// By p.s.w.g
/// </summary>
static double[] OriginalStack(params double[] input)
{
    int N = input.Length;        // assuming input = { 1, 2, 3 }
    var z = new double[2 * N];   // z = { 0, 0, 0, 0, 0, 0 }
    input.CopyTo(z, 0);          // z = { 1, 2, 3, 0, 0, 0 }
    input.CopyTo(z, N);          // z = { 1, 2, 3, 1, 2, 3 }
    Array.Reverse(z, N, N);      // z = { 1, 2, 3, 3, 2, 1 }

    return z;
}

Results:

LinqStack(): 647ms SimpleStack(): 184ms OriginalStack(): 66ms

LinqStack(): 710ms SimpleStack(): 175ms OriginalStack(): 66ms

LinqStack(): 602ms SimpleStack(): 334ms OriginalStack(): 95ms

As you see, while the linq method is the simplest and most readable, it is not the most efficient. Using self-written loop also is not the best idea if you worry about performance. Standart array operations are faster.

Comments

1

it's work

 public  static  double []  stack ( double [] input ) 
{ 
    int N = input.Length; 
    var z =  new  double[2*N]; 
    input.CopyTo(z, 0); 
    Array.Reverse(input);
    input.CopyTo( z , N );
    return z; 
}

1 Comment

You should note that this modifies input. That may or may not be the intent.

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.