-3

I have code that lists a combination of numbers at 6 length but it stops at 4-5-6-7-8-9. I need it to continue and stop when first column reaches 9. I have tried many ways but couldn't resolve the problem.

 static void Main()
   {
      Console.Write("n = ");
     var n = int.Parse(Console.ReadLine());

    Console.Write("k = ");
    var k = int.Parse(Console.ReadLine());

    foreach (var combo in Combinations(k, n))
    {
        Console.WriteLine(string.Join(", ", combo));
    }
    Console.ReadLine();
}

private static IEnumerable<int[]> Combinations(int k, int n)
{
    var result = new int[k];
    var stack = new Stack<int>();
    stack.Push(1);

    while (stack.Count > 0)
    {
        var index = stack.Count - 1;
        var value = stack.Pop();

        while (value <= n)
        {
            result[index++] = value++;
            stack.Push(value);
            if (index == k)
            {
                yield return result;
                break;
            }
        }
    }
}
SPQR
  • 514
  • 1
  • 7
  • 25
Nactrem
  • 45
  • 1
  • 5
  • 1
    Put a conditional breakpoint on your loop and then debug through it and see what happens. – xxbbcc Jan 08 '18 at 14:00
  • 2
    The mathematical term for this is called "Permutations". And knowing how to do it in math is a requirement to teach a computer to do it for you. it also helps finding core a guidelines for it. https://en.wikipedia.org/wiki/Permutation – Christopher Jan 08 '18 at 14:02
  • This code is working but stops at 4 5 6 7 8 9 because of reason that i don't know.By the way i need combination of the numbers. – Nactrem Jan 08 '18 at 14:04
  • I doubt you can do this with a loop. Looking at some basic examples (https://www.geeksforgeeks.org/write-a-c-program-to-print-all-permutations-of-a-given-string/), it seems they are all solved via recursion. There are few problems that simply can only be solved via recursion. – Christopher Jan 08 '18 at 14:07
  • @Christopher, `Stack` can be used to [replace recursion](https://stackoverflow.com/q/159590/1997232). But yes, using such technique and then trowing it into face of someone "it doesn't work" is a poor deal. The *"I have tried many ways"* sounds rather silly here. But there is always a chance for someone willing to debug. – Sinatr Jan 08 '18 at 14:13
  • 1
    Possible duplicate of [recursive Permutation of a 3 Digit Number](https://stackoverflow.com/questions/23185925/recursive-permutation-of-a-3-digit-number) – Cee McSharpface Jan 08 '18 at 14:41
  • @Steve I suspect the point is that all digits have to be distinct - no duplicates. Also, your answer includes zeros whereas the question excludes 0. Easy work-around for this, but still not negligible. – BlueMonkMN Jan 08 '18 at 15:55
  • @BlueMonkMN, fair point, I didn't grasp the problem. – Steve Jan 08 '18 at 16:06
  • 1
    "I wrote some buggy code and I can't find the bug" is not a question, and this isn't a service for finding the bugs you wrote. Do you have a *specific* question? – Eric Lippert Jan 08 '18 at 16:06
  • If what you need is a correct algorithm for producing permutations in C#, I give several here: https://ericlippert.com/2013/04/15/producing-permutations-part-one/ Note that of course if you are doing this for a school project, you must give credit where it is due and not plagiarize someone else's work. – Eric Lippert Jan 08 '18 at 16:10

1 Answers1

0

I suggest shortening your length (k) to 2 and reducing your range (n) to 3 so that you can get a better mental handle on what your program is doing and why it's missing values. I suspect that your condition while (stack.Count > 0) might be the wrong condition to have there. You might need to loop through a first digit 1 to n instead or in addition to this. Maybe you need to push 1 through N onto the stack at the beginning instead of just 1? In any case, you should be able to better understand the problem if you make the problem smaller. I don't want to rob you of that learning experience. :)

The output of your program with k=2 and n=3 is

1, 2
1, 3
2, 3

Hopefully that helps you understand what you're missing. You neglected to output 2, 1 before 2, 3. You can't always assume all values are only increasing like your code seems to do.

BlueMonkMN
  • 25,079
  • 9
  • 80
  • 146