#Having trouble with logic

52 messages · Page 1 of 1 (latest)

spark current
#

Hi! I was doing some practice questions on recursion and i was having some trouble with this problem. I cant seem to figure out the logic behind it. I'm required to use up to four helper recursive functions and no loops
I have to make this pattern on the terminal:

mossy iglooBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

spark current
#

That's the problem, i can't figure out what logic to apply

#

Having trouble with logic

silk musk
#

And whether you've got experience with these problems where you have to print a pattern

spark current
#

i've made basic patterns like triangles and stuff using recursion

#

like this for example

#

my main issue is with patterns, i've got a good grasp of recursion otherwise

silk musk
#

Honestly, if the exercise didn't specify that it was about recursion I would have personally solved it with simple loops alone

spark current
#

yeah with loops it's easy, but that's the restriction

silk musk
#

But the challenge I guess is to break it down into simple chunks of code by looking for patterns, like probably needing if (x == 0 && y == 1 && in_window) { // print "R" }

spark current
#

that's what i'm having trouble figuring out

silk musk
#

So the thing to realize here is that when you say it's easy with loops, you are essentially saying it's easy with recursion:

void foo(void) {
    int i = 0;
    while (i < 3) {
        printf("%d\n", i);
        i++;
    }
}
void foo(int i) {
    if (i < 3) {
        printf("%d\n", i);
        foo(i + 1);
    }
}
#

So what I recommend is first writing the exercise with regular loops, and then rewriting that code so it uses recursion in a hamfisted way like this

#

Of course you'd ideally write the recursive version straight away, but I think this makes it significantly more gentle

spark current
#

that's true

#

i'll try doing it that way

silk musk
#

Let me know if you get stuck making it recursive

spark current
#

sure

#

thank you!

silk musk
#

No worries, and as a further tip, you'll want to have your loop version only use up to a single loop per function, since that makes it way easier to turn it recursive

#

As seen in the block of code I sent

spark current
#

sure

spark current
#

that would help

mossy iglooBOT
#

@spark current Has your question been resolved? If so, type !solved :)

silk musk
spark current
#

yeah

#

and a bit of the recursive approach too

silk musk
#

Well, the first thing that comes to mind looking at the picture you're expected to recreate is that there are four large squares, one in each of the corners

#

So I'd write something like:

int main(void) {
    print_square(-1, -1); // Top-left
    print_square(1, -1); // Top-right
    print_square(-1, 1); // Bottom-left
    print_square(1, 1); // Bottom-right
}
#

Or actually, I'd pass booleans here

#

print_square(bool left, bool top);

#

In order to make your life easier I also recommend making a global 2D char array, so that you can just insert characters into their final position without having to worry about the order things need to be printed

#

You can also do it without making that array global by passing it into every function, but that'd make the code significantly less readable imo

#

And if you only need to recreate that specific picture, I recommend hardcoding the width and height directly into the definition of the size like so:

constexpr size_t width = ?; 
constexpr size_t height = ?;
char map[height][width];
mossy iglooBOT
#

This question is being automatically marked as stale.
If your question has been answered, type !solved.
If your question is not answered feel free to bump the post or re-ask.
Take a look at !howto ask for tips on improving your question.

spark current
#

@silk musk did it lets gooooo

#

!solved

mossy iglooBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

silk musk
# spark current <@201096871341588480> did it lets gooooo

Good job! Did you end up doing it roughly the way I recommended, where you started out doing it without recursion where every single function contained just a single loop, and you then turned that into recursive functions later?

spark current
silk musk
#

I'm required to use up to four helper recursive functions and no loops

spark current
#

yeah i meant two recursive functions*

#

my bad

silk musk
#

good job

spark current
#

thanku

silk musk