Wednesday, June 1, 2011

Menu driven program on dynamic insertion to or deletion from an array


In this array program we will take values in an array first and then take choice from the user which operation he / she wants,  to insert a new value at any specific location or wants to delete value from a specified location and required operation will be carried out.

Codes of the array program

import java.io.*;
class Array
{
 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
 int arr[]=new int[11];
 int i,loc,n,choice,size;
 public void take() throws Exception
 {
  for(i=0;i< 10;i++)
  {
  System.out.println("Enter value:");
  arr[i]=Integer.parseInt(br.readLine());
  }
  System.out.println("After inserting the values, array is as follows\n");
  size=10;
  display(size);
  System.out.println("\nWhich operation you want, press 1 for insertion and press 2 for deletion:");
  choice=Integer.parseInt(br.readLine());
  if (choice==1)
  insert();
  else if(choice==2)
  delete();
  }
  void insert() throws Exception
  {
    System.out.println("Enter the value to insert:");
    n=Integer.parseInt(br.readLine());
    System.out.println("Enter the location where to insert:");
    loc=Integer.parseInt(br.readLine());
    loc=loc-1;
    for(i=10;i > loc;i--)
    {
     arr[i]=arr[i-1];
    }
    arr[i]=n;
  System.out.println("After inserting new value, array is as follows\n");
  size=11;
  display(size);
 }
 void delete()  throws Exception
 {
    System.out.println("Enter the location where the value is to be deleted:");
    loc=Integer.parseInt(br.readLine());
    loc--;
    for(i=loc;i< 10-1;i++)
    {
     arr[i]=arr[i+1];
     }
  System.out.println("After deleting any value, array is as follows\n");
  size=9;
  display(size);
 }
  void display(int s)
  {
   for(i=0;i< s;i++)
   {
    System.out.print(" "+arr[i]);
   }
  }
  public static void main(String args[]) throws Exception
  {
   Array ob=new Array();
   ob.take();
   }
   }

Technical analysis of the array program

I think storing values in the array and displaying the array needs no explanations. Size of the array is kept 1 more than the required size as it may have to accommodate one extra value in the existing array. According to user choice, either insert () function or delete () function is invoked. Both the functions are defined in the program. If the job is to insert a new value in the array, then the value and the locations are stored in two separate variables. If the location specified is 5, then the actual location in the array is 4 and that’s why the value of ‘loc’ is reduced by 1 in both the cases, insertion or deletion. In case of inserting new value at any specific location of the array, the values in the array from extreme right to the specific location are copied to it’s right location to make room for the new value. If the case is of deletion from array, the values from the just right location value where the value of the array is to be deleted to the extreme right value are copied to it’s location to eliminate the specific location value of the array.
After each operation, values in the array are displayed with appropriate size value of the array.

Related Post: BlueJ Menu Based Programs

3 comments:

  1. Sir is it possible to solve the program of "counting the frequency of each word in a string" by not using the String tokeniser. Because in icse we dont learn string tokeniser. Pls help Sir.

    ReplyDelete
  2. yes, it can be done using 'indexOf()' and 'substring()' function. indexOf() will locate the space and substring() will extract the word. Try it yourself, if any problem, I will help.

    ReplyDelete
  3. program on inheritance using super,

    ReplyDelete

Subscribe via email

Enter your email address:

Delivered by FeedBurner