Q:

Merge two arrays into third array using C# program

belongs to collection: C# Basic Programs | array programs

0

Given two integer arrays and we have to merge them and store in third array.

For example we have two arrays:

ARR1: 10 20 30 40 50
ARR2: 60 70 80 90 100
Now we merge above arrays into another temporary array ARR3:
ARR3: 10 20 30 40 50 60 70 80 90 100

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Consider the example:

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            int i = 0 ;
            int j = 0;

            int[] arr1 = new int[5];
            int[] arr2 = new int[5];
            int[] arr3 = new int[10];

            //Read numbers into array
            Console.WriteLine("Enter elements of ARR1 : ");
            for (i = 0; i < 5; i++)
            {
                Console.Write("Element[" + (i + 1) + "]: ");
                arr1[i] = int.Parse(Console.ReadLine());
            }

            //Read numbers into array
            Console.WriteLine("Enter elements of ARR2 : ");
            for (i = 0; i < 5; i++)
            {
                Console.Write("Element[" + (i + 1) + "]: ");
                arr2[i] = int.Parse(Console.ReadLine());
            }

            //Merge arr1 and arr2 to arr3
            for (i = 0,j=0; i < 5; i++)
            {
                arr3[j++] = arr1[i];
            }
            for (i = 0; i < 5; i++)
            {
                arr3[j++] = arr2[i];
            }

            //Print merged array
            Console.WriteLine("Elements of ARR3 : ");
            for (i = 0; i < 10; i++)
            {
                Console.WriteLine("Element[" + (i + 1) + "]: "+arr3[i]);
                
            }

            Console.WriteLine();
        }
    }
}

Output

Enter elements of ARR1 :
Element[1]: 10
Element[2]: 20
Element[3]: 30
Element[4]: 40
Element[5]: 50
Enter elements of ARR2 :
Element[1]: 60
Element[2]: 70
Element[3]: 80
Element[4]: 90
Element[5]: 100
Elements of ARR3 :
Element[1]: 10
Element[2]: 20
Element[3]: 30
Element[4]: 40
Element[5]: 50
Element[6]: 60
Element[7]: 70
Element[8]: 80
Element[9]: 90
Element[10]: 100

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# Basic Programs | array programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C# program to make a simple ATM machine... >>
<< Find total number of occurrence of a given number ...