Counter in recursive function

-1

I have to perform a recursive procedure that prints this:

  

'\ n'   -----one   ----two   ---3   --4   -5

and for now I have this code that prints the numbers but not the "-".

#include <stdio.h> // sprintf, scanf, ungetc, stdin


void recurrencia(int n) {

if (n > 0) {

   recurrencia(n -1);

   printf("%d\n", n);


} else printf("\n");


}


int main()

{

recurrencia(5);

return 0;

}

The problem is that I can not add a new parameter to the procedure nor can I use the variable n as a counter.

So my question is: How to register each time I enter the same procedure recursively so I can add the amount of "-" corresponding to the "depth" with which I entered recursively?

    
asked by Edu 13.04.2018 в 22:10
source

1 answer

1

If you can not change the procedure recurrence (Int n) , but if you can use an auxiliary procedure, I would define one that takes control over the number of recursions so that I can print the characters' - ':

void auxiliar(int n, int m) {

if (n > 0) {

   auxiliar(n -1, m);
  for(int i = 0; i < m-(n-1); i++){
      printf("-");
  }
   printf("%d", n);


} else printf("\n");


}

void recurrencia(int n){
    auxiliar(n, n);
}

int main()
{
    recurrencia(5);

return 0;

}

Departures:

// n = 5

-----1----2---3--4-5

// n = 8

--------1-------2------3-----4----5---6--7-8
    
answered by 14.04.2018 / 01:52
source