SabirSazzad

Array Push & POP at given index

Feb 26th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.29 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.     int n,i,newIndex,newInput,temp,choice;
  7.     cout << "Insert Array size: ";
  8.     cin >> n;
  9.     int *num=new int[n];
  10.     cout << "Insert Array Elements...." <<endl;
  11.     for(i=0; i<n; i++)
  12.     {
  13.         cin >> num[i];
  14.     }
  15.     cout << "For PUSH press 1 & For POP press 2"<<endl;
  16.     cout << "Choise: ";
  17.     cin >> choice;
  18.     if(choice == 1)
  19.     {
  20.         num[n+1];
  21.         cout << "Input at which index new Data PUSH: ";
  22.         cin >> newIndex;
  23.         cout << "Input New element: ";
  24.         cin >> newInput;
  25.         for(i=n+1; i!=newIndex; i--)
  26.             {
  27.                 num[i+1] = num[i];
  28.             }
  29.         temp = num[newIndex];
  30.         num[newIndex] = newInput;
  31.         num[newIndex+1] = temp;
  32.         cout << "New Array is....." <<endl;
  33.         for(i=0; i<n+1; i++)
  34.         {
  35.             cout << num[i] <<endl;
  36.         }
  37.     }
  38.     else if(choice == 2)
  39.     {
  40.         cout << "Input at which index new Data POP: ";
  41.         cin >> newIndex;
  42.         for(i=newIndex; i<n; i++)
  43.         {
  44.             num[i] = num[i+1];
  45.         }
  46.         num[n-1];
  47.         cout << "New Array is....." <<endl;
  48.         for(i=0; i<n-1; i++)
  49.         {
  50.             cout << num[i] <<endl;
  51.         }
  52.     }
  53.  
  54.     return 0;
  55.  
  56. }
Advertisement
Add Comment
Please, Sign In to add comment