C# Program to Copy a Section of One Array to Another

«
»

This is a C# Program to copy a section of one array to another.

Problem Description

This C# Program Copies a Section of One Array to Another.

Problem Solution

Here a section of array is copied using the array.copy() with the source array,target array and the size.

advertisement
Program/Source Code

Here is source code of the C# Program to Copy a Section of One Array to Another. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Copy a Section of One Array to Another
 */
using System;
class Program
{
    static void Main()
    {
 
        int n, m, size;
        Console.WriteLine("Enter the size of the Array : ");
        n = Convert.ToInt32(Console.ReadLine());
        int [] a = new int[n];
        Console.WriteLine("Enter the Elements of the First Array :");
        for (int i = 0; i <n; i++)
        {
            a[i] = Convert.ToInt32(Console.ReadLine());
        }
        Console.WriteLine("Enter the Size of the Target Array : ");
        m = Convert.ToInt32(Console.ReadLine());
        int[] target = new int[m];
        Console.WriteLine("Enter the section of the First Array "+ 
                          "that has to be Copied :");
        size = Convert.ToInt32(Console.ReadLine());
        Array.Copy(a, 0, target, 0, size);
        Console.WriteLine("New Array With The Specified Section of Elements "+
                          "in the First Array");
        foreach (int value in target)
        {
            Console.WriteLine(value);
        }
        Console.Read();
    }
}
Program Explanation

In this C# program, we are reading the size of the array using ‘n’ variable. Using for loop we are entering the coefficient element values of an array using a[] variable.

Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
advertisement

Enter the size of the target array the section of the first array that has to be copied using ‘m’ and ‘size’ variables respectively. Here a section of array is copied using the array.copy() with the source array, target array and the size.

Using foreach loop print the new array with the specified section of elements in the first array. 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 the size of the Array : 5
Enter the Elements of the First Array : 
1
2
3
4
5
Enter the Size of the Target Array :
6
Enter the section of the First Array that has to be Copied : 3
New Array With The Specified Section of Elements in the First Array :
1
2
3
0
0
0

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
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.