-2

I have a homework, I should make a program that writes out '*' marks, one in the first line, two in the second line, and so on. The twist is, it must be made with a recursive method, and I can't really handle that.

    static void csillag(int i,int x = 1)
    {
        for (int z = 0; z < x; z++) Console.Write('*');
        x++;
        while (i > x)
        {
            Console.WriteLine();
            csillag(i);
        }
    }

This is the code I came up with, but it's not working for some reason, it keeps writing single * one in each line infinitely. I thought, maybe the "int x = 1" part keeps resetting x to 1 each time the method runs itself. Hope you can help, sorry for my unprofessional english.

  • 1
    Sack your teacher. This is a terrible use for recursion and doesn't in any way teach you the power of recursion. – Lee Taylor Jan 09 '17 at 23:48
  • 1
    First of all, you don't need `x` argument. Second - I suggest you to start with condition of recursion exit, which should look like `if (i == 10)` or whatever max number you need to draw. And I hope you will not get complete working solution here. There are lot of recursion samples in the network – Sergey Berezovskiy Jan 09 '17 at 23:48
  • The maximum number should be given be the user, so if I type in 11, it does it 11 times, you get it I think – Viktor Hart Jan 09 '17 at 23:52
  • `x` is 1 because every time you call the method you set it to 1 and never pass it a value different from that. Each call to the method has its own `x`, starting from scratch. – David Jan 09 '17 at 23:53
  • Is there any way I can get around that ? Declare it somewhere else in the program or something ? – Viktor Hart Jan 09 '17 at 23:54
  • Possible duplicate of [Using a single function recursively to display a triangle with a minimum and maximum value](http://stackoverflow.com/questions/24878842/using-a-single-function-recursively-to-display-a-triangle-with-a-minimum-and-max) – Prune Jan 10 '17 at 00:12

1 Answers1

1

First of all this is really a strange case to show you the real use case of the recursive functions, but the following solution works. Please do not copy and paste it and first read why it works and why it's not a good problem:

static void Main(string[] args)
{
    PrintStar(1, 10);
}

static void PrintStar(int n, int max)
{
    Console.WriteLine(new String('*', n));
    if (n == max)
        return;
    PrintStar(n + 1, max);
}

It works because I'm just implementing a simple loop by abusing the function calls; But, as you see PrintStar(n) and PrintStar(n+1) are totally irrelevant, so why you should replace just a simple loop with a function with extra unnecessary function calls?!

Note: If you got the point, now try to fix your solution.

Mohsen Kamrani
  • 7,177
  • 5
  • 42
  • 66
  • Thank you very much for the help, I'll look into the code first thing tomorrow, it's quite late now in my country. Also, if it's not a good use of recursive functions, then could you please tell me an example where it is ? – Viktor Hart Jan 10 '17 at 00:01
  • There is no reason to convert perfect [tail-recursive](http://stackoverflow.com/questions/33923/what-is-tail-recursion?rq=1) call to loop :) – Alexei Levenkov Jan 10 '17 at 00:32
  • Factorial is way too simple (and essentially equivalent of this sample if you use `new string('*', max - n)` and count down - just less visual) - recursive Fibonacci is by far more educational. – Alexei Levenkov Jan 10 '17 at 00:38
  • @AlexeiLevenkov I don't see any difference indeed, though I mentioned factorial is an elementary example. Both are very simple; and the only point is that they show how you break the problem and how you use intermediate results to achieve the final result. – Mohsen Kamrani Jan 10 '17 at 00:41