#Having trouble with logic
52 messages · Page 1 of 1 (latest)
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.
What code do you have so far?
That's the problem, i can't figure out what logic to apply
Having trouble with logic
It'd help us to know what problems you've already solved with recursion, so that we don't overexplain nor underexplain
And whether you've got experience with these problems where you have to print a pattern
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
Honestly, if the exercise didn't specify that it was about recursion I would have personally solved it with simple loops alone
yeah with loops it's easy, but that's the restriction
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" }
that's what i'm having trouble figuring out
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
Let me know if you get stuck making it recursive
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
sure
can you give me some few tips on how you'd do it
that would help
@spark current Has your question been resolved? If so, type !solved :)
By "it" you mean solving the problem with loops?
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];
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.
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
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?
i did it in two loops, one handled the rows and one handled the columns, the columns one iterated first, reached the max value and then called a new line, increased the row and set column value back to zero
So did you use recursion?
I'm required to use up to four helper recursive functions and no loops
good job
thanku
I presume you used tons of checks like if (x == 42 && y == 69) { printf("R"); } for every row?