-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwo_Sum.cpp
More file actions
96 lines (78 loc) · 2.05 KB
/
Two_Sum.cpp
File metadata and controls
96 lines (78 loc) · 2.05 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
#include <iostream>
// From leetcode
using namespace std;
void start_stop(bool &execute_code){
char user_decision;
cout << "Enter S to start or Q to quit" << endl;
cin >> user_decision;
while(user_decision != 'S' && user_decision != 's' && user_decision != 'Q' && user_decision != 'q')
{
cin.clear();
string dummy;
cin >> dummy;
cout << "ERROR!, invalid input was detected" << endl;
cout << "Please enter either \"S\" or \"Q\" only" << endl;
cin >> user_decision;
}
if (user_decision == 'S' || user_decision == 's')
{
execute_code = true;
}
else if (user_decision == 'Q' || user_decision == 'q')
{
execute_code = false;
}
}
int main(){
int counter = 0;
int values[counter];
int input;
bool found = false;
bool continue_running;
start_stop(continue_running);
while(continue_running){
cout << "Enter numbers into array" << endl;
while(cin >> input){
cout << "Enter number or letter to quit" << endl;
while(cin.fail()){
cin.clear();
string temp;
cin >> temp;
cin >> input;
}
if(input == 0){
break;
}
values[counter] = input;
counter++;
}
int i = 0;
int y = counter - 1;
char quit;
int target;
cout << "Enter target" << endl;
cin >> target;
while(i < counter || y < i){
if(values[i] + values[y] == target){
cout << "target found index: " << i << " and index: " << y;
found = true;
break;
}
else if(values[i] + values[y] > target){
y--;
}
else if(values[i] + values[y] < target){
i++;
}
}
if(!found){
cout << "Target not found" << endl;
}
cout << "Enter q to quit program" << endl;
cin >> quit;
if(quit == 'q'){
break;
}
}
return 0;
}