Beyond the First (logic) Gates: EdX 2021 week 1 (and more)
Well, that was a trip. Programming in C doesn’t seem to be especially complicated, as such – the concepts, once grasped, are quite straightforward – but it is definitely unintuitive. The silver lining is that finally coaxing the code into doing what I wanted it to do was intensely satisfying.
C is a language that doesn’t have a lot of frills. Its saving grace is apparently that it is very fast to execute, but a lot of things that I would have expected to be able to do with a very limited range of functions and statements were actually quite fiddly, and getting my side-project done this week (the next iteration of the character generator program) involved material not covered in the early course. This includes for example:
- Generating a pseudorandom number.
- Using a single ‘generate random number between X and Y’ function and then calling it whenever I wanted to generate a random number for a new integer.
- Making the execution pause in the right places until the user presses a key (I had to abandon an attempt to use Enter consistently because it kept remaining in the input buffer, which caused the next pause to input to fall through because the condition was already satisfied, which meant that the next three times I wanted a prompt, all three fell through because the condition was already satisfied. I ended up using “press Y to continue” throughout, which worked somehow). This is apparently called “event-based programming” and is somewhat difficult to do in C, involving mysterious functions called things like fflush that are either absolutely critical or a shortcut to formatting your C:/ drive, depending on which stackoverflow entry you believe.
- I wanted to build a library of paragraphs and print them when the right condition was fulfilled (i.e. baby’s first CYOA). It feels like I should have been able to set them up somewhere outside the “int main (void)” loop so that the program was less sensitive to the order of operations, but I couldn’t work out how in the time available so I ended up using a gajillion flags to determine what happens next. This feels, uh, sub-optimal.
On the other hand, some things were easier than expected
- Generating convincingly random Chinese names from a couple of string arrays. I’m sure there is an optimal number where it feels properly random, but five surnames and ten first names gave surprisingly decent randomness.
- Dealing with text. As expected, it’s much easier than in Scratch to get the kind of thing that I want printed on the screen in the kind of way that I want.
Trip
report: EdX 2021 week 1 content
The content
was much denser this week - the lecturer
described it as ‘drinking from the firehose’ and while I’m usually quite leery
of cliches, in this case it absolutely was. New concepts come thick and fast in
week 1, and are distinct enough that you’re usually not just building on what
was there before but absorbing genuinely new knowledge. Every thirty minutes or
so I found myself taking a break just to make sure that it had all sunk in.
The actual
exercises were simple but satisfying: getting some blocks to print in a
predictable pattern and writing a checksum. Probably mostly useful for the
muscle memory and to cement in the concepts, and to feel a little bit clever.
Scheduling
I’m a week
behind; by now I was supposed to have been at the end of week 3 (that is, the
fourth week) on my original plan whereas I’ve only finished up to the end of
week 1. This is partly due to a particularly full diary for work and family,
but mostly due to the side-project: the EdX week 1 content was done in a couple
of evenings and was covered well enough in the course content, but to get my
simple little story generator going ended up costing me hours spent working
through The C Programming Language and looking stuff up on stackoverflow
(and on Gitlab, where a C version of Colossal Cave Adventure has been
made available called “Open Adventure”. I’m not proficient enough to understand
yet how they solve for the things I’m trying to solve for, and it will be
interesting to look at that again once I know more).
Going
forward I’m going to have to decide whether it’s worth slowing down to do side
projects to get more directly usable experience, or whether I should just plough
ahead through the course to try to unlock more advanced concepts and better
tools for the task faster. Currently thinking I will keep up the side projects
for now, but if I lose another week or the course content becomes actually
difficult, I may revisit this.
Resources
The
C Programming Language – nice clear reference work written by the language’s authors, with
exercises to work through. I found this very helpful to work through along the course
content.
C
Programming for dummies – I’ve never loved the dummies’ guides series – something about the
sense of humour doesn’t quite work for me – but it was quite useful for working
out how to achieve a couple of specific results.
Gitlab: Open Adventure – looks clever, I look forward to
reading and understanding this one day.
Comments
Post a Comment