Write a program in C# Sharp to delete an element at desired position from an array ?

  • برمجة سي شارب
  • برمجة

Write a program in C# Sharp to delete an element at desired position from an array ?

Output:

Delete an element at desired position from an array :                                                         
---------------------------------------------------------                                                     
Input the size of array : 3                                                                                   
Input 3 elements in the array in ascending order:                                                             
element - 0 : 2                                                                                               
element - 1 : 4                                                                                               
element - 2 : 6  
Input the position where to delete: 2      
The new list is :   2  6 

الأجوبة

using System;  
public class Exercise15  
{  
    public static void Main() 
{
    int i,pos,n;
	int[] arr1 = new int[50]; 
       Console.Write("\n\nDelete an element at desired position from an array :\n");
       Console.Write("---------------------------------------------------------\n");   
       Console.Write("Input the size of array : ");
	   n = Convert.ToInt32(Console.ReadLine()); 
    /* Stored values into the array*/
       Console.Write("Input {0} elements in the array in ascending order:\n",n);
       for(i=0;i<n;i++)
            {
	      Console.Write("element - {0} : ",i);
		  arr1[i] = Convert.ToInt32(Console.ReadLine()); 
	    }
     Console.Write("\nInput the position where to delete: ");
	 pos = Convert.ToInt32(Console.ReadLine());   
/*---- locate the position of i in the array -------*/
  i=0;
  while(i!=pos-1)
            i++;
/*---- the position of i in the array will be replaced by the 
       value of its right */
  while(i<n){
            arr1[i]=arr1[i+1];
            i++;
  }
  n--;
   Console.Write("\nThe new list is : "); 
  for(i=0;i<n;i++)
        {
		   Console.Write("  {0}",arr1[i]);
		}  
            Console.Write("\n\n");
}
}
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...