## a recursion to arrange a stars ! it's difficult :(

hello ! :) my name is bella and i am still a student. i've instructed by my lecturer to create a simple program that will appear such as this output :

Enter number of star : 5(user will key in the data)

*
**
***
****
***** (result)
****
***
**
*

My question is I only can make the last five * to be appear and my coding is just like this.

#include <stdio.h>
#include <conio.h>

void recursion(int);
void recursion1(int);

int main()
{
int n;

printf("\nEnter number of star : ");
scanf("%d",&n);
printf("\n");
recursion(n);
recursion1(n);
printf("\n");

getch();
}

void recursion(int n)
{
int i;

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

}

else
{
for (i=0; i>n; i--)
printf("*");
printf("\n");
recursion(n-1);

}

}

void recursion1(int n)
{
int i;

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

}

else
{
for (i=0; i<n; i++)
printf("*");
printf("\n");
recursion1(n-1);

}

}

My lecture said the coding will have two calling function but what should I change from my first calling function ? I haven't sleep for a couple of days thinking about it and yet, I still didn't find it's answer. Could it be other way of coding to appear the same result ? Can someone help me and guide me ? :((

notes : it's only C languange. ;) TQ
 I think your optimal solution is something called "nested for loops," or a "nested recursion;" that is, a recursion inside another recursion. What can you come up with using that? :)
 Recognitions: Homework Help Would it be ok to have the main function have a first loop loop from 1 to n, then a second loop from n-1 to 1 and only call a recursive function to print out the stars?

Recognitions:
Homework Help

## a recursion to arrange a stars ! it's difficult :(

The examples here are not recursion. I think this is part of what you're supposed to be doing:

Code:
void recursiveprintstar(int n)
{
if(n >= 1)
{
printf("*");
recursiveprintstar(n-1);
}
}

 Quote by zhermes I think your optimal solution is something called "nested for loops," or a "nested recursion;" that is, a recursion inside another recursion. What can you come up with using that? :)
yeah. I do think so. It's a nested. BTW, thanks for your advice. ;)

 Quote by rcgldr Would it be ok to have the main function have a first loop loop from 1 to n, then a second loop from n-1 to 1 and only call a recursive function to print out the stars?
No, I don't think so... But I'll try. TQ ;)

 Quote by rcgldr The examples here are not recursion. I think this is part of what you're supposed to be doing: Code: void recursiveprintstar(int n) { if(n >= 1) { printf("*"); recursiveprintstar(n-1); } }
Before or after the main function like what Mr.Snider had done ?
 Recognitions: Gold Member Sorry for the full answer, that was a no-no for homework. Anyway, you can modify your first function by passing in all the variables you need for the subsequent recursive calls. Where the main is doesn't matter as long as the function signatures are predefined like you have in your original example. I don't know what rcgldr means by saying that the other examples are not recursive. They are. (Unless he means zhermes, nested for-loops are not recursion)

Recognitions:
Homework Help
 Quote by DavidSnider I don't know what rcgldr means by saying that the other examples are not recursive.
I think the goal of this assignment is to use recursion instead of looping for "printing" a string of stars.

Recognitions:
Homework Help
 Quote by rcgldr The examples here are not recursion. I think this is part of what you're supposed to be doing: Code: void recursiveprintstar(int n) { if(n >= 1) { printf("*"); recursiveprintstar(n-1); } }
 Quote by bella mason main function?
I was leaving that for you to figure out. main() will be calling recursiveprintstar() to print out a row of stars, then print out a "\n", for each row to be printed. There could also be 2 more recursive functions, one to print out rows of increasing number of stars and another to print out rows of decreasing number of stars.

 Quote by DavidSnider Sorry for the full answer, that was a no-no for homework. Anyway, you can modify your first function by passing in all the variables you need for the subsequent recursive calls. Where the main is doesn't matter as long as the function signatures are predefined like you have in your original example. I don't know what rcgldr means by saying that the other examples are not recursive. They are. (Unless he means zhermes, nested for-loops are not recursion)

Oh I see. So the problem occurs in my second calling function isn't it? Nested for-loops ? How is it going? There is a for inside a for ?

 Quote by DavidSnider Sorry for the full answer, that was a no-no for homework. Anyway, you can modify your first function by passing in all the variables you need for the subsequent recursive calls. Where the main is doesn't matter as long as the function signatures are predefined like you have in your original example. I don't know what rcgldr means by saying that the other examples are not recursive. They are. (Unless he means zhermes, nested for-loops are not recursion)

Oh I see. So the problem occurs in my second calling function isn't it? Nested for-loops ? How is it going? There is a for inside a for ?

 Tags c++, c++ assignment, c++help