hey so you managed to reach the final exercise of the first chapter of the piece in congrats to you and we are ready to take on the last one the Lost Beast the final boss let's check it out we have this time to create a function that displays all different combinations of n numbers by sending core we already made a similar exercise but we had only three digits this time we have to deal with the variable number which is in the range of one to nine so the final output has to be something like that if n is equal to two we have this series of numbers in ascending order like the exercise we made and here we have the Prototype so this function won't return any value and takes an input which is uh the number of figures the number of digits okay as usual let's dive into the code here I have all my program as you can see it's kind of a long-ish because it is the trickiest one of course the reason it is long it's that it is plenty your comments as you can see because I want you to really understand what is going on this is the purpose of this these videos of course so first of all let's try to compile and run this program initially with the value of four so I want to run this program with four digits let's check it out so compile and run here it comes as you can see I have my output we have initially 0 1 2 and 3 which is of course the first combo by ascending order and and this program ends with 6789 which is the last configuration of my combo four digit maintaining the ascending property and in between of the two states we have all the middle values as you can see we jump here from 0 1 2 9 2 0 1 3 4 which of course is the next value if you want to maintain yes sending property and basically that's it now here I have in my main uh all the combos for all the other digits let's check them out very briefly so you can appreciate the output of this program in all its Glory here it comes I save and I quit the new program okay and then basically I compile and run like before here you see all the combos right when we give to the program the input 0 we get this prompt that states insert an integer from one to nine essentially when the input is one I have this combo of course we have zero one two to nine we have only one digit to deal with or and of course this is the way it is when we have two digits look at it we have this combo zero nine and then one two of course the ascending property we end with eight and nine and basically this is the rule for all the Combos and that's the way it is until we reach the final which is 10 elements the exercise doesn't require 10 values 10 digits but I did I did the same the principle is the same so who cares I did also for 10 symbols the last one required by the exercise is this one when we have nine digits and this is the combine okay so we have all the combos in a variable fashion depending on the input now we have to understand what the heck is going on now let's start again our program with only my print combination four let's decipher the print combination function in all its constituents in All Leads atomical parts we declare initially two arrays that I've called v stands for vector or values as you want and the other one is Vector values Max these two arrays contain 10 elements why tell elements well because the maximum value that the function has to handle is nine I've put 10 because I want to handle also 10 values but I could have used nine here would it would have been the same I use 10 because I want to handle all the symbols all the digits from 0 to 9 so 10 symbols here I declare an integer which is a flag and then I declare another integer that I've called a base later you will understand what this means then an if statement if n is less or equal zero or n is Major not equal only major to 10 but it's going to happen well I just write the prompt you remember the prompt that states insert an integer in this range as you can see I have my right function here with the buffer which is a string constant and the number of bytes I wanna flash in the standard output from this buffer and then basically the function ends so if I have an input to the function which is not in the range 1 to 10 the function aborts so I have this prompt that is going to be flushed into the standard output and everything ends if it is not the case what is going to happen so if I have a number which is in the range 1 to 10. I do this for Loop of course you cannot use the for Loop in uh uh in the PC but you can change the for Loop in a while loop very easily so I'm sure you can do this here I've used the for Loop because it is more intelligible it is more compact easy to understand and you are here just to understand how the algorithm works so what do I do for this amount of times so for n times n is the value that I get as an input so in this case it's going to be four four four times what do you do you put in the array in position I so in the first Loop is going to be zero the value I plus 48 okay so basically I initialize my array with the values with the Char values from 0 to n minus one zero one two and three right this is pretty easy this 48 you understood is just a way to change an integer to a chart so it is pretty straightforward so I have my array V that has the first four elements zero one two and three then I initialize my array Max with this expression let's try to decipher it I have 10 minus n what is what is 10 minus n well it is 10 minus 4 so it's going to be 6 plus I which in the first Loop is going to be 0 plus 48 so this expression in the first Loop is going to result in the Char six in the second Loop in the Char seven then Char 8 then Char 9. do you understand what it is well these two arrays are the initial state that I wanna flush out in the standard output and the final State because I have six seven eight nine now we are working with the value four so in this specific case is six seven eight nine so this is the maximum value that I can reach at every level 6 7 8 9. let's understand it better the first array as you can see is this one zero one two and three so the initial state of my series of my Combos and V Max is this state here six seven eight nine so I have initial and final States and in between there is all the rest that we are gonna check how it works okay by now you understood what is going on so long story short I create my initial and final State and I stock inside my true arrays and I call V and V Max then what do I do well I just print my array I just print my array with a subroutine that I created later you will see I know I'm sure that at this level the array is already in ascending order because I have this for Loop that every time increase I plus one so I have 0 1 2 and 3 at the first iteration and that's for sure okay so I immediately bring into my array I repeat myself and then we jump into the real myth of this algorithm let's check it properly here I say while V in position 0 is different than V Max in position zero so the maximum value that these V in position 0 can reach has to be different to Value Max in position zero Okay so until this condition is not matched but it's going to happen the idea behind my algorithm is pretty simple I have my array with all the values and every time I search for the value that has not reached the maximum value let's try to understand it better I say flag the value flag is equal to n minus 1. in our case n is 4 so n minus 1 is 3. so what is this flag well this flag is the last position of the arrays this value is three of course and this is the last value of my array specific in this case or four digits then I say while the value which is in the position of my array V is equal to the value was found in the same position in the array V Max what do you do you decrease the flag try to understand this code very well so you have the flag which initially is in position three I assume that the value that I have to change is in the last position of my array this is going to happen for every iteration then I say is this value already in its maximum value is I can see thanks to the V Max array right if this value is equal to its maximum configuration what do you do you just decrease the flag so you decrease the position that you're gonna check and you do this until you find the value that you want to change so you find the value that is not in its maximum State you understood perfectly I'm sure so let me repeat with these three lines I search my value Sentinel so the value that is not already in its maximum configuration perfect then I say okay plug has to be in the position of my Sentinel now I reached a value that I need to change what do I do I say in my array via with all the values in the position flag so where there is the value that I have to change make an increase by one here is written increase by one the value which is selectioned by the flag okay and then what do I do I save this value in my base so I save the increased value of my Sentinel and then what do I do well I say the value on the right of my Sentinel has to be in ascending order I need to maintain the ascending property of all the values which are on the right of my Sentinel try to visualize this and I'm sure you will understand so I say while the flag is minor to n and are equal to U in our hypothetical cases for so until the flag is maximum three namely the last position of my array what do you do you increase all the values that you find if I say first of all plus plus base so I increase by one by base base is equal to the value of my Sentinel then I put this value where do I put in my array V in the position flag plus one you can see here there is a plus plus which is a prefix increase so I put the value refreshed in my position exactly after the Sentinel this is all there is in this code in these two lines and then again I print my array because I'm sure I add my assigning property maintained and basically that's it at the following iteration I say I've reached already my final set namely the position 0 is is equal to the value Max in position zero no it is not so by the way I start from the beginning I restart from the last position of my array then I say search please search the value that I have increase when you find it just increase it and save this value then set all the following values after the Sentinel to plus one at every step and then just print the array that's basically it for the algorithm it's not very simple I think but spend some time try to really visualize this algorithm try to understand it that's all there is here I have some theory behind the this algorithm that we're gonna understand together the first combination as all digits set to their minimum value starting at zero for the leftmost digits for four digits so we have zero one two and three we are here here I create my array with 0 1 2 and 3. then I say the last combination has all digits set to their maximum value starting at 10 minus n in our case 10 minus 4 is equal to 6 for an end digit number so we have six seven eight nine for this array we are here here I create this true arrays B and V Max initial State final State then we have the combinations in between are found by incrementing the previous combination this involves a search from right to left looking for the rightmost digit that is not at X maximum value look careful with this you have zero five eight nine this five here is the rightmost digit that can be incremented you search you start from the very big the very last value which is in position three nine is this value at its maximum value yes it is is 8 at X maximum value yes of course is 5 at X machine value no it is not here I have seven as a maximum value so what do I do for code comparison we are here so I search my value I search my five which is in position one in this case and I say okay I found my Sentinel position then I form the next combination by incrementing the digit and setting all digits to its right to one more than the previous digit so this is what I do I increment the 5 to the 6 and then I set all the value next to him to one more so 7 and 8 and that's basically it and this is all there is for this algorithm essentially This is the part when I increase by one the current position and then I increase all the subsequent digits one plus one plus two plus three plus four and so forth respecting to The Sentinel and then I just print my array easy peasy was it difficult I don't know you tell me let's check it out my print array function I have here my Printery function that takes a pointer to char in this case I can write in this fashion I can write also like this because when you pass an array to a function it it is converted to a pointer never mind you don't have to understand it now then I have my size of the array and I have a flag which is last then you will see what it means add the color integer I then I say are we in the last position namely is the value in position 0 equal to the last value the last value is 6 or over four right we have B Max in position zero which is six so I say is the value in position zero equal to 6 in my case well in that case you you write all the elements uh inside the array and then you just write a point and a new line if we are not in the last position that I know because we skip this if statement what do you do you just write all the elements in my vector and then you print a comma and a space well that's it my friend you complete it let's try to rerun my program with all the combination so you can appreciate it once more with words so we have compile and run here it comes all the combos so we really we start with the first dates in our combination for four we handle with the last combination which is six seven eight nine as you can see here the six is a value that I use like a flag because when when I have this value I know that this is the last combination if I want to maintain the ascending program okay it works for every values I repeat to you till 10 of course it works also with 10 values even though the exercise doesn't require it for the 10 of course I have only one combo that maintain this any property I have all the values from zero to nine and that's it