Advertisement
Guest User

array insertion and deletion

a guest
Jun 16th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.84 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3.  
  4. using namespace std;
  5.  
  6. void inpins()
  7. {
  8.     int a [100] , n , pos , item ;
  9.     cout << "ENTER THE ARRAY SIZE: " << endl ;
  10.     cin >> n ;
  11.     cout << "ENTER THE ARRAY ELEMANT'S: " << endl ;
  12.     for(int i=0 ; i<n ; i++)
  13.     {
  14.         cin >> a [i] ;
  15.     }
  16.     cout << "ENETER THE ITEM VALUE: " << endl ;
  17.     cin >> item ;
  18.     cout << "FIND THE POSITION WHICH YOU INSERTED: " << endl ;
  19.     cin >> pos ;
  20.     if(pos > n || pos <= 0)
  21.             cout<< "POSITION NOT FOUND !" << endl ;
  22.     else
  23.         for(int i = n ; i>=pos ; i--)
  24.         {
  25.                 for(i=n; i>=pos; i--)
  26.                 {
  27.                     a [i] = a [i-1];
  28.                 }
  29.                 a [i] = a [i-1] ;
  30.                 a [pos-1] = item ;
  31.                 n++;
  32.                 cout << "ARRAY INSERTED SUCCESSFULLY." << endl ;
  33.                 cout << "THE NEW ARRAY LINE IS: " << endl ;
  34.                 for(int i=0 ; i<n; i++)
  35.                 {
  36.                     cout << a [i] << " " ;
  37.                 }
  38.         }
  39.  
  40. }
  41.  
  42. void outins()
  43. {
  44.     int a [70] = {5346,547,8578,468,36,6} ;
  45.     int n = 6 ;
  46.     int pos = 5 ;
  47.     int item = 90 ;
  48.     for(int i = n ; i>=pos ; i--)
  49.     {
  50.         a [i+1] = a [i] ;
  51.     }
  52.     a [pos] = item ;
  53.  
  54.     cout << "NEW ARRAY LINE IS: " << endl ;
  55.     for(int i=0 ; i<n+1 ; i++)
  56.     {
  57.         cout << a [i] << " " ;
  58.     }
  59. }
  60. void outdel()
  61. {
  62.     int a [70] = {5346,547,8578,468,36,6} ;
  63.     int n = 6 ;
  64.     int pos = 1 ;
  65.  
  66.     for(int i = pos ; i<n ; i++)
  67.     {
  68.         a [i] = a [i+1] ;
  69.     }
  70.  
  71.     cout << "NEW ARRAY LINE IS: " << endl ;
  72.     for(int i=0 ; i<n - 1 ; i++)
  73.     {
  74.         cout << a [i] << " " ;
  75.     }
  76. }
  77.  
  78. void inpdel()
  79. {
  80.     int a [100] , n , pos ;
  81.     cout << "ENTER THE ARRAY SIZE: " << endl ;
  82.     cin >> n ;
  83.     cout << "ENTER THE ARRAY ELEMANT'S: " << endl ;
  84.     for(int i=0 ; i<n ; i++)
  85.     {
  86.         cin >> a [i] ;
  87.     }
  88.     cout << "FIND THE POSITION WHICH YOU DELETED: " << endl ;
  89.     cin >> pos ;
  90.     for(int i = pos ; i < n ; i++)
  91.     {
  92.         a [i] = a [i+1] ;
  93.     }
  94.     cout << "ARRAY DELETED SUCCESSFULLY." << endl ;
  95.     cout << "THE NEW ARRAY LINE IS: " << endl ;
  96.     for(int i=0 ; i<n -1; i++)
  97.     {
  98.         cout << a [i] << " " ;
  99.     }
  100.  
  101. }
  102. void arrpoi()
  103. {
  104.     int  a [4] = {2334,46,577,66} ;
  105.     int *o ;
  106.     o = & a [0] ;
  107.     cout << "first ind add: " << o << endl ;
  108.     cout << "first ind val: " << *o << endl ;
  109.     cout << "fourth ind add: " << o+3 << endl ;
  110.     cout << "fourth ind val: " << *(o+3) << endl;
  111.     cout << "third ind add: " << a+2 << endl ;
  112.     cout << "third ind val: " << *(a+2) << endl;
  113.  
  114.  
  115. }
  116.  
  117.  
  118.  
  119. int main()
  120. {
  121.    inpins() ;
  122.     getch();
  123.     cout << endl ;
  124.     inpdel();
  125.     cout << endl ;
  126.     outins();
  127.     cout << endl ;
  128.     outdel();
  129.     getch();
  130.     arrpoi();
  131.     getch() ;
  132.  
  133.     return 0;
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement