Page 285 - computer science (868) class 11
P. 285

13              }
                   14              System.out.println("Enter a number to insert: ");

                   15              n=sc.nextInt();
                   16              System.out.println("Enter a position where to insert: ");

                   17              pos=sc.nextInt();
                   18              if(pos>=0 && pos<=9)

                   19              {
                   20                  for(i=9; i>pos; i--) // Shift the elements to the right by one position
                   21                  {

                   22                         ar[i]=ar[i-1];

                   23                  }
                   24                  ar[pos]=n;
                   25              }

                   26              for(i=0; i<10; i++)
                   27              {

                   28                   System.out.print(ar[i]+ " ");
                   29              }

                   30          }
                   31      }


                 The output of the preceding program is as follows:
                 Enter a number: 23
                 Enter a number: 45
                 Enter a number: 21
                 Enter a number: 67
                 Enter a number: 90
                 Enter a number: 27
                 Enter a number: 98
                 Enter a number: 78
                 Enter a number: 56
                 Enter a number to insert:
                 100
                 Enter a position where to insert:
                 9
                 23 45 21 67 90 27 98 78 56 100

                     10.6 DELETION
                 An element can be deleted from the array at any time. Deletion is the process of deleting or removing an element from
                 the array from the given position. In this technique, the index position is taken from the user and then the value found
                 at that index is deleted. Also, the number to be deleted can also be taken from the user.


                                                                                                                       283
                                                                                                              Arrays   283
   280   281   282   283   284   285   286   287   288   289   290