-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathconsecutivePairs.cpp
More file actions
169 lines (132 loc) · 3.36 KB
/
consecutivePairs.cpp
File metadata and controls
169 lines (132 loc) · 3.36 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
Program : Check if successive pair of numbers in the stack
is consecutive or not. (Q2 : Assignment 1)
Author : © Vipin Kumar
Created on : March 14, 2018 15:28 IST
*/
#include <iostream>
#include <cstdlib>
#include <conio.h>
#include <stack>
using namespace std;
bool isConsecutive (int number1, int number2) {
/*
To check if a the given pair of numbers are consecutive or not.
This function will tells that, whether two given numbers are consecutive
or not. The numbers can be consecutive from any side of number line i.e,
pairs can be increasing or decresing.
Parameters
----------
number1 : int - given first number.
number2 : int - given second number.
Returns
-------
true : bool - given pair is consecutive.
false : bool - given pair is not consecutive.
Description
-----------
isConsecutive function definition.
Approach
--------
We take the absolute difference of two given numbers of a pair, if it is
1, then they are consecutive and true is returned else false is returned.
Examples
--------
isConsecutive(2,3) returns True
isConsecutive(1,2) returns True
isConsecutive(-2,-3) returns True
isConsecutive(-1,-2) returns True
isConsecutive(2,4) returns False
isConsecutive(-2,0) returns False
*/
return abs (number1 - number2) == 1;
}
bool isSuccessivePairs (stack <int> s) {
/*
To check if successive pair of numbers in the stack is consecutive or not.
This function will tells that, whether the successive pairs in the stack
is consecutive or not. Since, we're checking the pairs, if there are
odd number of elements in the stack, we popped it out and check the remaining
even number of elements.
Parameters
----------
s : stack <int> - given Stack containing the numbers.
Returns
-------
true : bool - given pair is consecutive.
false : bool - given pair is not consecutive.
Description
-----------
isSuccessivePairs function definition.
Approach
--------
We popped the two elements at a time and check if they are consecutive using
the isConsecutive function. The loop runs until stack becomes empty.
If at any place, a non consecutive pair is encountered, we returned false,
else after completing the loop, we return true.
*/
int element1;
int element2;
while (!s.empty()) {
element1 = s.top ();
s.pop ();
element2 = s.top ();
s.pop ();
if (isConsecutive(element1,element2)) {
continue;
}
else {
return false;
}
}
return true;
}
int main () {
/*
Summary
-------
Create main() function.
Extended Summary
----------------
main() function will invoke the isSuccessivePairs function.
Parameters
----------
None
Returns
-------
0 : int - succesful termination of program.
Description
-----------
main() function definition.
Approach
--------
Invoke isSuccessivePairs function with argument `s`.
*/
system ("cls");
stack <int> s;
int count;
int element;
bool finalAnswer;
cout << "\t\tINPUT\n\n";
cout << "How many elements you want to enter: ";
cin >> count;
cout << "Start pushing elements: ";
for (int i = 0; i < count; i++) {
cin >> element;
s.push (element);
}
if (count % 2 != 0) {
s.pop ();
}
finalAnswer = isSuccessivePairs (s);
if (finalAnswer) {
cout << "Each succesive pair of numbers in stack is" \
" consecutive.";
}
else {
cout << "Each succesive pair of numbers in stack is" \
" NOT consecutive.";
}
getch ();
return 0;
}