-4

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

#include <iostream>

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 < s - 1){
top = top + 1;
a[top] = x;
}
else
    std::cout << "overflow" << "\n";
}

int pop(int a[]){
if (stack_empty(a) == true)
    std::cout << "Underflow" << "\n";
else{
    --top;
    return a[top+1];
}
}

void display(int a[]){
  for(int i = 0;i <= top; i++){
    std::cout << a[i] << " ";
  }
}
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 << "\n";
    int out = pop(arr);
    std::cout << pop << "\n";

    return 0;
}

Here is the snapshot of the output enter image description here

coder
  • 143
  • 10

4 Answers4

2

In your implementatiton you have "initialise_top()" function.

void initialise_top(){
   top=-1;
}

But you don't call it in main function. If you don't call it you can't initialize "top" variable and "top" variable will hold garbage value. You can read details in here : Default variable value

And also in theese lines you have some mistakes:

int out=pop(arr);
std::cout<<pop<<"\n";

you must print "out" variable :

std::cout << out << "\n";

You can look to corrected code for your implementation in here :

https://repl.it/JaOd/0

EmreAydin
  • 183
  • 2
  • 7
0

I have this stack array code in C. You can use it as your guide in implementing it to C++.

#include <stdio.h>
#include <stdlib.h>

void push(void);
void pop(void);

int a[5];
int top = -1;
int counter = 0;
int choice;


main() {

do{
    printf("*********************************************\nSTACK\nPress the 
corresponding button you desire.\n\nPress 1 to push a number to 
stack.\nPress 2 to display the current stack.\nPress 3 to pop the current 
stack.\nPress 0 to exit.\n\n");
    scanf("%d", &choice);
    if(choice == 0){
        choice = 0;
    }
    else if(choice == 1){
        push();
    }
    else if(choice == 2){
        int i;
    printf("Current Stack:\n");
    for(i = 0;i <= 4;i++){
        printf("%d", a[i]);
    }
    printf("\n\n");
    }
    else if(choice == 3){
        pop();
    }
 }while(choice != 0);


 }

 void push(){

    if(top <= 3){
    int input;
    printf("Enter number to push: ");
    scanf("%d", &input);
    top = top + 1;
    a[top] = input;

    int i;
    printf("Current Stack:\n");
    for(i = 0;i <= 4;i++){
        printf("%d", a[i]);
    }
    printf("\n\n");
    }else{
    printf("Out of Bounds\n\n");
    exit(0);
    }
}

void pop(){
    if(top >= 0){ 
    printf("You just popped: ");
    printf("%d \n\n", a[top]);
    a[top] = 0;

    printf("Current Stack:\n");
    int i;
    for(i = 0;i <= 4;i++){
        printf("%d", a[i]);
    }
    printf("\n\n");
    top = top - 1;
    }else{
    printf("Out of Bounds\n\n");
    exit(0);
    }

 }
JP Dolocanog
  • 451
  • 3
  • 19
0
#include <iostream>

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<s-1){
top=top+1;
a[top]=x;
}
else
std::cout<<"overflow"<<"\n";
}

int pop(int a[]){
if (stack_empty(a)==true)
 std::cout<<"Underflow"<<"\n";
else{
 --top;
return a[top+1];
}
}

void display(int a[]){
  for(int i=0;i<=top;i++){
   std::cout<<a[i]<<" ";
}
}
int main()
{
   **initialise_top();**//this statement initialises top=-1
  int arr[7];
  //std::cout<<stack_empty(arr)<<"\n";
  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<<"\n";
  int out=pop(arr);
  std::cout<<**out**<<"\n";
  return 0;
}

1.In your program the value of top=1 when the first element 15 is inserted. due to this another value is shown for index 0. So to have top=0, call the function initialise_top(); in main function. 2.To display 3 instead of 1 use std::cout<<out<<"\n"; Modifications in the program are bold.

0

I have tried to improve my code. Please tell me if can improve.

#include <iostream>

#define max 1000
class Stack{
  int top;

 public:
   int a[max];
     Stack(){
      top=-1;
      }
      bool stack_empty();
      void push(int x);
      int pop();
      void display();
};

bool Stack::stack_empty(){
if(top==-1)
    return true;
else
    return false;
}

void Stack::push(int x){
  int s=max-1;
if(top<s){
top=top+1;
a[top]=x;
}
else
    std::cout<<"overflow"<<"\n";
}

int Stack::pop(){
if (stack_empty()==true)
    std::cout<<"Underflow"<<"\n";
else{
    --top;
    return a[top+1];
}
}

void Stack::display(){
for(int i=0;i<=top;i++){
    std::cout<<a[i]<<" ";
}
}

int main()
{
    Stack stack1;
    stack1.push(15);
    stack1.push(6);
    stack1.push(2);
    stack1.push(9);
    stack1.push(3);
    stack1.display();
    std::cout<<"\n";
    std::cout<<stack1.pop()<<"\n";
    stack1.display();
    return 0;
}
coder
  • 143
  • 10