Given two int arrays, return an array of length 2 containing as much will fit from the first and second array

0

I'm trying to solve the exercise bellow. I don't get any compiler errors.

When I run it though, in the main method only the first Make2 gets called and the program stops working with this error:

The program '[4864] Make2Two.vshost.exe' has exited with code -1073741510 (0xc000013a).

Could someone help me with that?

And is there any better way to solve the problem? I think I put too much unnecessary code.

Thanks a lot.

Problem: Given 2 int arrays, a and b, return a new array length 2 containing, as much as will fit, the elements from a followed by the elements from b. The arrays may be any length, including 0, but there will be 2 or more elements available between the 2 arrays.

My code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Make2Two
{
    class Program
    {
        static void Main(string[] args)
        {
            Make2(new int[] { 4 }, new int[] { 4, 2, 3 }); //output 44
            Make2(new int[] { 2, 3 }, new int[] { 2, 3, 6 }); //the others are not running
            Make2(new int[] { 2, 5 }, new int[] { 7, 6, 5 });
        }
        public static int[] Make2(int[] a, int[] b)
        {
            int[] result = new int[2];
            if (a.Length >= 2)
            {
                for (int i = 0; i < 2; i++)
                {
                    result[i] = a[i];
                    Console.Write(result[i]);
                }
                Console.WriteLine();
                Console.ReadLine();
            }
            if (a.Length < 2)
            {
                for (int i = 0; i < 2; i++)
                {
                    result[0] = a[0];
                    if (i == 0)
                    {
                        for (int j = 1; j < 2; j++)
                        {
                            result[j] = b[j - 1];
                            for (int k = 0; k < result.Length; k++)
                            {
                                Console.Write(result[k]);
                            }
                        }
                        Console.WriteLine();
                        Console.ReadLine();
                    }
                }
            }
            return a;
        }
    }
}
c#
arrays
asked on Stack Overflow May 9, 2015 by coze • edited May 9, 2015 by Alexey Gorozhanov

5 Answers

3

How about this?

int[] a = new int[5];
int[] b = new int[5];

var result = a.Concat (b).Take (2).ToArray();

PS: Would recommend reading about LINQ (https://msdn.microsoft.com/en-us/library/bb397897.aspx)

answered on Stack Overflow May 9, 2015 by Chris • edited May 9, 2015 by Chris
0

I do think you are overcomplicating it a little...

And your code is wrong:

result[0] = a[0];

How can you be sure that a has at least one element? You should check it!

Let's try in another way: three indexes: i (index of result), ai (index of a), bi (index of b). We always increment i. We increment ai or bi when we respectively take an element from a or from b.

public static int[] Make2(int[] a, int[] b)
{
    int[] result = new int[2];

    for (int i = 0, ai = 0, bi = 0; i < result.Length; i++)
    {
        if (ai < a.Length)
        {
            result[i] = a[ai];
            ai++;
        }
        else if (bi < b.Length)
        {
            result[i] = b[bi];
            bi++;
        }
        else
        {
            break;
        }

        Console.Write(result[i]);
    }

    Console.WriteLine();

    return result;
}

Another possible solution. Here we have two for cycles, one for a and one for b. The index i for result is shared.

public static int[] Make2(int[] a, int[] b)
{
    int[] result = new int[2];

    int i = 0;

    for (int ai = 0; i < result.Length && ai < a.Length; i++, ai++)
    {
        result[i] = a[ai];
        Console.Write(result[i]);
    }

    for (int bi = 0; i < result.Length && bi < b.Length; i++, bi++)
    {
        result[i] = b[bi];
        Console.Write(result[i]);
    }

    Console.WriteLine();

    return result;
}

Addendum: if you want to check that your algorithm works, you should check corner cases like:

Make2(new int[] { }, new int[] { 1, 2 }); // output 12
Make2(new int[] { 4, 5 }, new int[] { }); // output 45
Make2(new int[] { 1 }, new int[] { 5 }); // output 15

Note that in general, functions that do calculations and functions that write should be separate, so you should remove all the Console.Write from Make2 and put them in a separate method, that should be called by the Main method.

answered on Stack Overflow May 9, 2015 by xanatos • edited May 9, 2015 by xanatos
0
public int[] make2(int[] a, int[] b) {
  int[] arr=new int[2];


  if(a.length>=2){
    arr[0]=a[0];
    arr[1]=a[1];
    return arr;
  }
  if(a.length==1){
    arr[0]=a[0];
    arr[1]=b[0];
    return arr;

  }
  if(a.length==0){
    arr[0]=b[0];
    arr[1]=b[1];
    return arr;
  }
  return arr;




}
answered on Stack Overflow Aug 16, 2019 by Rares • edited Aug 16, 2019 by Rares
0
public int[] make2(int[] a, int[] b) {
  int[] array = new int[2];
  
  int counter = 0;

 for(int i=0;i<a.length;i++)
 {
   if(counter < 2)
     array[counter++] = a[i];
 }
 for(int i=0;i<b.length;i++)
 {
   if(counter < 2)
     array[counter++] = b[i];
 }
  return array;
}

Run separate for loops to check the elements in each array.

answered on Stack Overflow Nov 1, 2020 by Ambrish Rajput
-2
public int[] plusTwo(int[] a, int[] b)
{
    int res[]=new int[4];

    for(int i=0; i<2; i++)
    {
        res[i]=a[i];
        res[i+2]=b[i];
    }

    return res;
}
answered on Stack Overflow Aug 5, 2020 by codespider • edited Aug 5, 2020 by Hostel

User contributions licensed under CC BY-SA 3.0