Pages

Wednesday, 18 December 2019

Program to delete an element in an array

import java.util.Scanner;
public class Rkdelete
 {
  public static void main(String[] args)
  {
   int pos,item,n;
   Scanner s=new Scanner(System.in);
   System.out.println("Enter Size Of Array :");
   n=s.nextInt();
   int[] a;//declaration of array
   a=new int[n]; // Creation of array with given size plus one
 
   for(int i=0;i<n;i++)
       {
        System.out.printf("enter a[%d] = ",i);
        a[i]=s.nextInt();
       }
  System.out.print("\n ARRAY element  :");
  for(int i=0;i<n;i++)
       {
        System.out.print( a[i] +" ");
       }
 
   for(int i=0;i<n;i++)
   System.out.printf("\nAddress:%h ---array : a[%d] ---- data :%d  position : %d",i*4,i,a[i],i+1);
   System.out.print("\nenter the position of the item to be deleted :\n");
   pos=s.nextInt();
   pos=pos-1;
   for(int j=pos;j<n-1;j++)
       {
        a[j]=a[j+1];
       }
 
   System.out.print("\n ARRAY after deleting an element  :\n");
   for(int i=0;i<n-1;i++)
       {
        System.out.print( a[i] +" ");
       }
 
for(int i=0;i<n-1;i++)
System.out.printf("\nAddress:%h ---array : a[%d] ---- data :%d  position : %d",i*4,i,a[i],i+1);
}
}
output:

No comments:

Post a Comment

Constructors & Destructors in c++

  Constructors :  A Constructor is a special member function, which is used to initialize the objects of its class. The Constructor is invok...