-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstack-grows_up_or_down.cpp
More file actions
46 lines (38 loc) · 1.32 KB
/
stack-grows_up_or_down.cpp
File metadata and controls
46 lines (38 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/* Submitted By : DEEPAK YADAV
Question 1:
(STACK):On a given machine, how do you check whether the stack grows up or down?
*/
#include<iostream>
#include<cstdlib>
#include<stack>
using namespace std;
int main()
/*
objective: to check whether the stack grows up or down
input parameters:
ch1 : an integer that is to be pushed into the stack
ch2 : another integer that is to be pushed into the stack
output value:
addr1 : the address of the first input variable that is pushed into the stack
addr2 the address of the second input variable that is pushed into the stack
approach: the address of both the integers are compared to determine whether the stack grows up or down
*/
{
stack<int> st;
int ch1,ch2;
int *addr1,*addr2;
cout<<"Enter a number to push into the stack :";
cin>>ch1;
st.push(ch1);
addr1=&st.top();
cout<<"Enter another number to push into the stack:";
cin>>ch2;
st.push(ch2);
addr2=&st.top();
if(addr1<addr2)
cout<<"\nThe stack grows up..\n\n\tSince, address of 1st number is "<<addr1<<"\n\tWhereas, address of 2nd number is "<<addr2;
else if(addr2<addr1)
cout<<"\nThe stack grows down..\n\n\tSince, address of 1st number is "<<addr1<<"\n\tWhereas, address of 2nd number is "<<addr2;
else
cout<<"Error..";
}