3
Reply

C# ARRAY Program

Sun Light

Sun Light

Nov 3 2016 12:36 PM
357
Hi all,
 
 
///Program for new element to be added in array
 
int[] arr1 = new int[] {2,3,4,5,6};
int m, n;
Console.Write("Input the value to be inserted in array:");
m = Convert.ToInt32(Console.ReadLine());
Console.Write("Input the position,where the value to be inserted in array:");
n = Convert.ToInt32(Console.ReadLine());
/* Move all data at right side of the array */
for (int i = arr1.Length; i >= n; i--)
arr1[i] = arr1[i -1];
/* insert value at given position */
arr1[n - 1] = m;
Console.Write("New array is:");
for(int i =0;i <arr1.Length;i++)
{
Console.Write("{0} ", arr1[i]);
}
Console.ReadLine();
 ///********************************************
 
 
Can someone help me for this program.Its showing me error on  arr1[i] = arr1[i -1] ; "index was outside the bounds of array."how can i solve it.pls help.
 
Thanks 

Answers (3)