-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
71 lines (60 loc) · 1.4 KB
/
main.cpp
File metadata and controls
71 lines (60 loc) · 1.4 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
#include <iostream>
#include <cstdlib>
#include <ctime>
struct result{
int nums[2];
bool isValid = false;
};
int comp(const int *i, const int *j)
{
return *i - *j;
}
result* xseek(int* a, int len, int x)
{
auto res = new result;
int* b = a + --len;
do {
if((*a + *b) == x) {
res->nums[0] = *a;
res->nums[1] = *b;
res->isValid = true;
return res;
} else if((*a + *b) < x) {
a++;
} else if((*a + *b) > x) {
b--;
}
} while (a != b);
return res;
}
int main()
{
int x = 0;
int len;
std::cout << "Enter target value:" << std::endl;
std::cin >> x;
std::string input;
std::cout << "Enter array size (default = 5):" << std::endl;
std::cin >> len;
if(!len) {
len = 5;
}
int arr[len];
srand(time(nullptr));
for (int i = 0; i < len; i++) {
arr[i] = rand() % len + 1;
}
std::qsort(arr, len, sizeof(int), (int(*) (const void *, const void *)) comp);
int* a = arr;
auto res = xseek(a, len, x);
if(res->isValid) {
std::cout << res->nums[0] << ',' << res->nums[1] << std::endl;
} else {
std::cout << "Array does not contain target value." << std::endl;
}
std::cout << "Array was:" << std::endl;
for (int i = 0; i < len; i++) {
std::cout << arr[i] << std::endl;
}
return 0;
}