#include<iostream.h>
#include<conio.h>
void insert(int stack[],int &top,int n)
{
stack[top]=n;
top++;
}
void show(int stack[],int top)
{
while(top>=0)
{
cout<<" "<<stack[top];top--;
}
}
void main()
{
clrscr();
int n,stack[50],top=0;char op='y';
while(op=='y')
{
cout<<"\nEnter the number ";cin>>n;
insert(stack,top,n);
cout<<"\nThe stack is ";
show(stack,top);
cout<<"\nWant to enter more number ";cin>>op;
}
}
Even when I am not passing the stack array as reference in the insert function, the change occurs in that array. Why? And also while showing the output starting from 0 which I have not even entered.Please Help!!
Even when I am not passing the stack array as reference in the insert function, the change occurs in that array.
What do you mean?
And also while showing the output starting from 0 which I have not even entered.
What do you mean?
I would guess this is your problem:
1 2 3 4 5 6
while(top>=0)while(top>0) // Note: > instead of >=
{
top--; // Note: decrement before showing since it is 1 beyond the end.
cout<<" "<<stack[top];top--;
}