# Homework Help: A recursion to arrange a stars ! it's difficult

1. Aug 21, 2010

### bella mason

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

2. Aug 21, 2010

### zhermes

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

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? :)

3. Aug 21, 2010

### rcgldr

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

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?

4. Aug 21, 2010

### rcgldr

Re: 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 (Text):

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

Last edited: Aug 21, 2010
5. Aug 21, 2010

### bella mason

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

yeah. I do think so. It's a nested. BTW, thanks for your advice. ;)

No, I don't think so... But I'll try. TQ ;)

Before or after the main function like what Mr.Snider had done ?

6. Aug 22, 2010

### DavidSnider

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

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)

Last edited: Aug 22, 2010
7. Aug 22, 2010

### rcgldr

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

I think the goal of this assignment is to use recursion instead of looping for "printing" a string of stars.

8. Aug 22, 2010

### rcgldr

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

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.

9. Aug 22, 2010

### bella mason

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

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 ?

10. Aug 22, 2010

### bella mason

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

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 ?