A recursion to arrange a stars it's difficult

  • Thread starter Thread starter bella mason
  • Start date Start date
  • Tags Tags
    Recursion Stars
AI Thread Summary
The discussion centers on a programming assignment requiring a C program to print a pattern of stars using recursion. The user, Bella, struggles with implementing the recursive functions correctly, particularly in generating both increasing and decreasing star patterns. Suggestions include using nested recursion and modifying the main function to call a recursive function for printing stars. Participants emphasize the importance of recursion over loops for this task, and there is confusion about whether certain examples provided are truly recursive. The conversation highlights the need for clarity in function design and the proper use of recursion to achieve the desired output.
bella mason
Messages
4
Reaction score
0
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
 
Physics news on Phys.org


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


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?
 


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);
    }
}
 
Last edited:


zhermes said:
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. ;)


rcgldr said:
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 ;)

rcgldr said:
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 ?
 


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:


DavidSnider said:
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.
 


rcgldr said:
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);
    }
}

bella mason said:
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.
 


DavidSnider said:
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 ?
 
  • #10


DavidSnider said:
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 ?
 

Similar threads

Replies
12
Views
2K
Replies
12
Views
2K
Replies
3
Views
1K
Replies
3
Views
1K
Replies
4
Views
1K
Replies
3
Views
1K
Replies
1
Views
10K
Replies
2
Views
2K
Back
Top