Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace OnlineExercise15._4
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("Enter the length of the array: ");
- int length = int.Parse(Console.ReadLine());
- int[] array = new int[length];
- for(int i = 0; i < array.Length; i++)
- {
- Console.Write("Element [{0}] = ", i+1);
- int l = int.Parse(Console.ReadLine());
- array[i] = l;
- Console.WriteLine();
- }
- Console.WriteLine("In which position would you like to put the new element: ");
- int positionOne = int.Parse(Console.ReadLine());
- Console.WriteLine("Enter the number you want to enter in that position: ");
- int elementValue = int.Parse(Console.ReadLine());
- int[] newArray = new int[length + 1];
- for(int m = 0; m < positionOne; m++)
- {
- newArray[m] = array[m];
- }
- newArray[positionOne] = elementValue;
- for(int u = positionOne + 1; u < newArray.Length; u++)
- {
- newArray[u] = array[u - 1];
- }
- //Printing the new array
- for(int s = 0; s < newArray.Length; s++)
- {
- Console.Write(newArray[s] + " ");
- }
- Console.WriteLine("Now enter the position which you want to erase: ");
- int positionTwo = int.Parse(Console.ReadLine());
- for (int f = 0; f < positionTwo; f++)
- {
- array[f] = newArray[f];
- }
- for(int g = positionTwo + 1; g < newArray.Length; g++)
- {
- array[g - 1] = newArray[g];
- }
- for(int h = 0; h < array.Length; h++)
- {
- Console.Write(array[h] + " ");
- }
- Console.ReadKey();
- }
- }
- }
Add Comment
Please, Sign In to add comment