-4
#include <stdio.h>
void printPattern ( int n );
int main(){
     int n =3;
     printPattern(n);
     return 0;
}
// for n = 3
// *
// ***
// *****
// 1-1
// 2-3
// 3-5
void printPattern ( int n ){

if (n ==1 ){
    printf("*\n");
    return;
}

printPattern(n-1);
for (int i = 0;i<(2*n-1);i++){
    printf("*");
}
printf("\n");
}

please explain to me this C language code I copied from a youtube tutorial it prints like this

*
***
*****

but how ?? I can't understand this recursion and please don't share another method to do this because I am trying to learn recursion in C but these types of projects shake my head!

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
Sonu
  • 3
  • 1
  • 3
  • 6
    Your line by line debugger will explain it for you. Learning to use a debugger is more important than learning how to write code. It's a shame that universities don't grasp that. – Bathsheba Aug 12 '21 at 09:57
  • What's unclear about it? – klutt Aug 12 '21 at 09:58
  • It is an example of *recursion* - a function calling itself. Please see [How Recursion works in C](https://stackoverflow.com/questions/5631447/how-recursion-works-in-c) and [What is recursion and when should I use it?](https://stackoverflow.com/questions/3021/what-is-recursion-and-when-should-i-use-it) – Weather Vane Aug 12 '21 at 10:01
  • 1
    @Bathsheba The problem here doesn't appear to be caused by a university but Youtube. Hint: searching for wisdom on Youtube is like searching for gold in the sewers. – Lundin Aug 12 '21 at 10:02
  • 1
    Learn C by working through a textbook/primer/tutorial that explains the language step-by-step, not by YouTube videos that do not. – Eric Postpischil Aug 12 '21 at 10:02
  • 1
    Additionally, learning how to use recursion is a huge waste of time. There almost exist no use cases in C programmer where recursion should be used. – Lundin Aug 12 '21 at 10:03
  • 2
    @Lundin; Forgive me: https://www.sciencemag.org/news/2015/01/sewage-sludge-could-contain-millions-dollars-worth-gold – Bathsheba Aug 12 '21 at 10:08
  • @Lundin Wouldn't say it's a huge waste of time. Mostly because it simply does not take so much time to learn, so matter if it's a waste or not, it's not huge ;) But I would also disagree with that premise. Recursion is a way of thinking that a programmer should know. But you should also learn when it's good and when it's bad. – klutt Aug 12 '21 at 10:18
  • @Lundin If for no other reason, you can always learn recursion to be able to change other peoples code to iterative when needed. – klutt Aug 12 '21 at 10:21
  • 3
    @klutt When you study something you sooner or later end up using it. What pisses me off the most is that schools spend lots of time teaching such useless things, but don't teach how to design programs or how to use a debugger. – Lundin Aug 12 '21 at 10:39
  • @Lundin Sure, but there ARE valid use cases for recursion in C. Sure, it's overused, but use cases exist. If I were to implement quicksort, I would only do it non-recursively if I had very strong reasons. But you're correct that schools should teach debugging more. – klutt Aug 12 '21 at 10:50
  • @Lundin Actually, I write recursive functions quite often, because it's mostly the case that it's more readable. And if needed, it's not a huge task to rewrite it. I often do it even if it's not proven to be needed. But it's often a very quick and easy way to write a function. – klutt Aug 12 '21 at 10:53
  • @klutt I have just started programming so that's why it is very hard for me to build logic I did understand this with the above comment and it is becoming very hard for me to visualize all these things and make programs like these myself. – Sonu Aug 12 '21 at 12:09

3 Answers3

0

Pattern of N - 1

*
***
...
***...***

Pattern of N

*                  // these
***                // form
...                // the
***...***          // pattern of N - 1
****...****

Pattern of N

<Pattern of N - 1>
****...****
pmg
  • 106,608
  • 13
  • 126
  • 198
0

The function printPattern is recursively defined. It has a base case for n equals 1, meaning that the recursion stops here. It has a recursive case for other values of n in which it first calls itself with n - 1 and then prints the asterisk character 2*n-1 times.

If we "unroll" the function for n equal to 3, we get:

  1. printPattern(3) -> call printPattern(2), print 5 asterisks afterwards.
  2. printPattern(2) -> call printPattern(1), print 3 asterisks afterwards.
  3. printPattern(1) -> base case, print 1 asterisk.
  4. The program now continues in the call at Step 2 and thus prints 3 asterisks.
  5. The program now continues in the call at Step 1 and thus prints 5 asterisks.
Yun
  • 3,056
  • 6
  • 9
  • 28
0

For n == 3, the execution looks like this:

printPattern( 3 ):
  if ( 3 == 1 ) // false, call printPattern( 3 - 1 )
  printPattern( 2 ):
    if ( 2 == 1 ) // false, call printPattern( 2 - 1 )
    printPattern( 1 ):
      if ( 1 == 1 )
        print "*\n"  //  first row
        return
    // back in printPattern( 2 )
    for( i = 0; i < 3; i++ ) // second row
      print "*"
    print "\n"
    return
  // back in printPattern( 3 )
  for ( i = 0; i < 5; i++ )  // third row
    print "*"  
  print "\n"
  return
John Bode
  • 119,563
  • 19
  • 122
  • 198
  • thanks for explaining and can you tell me how I build such logic so that i don't have to copy and make these types of programs myself – Sonu Aug 12 '21 at 12:10
  • Learning to program is learning to think. It takes time and effort. Follow a course step by step and do not progress until you understand what you are doing. – Cheatah Aug 12 '21 at 12:24
  • @GoDSoN_OP: Programming is a skill, and like all skills requires years of practice. You learn to code by writing code, making mistakes, fixing those mistakes, making new mistakes, fixing *those* mistakes, etc. I've been writing code in some form since the early 1980s (BASIC, Fortran, Ada, C, C++, Java), and I'm still learning new things. – John Bode Aug 12 '21 at 13:41