-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpermutations.cpp
More file actions
55 lines (46 loc) · 1.13 KB
/
Copy pathpermutations.cpp
File metadata and controls
55 lines (46 loc) · 1.13 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
// Michael Smith
// CS 361
// September 30, 2018
#include <iostream>
using namespace std;
void permutations(bool nArray[], int s, int k, int size, int results[]) {
// base case
if (k == 0) {
// Print out the results everytime we hit the bottom
for (int i = s - 1; i >= 0; i--) {
cout << results[i];
}
cout << ' ';
return;
}
for (int i = 0; i < size; i++) {
if (nArray[i] == false) {
nArray[i] = true;
results[k - 1] = i;
permutations(nArray, s, k - 1, size, results);
nArray[i] = false;
}
}
return;
}
int main(int argc, char* argv[]) {
// Variables for n and k. n
int n = atoi(argv[1]);
int k = atoi(argv[2]);
// Boolean array to track where we have been and an int array to print out the results
bool * nArray = new bool [n];
int * results = new int[k];
// set all values in the nArray to false
for (int i = 0; i < n; i++) {
nArray[i] = false;
}
// set all values in the results to 0
for (int i = 0; i < k; i++) {
results[i] = 0;
}
// call permutations
permutations(nArray, k, k, n, results);
cout << endl;
system("PAUSE");
return 0;
}