This C# Program to Copy a Section of One Array to Another. Here a section of array is copied using the array.copy() with the source array,target array and the size.
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();
}
}
Here is the output of the C# Program:
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.
If you wish to look at all C# Programming examples, go to 1000 C# Programs.