I have used Visual Studio 2012 for debugging purpose. But you can use any version of visul studio as per your availability.
using System;
class Program
{
static void Main()
{
int[] arr = new int[100];
int i, num, size, position;
// Reading array size & elements in the array
Console.WriteLine("Enter size of the array: ");
size = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter elements in the array: ");
for(i=0; i<size; i++)
{
arr[i] = Convert.ToInt32(Console.ReadLine());
}
//Reading element to insert & position of the element
Console.WriteLine("Enter element to insert: ");
num = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the element position: ");
position = Convert.ToInt32(Console.ReadLine());
//checking elements valis position
if (position > size + 1 || position <= 0)
{
Console.WriteLine("Invalid position! Please enter position between 1 to " + num);
}
else
{
//Inserting element in an array & increasing the size of the array
for (i = size; i >= position; i--)
{
arr[i] = arr[i - 1];
}
arr[position - 1] = num;
size++;
// Printing new array with new element
Console.WriteLine("Array elements after insertion :");
for (i = 0; i < size; i++)
{
Console.WriteLine(arr[i] + "\t");
}
}
Console.ReadLine();
}
}
I have used Visual Studio 2012 for debugging purpose. But you can use any version of visul studio as per your availability.
Result:
Enter size of the array:
5
Enter elements in the array:
1
2
3
4
5
Enter element to insert:
20
Enter the element position: 2
Array elements after insertion :
1
20
2
3
4
5
need an explanation for this answer? contact us directly to get an explanation for this answer