-4

This is the Excersice from school I got but Im a little stuck :/

"Write the Method Concat, it will take two arrays of integers as arguments and return an array of integers. The array returned should be resultetet of merging the both Input Arrays."

I only got this far And I dont know how to do the "ending" or what more to add. As you probably guess im new. I tried varius things but I cant get the hang of it :/

public int Concat()
     {
         int[] x = new int[] { 1, 2, 3 };
         int[] y = new int[] { 4, 5, 6 };

         int[] z = x.Concat(y).ToArray();


     }
Hippimaster
  • 41
  • 2
  • 10
  • 2
    do you know how to pass arguments to method? and what is return type of method? – M.kazem Akhgary Nov 09 '16 at 11:48
  • All you did was using the existing Method for this. I doubt this is an allowed solution. So write your own. You need to create a new Array with the length of x.Length + y.Length and load all fields accordingly. You should show atleast some effort. – CSharpie Nov 09 '16 at 11:48
  • 3
    This is not a homework service. – CSharpie Nov 09 '16 at 11:51
  • @CSharpie - I don't think we should judge the question's quality based solely on whether it's homework or not. Did you ever ask for help when you got stuck on your homework? – sr28 Nov 09 '16 at 11:57

3 Answers3

0

Try this

var z = new int[x.Length + y.Length];
x.CopyTo(z, 0);
y.CopyTo(z, x.Length);
Tony Vincent
  • 13,354
  • 7
  • 49
  • 68
  • Not a very good answer for someone who got stuck at passing arguments. – Poody Nov 09 '16 at 11:50
  • 1
    Also this makes use of CopyTo and i bet Op is asking for his homework here. The teacher probably wants a solution using for-loops. – CSharpie Nov 09 '16 at 11:50
  • 1
    This looks familiar...http://stackoverflow.com/questions/1547252/how-do-i-concatenate-two-arrays-in-c – sr28 Nov 09 '16 at 11:53
0

You have to pass two arguments to method and then concat those arrays like e.g. @Tony Vincent mention:

public int[] Concat(int x, int[] y)
{
    var z = new int[x.Length + y.Length];
    x.CopyTo(z, 0);
    y.CopyTo(z, x.Length);

    return z;
}
0

You need to pass two parameter from where you have called the function. I am providing an sample below.

class Program
{
    public int[] Concat(int[] x, int[] y)
    {
        int[] z = x.Concat(y).ToArray();
        return z;

    }
    static void Main(string[] args)
    {
        Program program = new Program();
        int[] x = new int[] { 1, 2, 3 };
        int[] y = new int[] { 4, 5, 6 };
        int[] z = program.Concat(x, y);
        Console.ReadLine();
    }
}

here we have passed x and y to the concat method and it will return the merged array and will be stored in the z int array of main method.

lukai
  • 536
  • 1
  • 5
  • 20