C# Program to Get 2 Arrays as Input and Produce a 3rd Array by Appending one to other

«
»

This is a C# Program to Get 2 arrays as input and produce a 3rd array by appending one to other.

Problem Description

This C# Program Gets 2 Arrays as Input and Produce a 3rd Array by Appending one to other.

Problem Solution

Here Buffer.BlockCopy is used to merge two int arrays. This method acts upon bytes, not elements.

advertisement
Program/Source Code

Here is source code of the C# Program to Get 2 Arrays as Input and Produce a 3rd Array by Appending one to other. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Get 2 Arrays as Input and Produce a 3rd Array by Appending one to 
 * other
 */
using System;
 
class Program
{
    static void Main()
    {
        int[] array1 = new int[5];
        int[] array2 = new int[5];
        int[] array3 = new int[array1.Length + array2.Length];
        Console.WriteLine("Enter Any 5 Elements for the First Array :");
        for (int i = 0; i < 5; i++)
        {
            array1[i] = int.Parse(Console.ReadLine());
        }
        Console.WriteLine("Enter Any 5 Elements for the Second Array :");
        for (int i = 0; i < 5; i++)
        {
            array2[i] = int.Parse(Console.ReadLine());
        }
        Buffer.BlockCopy(array1,0,array3,0,array1.Length * sizeof(int));
        Buffer.BlockCopy(array2,0,array3,array1.Length * sizeof(int),
                         array2.Length * sizeof(int));
        Console.WriteLine("Elements in the Third Array After Appending" +
                          " First and Second Arrays :");
        foreach (int value in array3)
        {
            Console.WriteLine(value);
        }
        Console.ReadLine();
    }
}
Program Explanation

This C# program is used to get 2 arrays as input and produce a 3rd array by appending one to other. Using for loop we are entering the coefficient element values for the array1 and array2 variables.

advertisement

The Buffer.BlockCopy() function is used to merge two int arrays. This method acts upon bytes, not elements. Using foreach loop print the elements in third array after appending with first and second arrays.

Here the foreach statement is used to iterate through the collection to get the information that you want, but cannot be used to add or remove items from the source collection to avoid unpredictable side effects.

Runtime Test Cases
 
Enter Any 5 Elements for the First Array :
1
2
3
4
5
Enter Any 5 Elements for the Second Array :
6
7
8
9
10
Elements in the Third Array After Appending First and Second Arrays :
1
2
3
4
5
6
7
8
9
10

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

advertisement
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn