A recursion to arrange a stars it's difficult

In summary, the conversation is about a student named Bella who is struggling to create a simple program that prints out a specified number of stars in a certain pattern using recursion in C language. The conversation includes advice from others on how to approach the problem and make use of nested recursion or nested for loops. The main issue seems to be in the second calling function and how to properly pass variables for subsequent recursive calls.
  • #1
bella mason
4
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
  • #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? :)
 
  • #3


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


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:
  • #5


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 ?
 
  • #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)
 
Last edited:
  • #7


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.
 
  • #8


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.
 
  • #9


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 ?
 

1. How does recursion work in arranging stars?

Recursion is a programming technique in which a function calls itself repeatedly until a certain condition is met. In the case of arranging stars, the function would call itself to place one star, then call itself again to place another star next to it, and so on until all stars are arranged.

2. What makes arranging stars using recursion difficult?

The difficulty in arranging stars using recursion lies in understanding the base case, or the condition that stops the function from calling itself. Without a proper base case, the function would continue to call itself infinitely, resulting in an error.

3. Can recursion be used for other tasks besides arranging stars?

Yes, recursion can be used for a variety of tasks, such as searching and sorting algorithms, tree data structures, and mathematical problems. It is a powerful tool for solving problems that have repeated subproblems.

4. Are there any advantages to using recursion for arranging stars?

One advantage of using recursion for arranging stars is that it can lead to more concise and elegant code compared to using traditional iterative methods. It also allows for better visualization and understanding of the problem.

5. What are some common mistakes to avoid when using recursion for arranging stars?

Some common mistakes to avoid when using recursion for arranging stars include not properly defining the base case, not handling edge cases or invalid input, and not considering the time and space complexity of the recursive solution. It is important to carefully plan and test the recursive function before implementing it.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
19
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
669
  • Engineering and Comp Sci Homework Help
Replies
3
Views
754
  • Engineering and Comp Sci Homework Help
Replies
3
Views
883
  • Engineering and Comp Sci Homework Help
Replies
4
Views
926
  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
Back
Top