+ 2
Output of this code.....
#include <iostream> using namespace std; class stk{ int top=-1; public: int a[100]; void push(int x); int pop(void); void display(void); }; void stk::push(int x) { if(top>100) cout<<"stack is overflow"; else if(top==-1) a[++top]=x; else a[++top]=x; } int stk::pop() { int s; if(top==-1) cout<<"Stack is under flow"; else{ s=a[top]; top--; } return s; } void stk::display(){ int j; for(j=0;j<=top;j++){ cout<<a[j]<<" "; } } int main() { stk k; int j,b; char l; int f[5]={12,45,32,67,98}; for(j=0;j<5;j++) k.push(f[j]); for(b=0;b<2;b++) k.pop(); k.display(); }
6 ответов
+ 2
here you go ...
https://code.sololearn.com/cnnIADf7S8KJ/?ref=app
+ 1
I know I'm questioning
+ 1
k -> object of class stk
k.push (f[j]) -> reference to function push() of class stk
Description:
top > 100 :- checks if index is beyond 100, if yes, stack overflow is displayed
else
increase the value of top (which becomes 0).
Element x is then stored in a[top (0)].
k.pop()
Explanation :- if top is greater than -1 (this checks if anything is stored in array a) , last element in array ' a ' is stored in ' s ', then the value stored in s is returned to main. Now here's the problem, as k.pop() is not assigned to any variable, return is useless in this function.
K.pop() does one thing though, it reduces the index (top) to 2.
k.display()
This function prints all the elements in array ' a ' till index 2 (3 elements)
That's it!
Hope this helps...
0
Copy that code, create a new online project in SoloLearn (private if needed), and paste the code, check the output.
You can also ty to download and use a C++ COMPILER.
0
click to edit that question , and select everything and copy.
0
https://play.google.com/store/apps/details?id=ru.iiec.cxxdroid
this is one example of C++ FREE COMPILER DOWNLOAD