milon

@milon@lemm.ee

This profile is from a federated server and may be incomplete. Browse more on the original instance.

milon,

It’s supposed to be a pyramid but not my code. It’s an example of a recursive function from a CS50 lecture and I’m just trying to understand how the code works line by line.

milon,

Yep

milon,

Why does the for loop return when it hits the end of the function? Isn’t the recursive portion already completed in draw(n - 1)? The rest of it is just normal non-recursive code if I understand it correctly.

milon,

Right. I was aware it was recursion as stated in the title of my post. I had two questions specific to where the for loop returns after printing #.

milon,

Thanks. I did see that. I have a general understanding of how recursion works I think where the function calls itself again and again but I don’t get why the code (for loop) below the draw(n - 1) is recursive.

milon,

I see. I guess my understanding was that the recursion was over after the recursive call, but it’s actually for all the code in draw().

milon,

Gotcha. Thanks for the explanation.

milon,

Yes - I finally caught that part about n as it’s just moving in reverse so it gets decremented. Now I’m not sure about i. In the debugger when the program gets to the for loop both n and i are equal to 1. The n I understand but i?

milon,

Yes, that helps. Thanks. I see now how n goes from 1 to 2 to 3…etc. Now not so sure how i = 1 when the for loop starts.

Why does isalpha() fail to identify an alphabetic character?

This is in C language. When I call rotate() in main, the function returns false for isalpha() even though the string entered for plaintext uses alphabetic characters. Perhaps it’s identifying an alphabetic character by its ASCII value (‘A’ = 65)? I tried to test that out and used (char) with the letter variable in rotate()...

milon,

Sorry. It’s in C. Updated post. Yes those are titles. I just included the relevant portions rather than the entire code.

milon, (edited )

Ah ha! Yes, I did check the docs but I think I just glanced over that portion. Be more careful next time. Now that I took another look at the other ctype.h functions, they all return 1 or 0. I think I confused equivalent python built-in functions as those evaluated to true/false. The < is a less than sign but it seems it doesn’t render correctly on Lemmy.

milon,

Good point!

milon,

Ah I see. I had a bad habit of using else if statements instead of else statements because I thought else if could be better in seeing the condition it’s testing for so it was clearer. I get the logic is actually different now.

  • All
  • Subscribed
  • Moderated
  • Favorites
  • JUstTest
  • mdbf
  • ngwrru68w68
  • modclub
  • magazineikmin
  • thenastyranch
  • rosin
  • khanakhh
  • InstantRegret
  • Youngstown
  • slotface
  • Durango
  • kavyap
  • DreamBathrooms
  • megavids
  • GTA5RPClips
  • tacticalgear
  • normalnudes
  • tester
  • osvaldo12
  • everett
  • cubers
  • ethstaker
  • anitta
  • provamag3
  • Leos
  • cisconetworking
  • lostlight
  • All magazines