title: C++ implementation of stack using array
body: <p>I have studied the algorithm from Introduction to Algorithm and then
  I have written this code. But in my output another value is showing for index 0. and when I use pop function it display 1 instead of 3</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  int top;
  void initialise_top(){
  top = -1;
  }
  
  bool stack_empty(int a[]){
  if(top == -1)
      return true;
  else
      return false;
  }
  
  void push(int a[], int x, int s){
  if(top &lt; s - 1){
  top = top + 1;
  a[top] = x;
  }
  else
      std::cout &lt;&lt; "overflow" &lt;&lt; "\n";
  }
  
  int pop(int a[]){
  if (stack_empty(a) == true)
      std::cout &lt;&lt; "Underflow" &lt;&lt; "\n";
  else{
      --top;
      return a[top+1];
  }
  }
  
  void display(int a[]){
    for(int i = 0;i &lt;= top; i++){
      std::cout &lt;&lt; a[i] &lt;&lt; " ";
    }
  }
  int main()
  {
      int arr[7];
      push(arr,15,7);
      push(arr,6,7);
      push(arr,2,7);
      push(arr,9,7);
      push(arr,17,7);
      push(arr,3,7);
      display(arr);
      std::cout &lt;&lt; "\n";
      int out = pop(arr);
      std::cout &lt;&lt; pop &lt;&lt; "\n";
  
      return 0;
  }
  </code></pre>
  
  <p>Here is the snapshot of the output
  <a href="https://i.stack.imgur.com/gAsEK.png" rel="nofollow noreferrer">enter image description here</a></p>