Please Help fix my C++ Program- Stack using 2d arrays I am suppose to be using a
ID: 3563754 • Letter: P
Question
Please Help fix my C++ Program- Stack using 2d arrays
I am suppose to be using a 2D array and have the functions :
Empty(),
Push()(adds a value at the top of the stack in a valid random space),
Top(),
Pop(),
Display()(show array index, data value and next array index)
I am having trouble making my array 2d and also adding a value to a random space, I also do not know how to go about the display function...Please please please help!
#include <iostream>
#include <conio.h>
#include <stdlib.h>
#include <cmath>
#include <ctime>
using namespace std;
class stack
{
int mystack[25], ttop;
public:
stack()
{
ttop=-1;
}
void push(int x)
{
if(ttop>25)
{
cout<<"The stack is full";
return;
}
mystack[++ttop]=x;
}
void top()
{
}
void pop()
{
if(ttop<0)
{
cout<<"The stack is empty";
return;
}
cout<<"The deleted element is:"<<mystack[ttop--];
}
void display()
{
if(ttop<0)
{
cout<<"The stack is empty"<<endl;
return;
}
for(int i=ttop; i>=0; i--)
{
cout<<mystack[i]<<" ";
}
cout<<endl;
}
};
int main()
{
char exit;
stack st;
st.push(1);
st.push(2);
st.push(3);
st.push(4);
st.push(5);
st.push(6);
st.push(7);
st.push(8);
st.push(9);
st.push(10);
st.display();
st.push(20);
st.display();
st.push(30);
st.display();
st.push(40);
st.display();
//st.top()//HELPPPP
//st.pop();
//st.display();
//st.top()//HELPPPP
//st.pop();
//st.display();
//st.top()//HELPPPP
//st.pop();
//st.display();
cin>>exit;
return 0;
};
Explanation / Answer
I hope you will understand that code easily that is done in so simple way plz rate n give me coin plzzzz
If you have any issue plz comment reply me plzzzzzzzzzzz
#include <iostream>
#include <iostream>
using namespace std;
class stack
{
int **mystack, ttoprow,ttopcol;
public:
stack(int row=0, int col=0)
{
ttoprow=row;
ttopcol=col;
mystack=new int*[ttoprow];
for(int i=0;i<ttopcol;i++)
{
mystack[i]=new int[ttopcol];
}
ttopcol=ttoprow=0;
}
bool IsEmpty()
{
if(ttoprow==0 && ttopcol==0)
{
return true;
}
return false;
}
bool IsFull()
{
if(ttoprow==25 && ttopcol==25)
{
return true;
}
return false;
}
void push(int x)
{
if(IsFull())
{
cout<<"The stack is full"<<endl;
return;
}
if(ttopcol==25)
{
ttoprow++;
ttopcol=0;
}
mystack[ttoprow][++ttopcol]=x;
}
int pop()
{
if(IsEmpty())
{
cout<<"The stack is empty";
return 0;
}
if(ttopcol==0)
{
--ttoprow;
ttopcol=25;
}
return mystack[ttoprow][--ttopcol];
}
int top()
{
if(IsEmpty())
{
cout<<"The stack is empty";
return 0;
}
return mystack[ttoprow][ttopcol];
}
void display()
{
if(IsEmpty())
{
cout<<"The stack is empty"<<endl;
return;
}
for(int i=ttoprow; i>=0; i--)
{
for(int j=ttopcol;j>0;j--)
{
cout<<mystack[i][j]<<endl;
}
}
cout<<endl;
}
};
int main()
{
stack st(25,25);
st.push(1);
st.push(2);
st.push(3);
st.push(4);
st.push(5);
st.push(6);
st.push(7);
st.push(8);
st.push(9);
st.push(10);
//st.display();
/* st.push(20);
st.display();
st.push(30);
st.display();
st.push(40);
st.display();*/
cout<<st.top()<<endl;
//st.pop();
//st.display();
//st.top()//HELPPPP
//st.pop();
//st.display();
//st.top()//HELPPPP
//st.pop();
//st.display();
}