| Thread Closed |
a recursion to arrange a stars ! it's difficult :( |
Share Thread | Thread Tools |
| Aug21-10, 03:10 PM | #1 |
|
|
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 |
| Aug21-10, 04:02 PM | #2 |
|
|
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? :)
|
| Aug21-10, 04:49 PM | #3 |
|
Recognitions:
|
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?
|
| Aug21-10, 10:13 PM | #4 |
|
Recognitions:
|
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);
}
}
|
| Aug21-10, 11:47 PM | #5 |
|
|
|
| Aug22-10, 02:13 AM | #6 |
|
|
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) |
| Aug22-10, 07:33 AM | #7 |
|
Recognitions:
|
|
| Aug22-10, 07:42 AM | #8 |
|
Recognitions:
|
|
| Aug22-10, 12:12 PM | #9 |
|
|
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 ? |
| Aug22-10, 12:20 PM | #10 |
|
|
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 ? |
| Thread Closed |
| Tags |
| c++, c++ assignment, c++help |
| Thread Tools | |
Similar Threads for: a recursion to arrange a stars ! it's difficult :(
|
||||
| Thread | Forum | Replies | ||
| Minimum distance between stars so we see them as two distinct stars | Introductory Physics Homework | 2 | ||
| We truly are the stuff of stars ... and stellar nurseries and supernovas and stars a | Astrophysics | 2 | ||
| Differential Equations - re-arrange? | Calculus & Beyond Homework | 4 | ||
| difficult recursion problem in C | Programming & Comp Sci | 5 | ||
| Help needed to re-arrange a formula | Introductory Physics Homework | 1 | ||