Hello cplusplusnightmare,
Welcome to the forum.
PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.
It may help if you look at your code this way:
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
|
#include <iostream>
using namespace std;
int i{}, n{}, k{}, st[20]{}; // <--- Do not count on these variables to be initialized. Also best not to use global variables.
void tipar()
{
for (i = 1; i <= n; i++)
cout << st[i] << " ";
cout << endl;
}
void init()
{
st[k] = 0;
}
int succesor()
{
if (st[k]<n)
{
k++;
return 1;
}
else return 0;
}
int valid()
{
for (i = 1; i <= k - 1; i++)
if (st[i] = st[k])
return 0;
return 1;
}
int solutie()
{
if (k == n)
return 1;
else return 0;
}
void backtr()
{
int ev{}, as{}; // <--- ALWAYS initialize your variables.
k = 1;
init();
while (k>0)
{
do
{
if (as == 1) ev = valid();
}
while (as && !ev);
{
if (as == 1)
{
if (solutie() == 1)
tipar();
else
{
k++; init();
}
}
else k--;
}
}
}
int main()
{
cout << "n= ";
cin >> n;
backtr();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue";
std::cin.get();
return 0;
}
|
As you can see when the {}s line up in the same column it makes the code easier to read and easier to see lines 65 and 77 limit the scope of what is inside the {}s and line 68 can only be reached if line 66 is true.
Like you I am not sure what the program does yet. I did get it to compile and run. Now all I have to do is figure out what it does and it it is working right. I will work on that shortly after lunch.
I do not know where you learned your style of coding at and although it will work what I showed you is easier to work with and read.
Hope that helps for now,
Andy