HomeCSharpC# Program to Merge Two Arrays into Third Array

C# Program to Merge Two Arrays into Third Array

This program demonstrates a basic array merging operation in C#. This operation involves combining the elements of two separate arrays to create a new array that contains all the elements from the original arrays. The merged array will be generated and displayed as the output.

Problem statement

You are required to write a C# program that merges two arrays into a third array.

C# Program to Merge Two Arrays into Third Array

using System;
using System.Linq;

class Program
{
    static void Main()
    {
        int[] arr1 = { 1, 2, 3 };
        int[] arr2 = { 4, 5, 6 };

        int[] mergedArray = arr1.Concat(arr2).ToArray();

        Console.WriteLine("Merged Array:");
        foreach (int element in mergedArray)
        {
            Console.Write(element + " ");
        }
    }
}

How it works

The provided C# program is designed to merge two arrays (arr1 and arr2) into a third array (mergedArray). Here’s an explanation of how the program works:

  1. Introduction Message:
    • The program starts by printing a welcome message to the console, indicating that it’s a program for merging arrays.
  2. Array Initialization:
    • Two integer arrays, arr1 and arr2, are defined. In this example, arr1 contains elements [1, 2, 3] and arr2 contains elements [4, 5, 6]. These arrays are used as input for merging.
  3. Merged Array Initialization:
    • A third array, mergedArray, is created with a length equal to the sum of the lengths of arr1 and arr2. In this case, mergedArray will have a length of 6.
  4. Merging Arrays:
    • Two loops are used to copy elements from arr1 and arr2 into mergedArray.
    • The first loop iterates over the elements of arr1. It copies each element to the corresponding position in mergedArray.
    • The second loop iterates over the elements of arr2. It copies each element to the position after the last element of arr1 in mergedArray.
  5. Display Merged Array:
    • After the merging process, the program prints the merged array by iterating over the elements of mergedArray and printing them to the console.
  6. Output:
    • In the provided example, the program prints:

Code:

Merged Array:
1 2 3 4 5 6

  • This indicates that arr1 and arr2 have been successfully merged into.

In summary, the program takes two input arrays, merges them into a third array using loops, and then displays the merged array. This program can handle arrays of different lengths, as long as they have at least three elements.

Input/Output

Share:

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...