Monday, February 18, 2019

CS50 - week 1 - Intro to C

CS50 - week 1 - Intro to C

lecture 1
So, last week all the coding was in scratch. Which is a program made by MIT to visually represent code. It's also not that difficult to use (the latest version does seem to be difficult to embed, though). So in this lecture we (we as in, every student following this course) get introduced to the language of C. An old programming language designed in 1973 which is pretty much the opposite of scratch.

First the lecture shows a comparison between C and scratch, which makes C appear not to bad at first glance (, This isn't true at all at second, third... glance). It contains some nuisances such as a semicolon (";") after every line of text and the program throwing a fit if you don't. The basic loops and if/else conditionals get compared from scratch to C. Some background info on compilation was given (compilation, like I understand it, turns the lines of C text into 1's and 0's that the computer can read) and some more functions were shown. So, I still know next to nothing but the first C problem set shouldn't be that difficult, right?

problem set 1

So , the first problem set (the one from previous week counted as problem set 0) was rather manageable. There were 5 problems, only 3 were compulsory but I didn't feel the need to skip any as I feel practice is the best way to learn something. The problems are divided into two categories, for those less comfortable with coding (easier versions) and those more comfortable (harder versions).

Hello.C (easy version)
Have the program print 'Hello world!\n", as this was shown in the lecture, this was not difficult at all and only took 10 minutes. (I ofcourse forgot the semicolon so it threw an error, then I forgot the "\n" so it did not start a new line. Therefore, pretty much everything that could go wrong, did go wrong).

Hello, world\n

Mario.C (two versions, easier and harder)
the tl:dr pf problem set 1 said to listen to part of lecture 2, so I watched that in whole. Because an example was given during this lecture that was rather similar to these two tasks, the logic which should be typed out was immediately apparent to me (nested loops of # and spaces). So the major difficulty was wading through the 20 errors I got while making this. It only took 45 minutes 😌.
Then the harder version, which was only slightly more then the easy version. Also I could cannibalize the code of that version and only had to add one more loop in the nesting. I was a bit to "overzealous" in that I added spaces after # which weren't necessary (that is, it was wrong).

      n = 4

      #  # 
    ##  ##
  ###  ###
####  ####
The hard version separates the two pyramids of #, the easy version doesn't.

Cash.C ("easy")
Easy between quotes here, this wasn't that easy.
So, what was the task?
If I have an amount of cash, was is the least amount of coins of 0.01,0.05,0.1 and 0.02 required sum to that amount?
so if I would have 1.06 cash (of any valuta), the coins would be 5x0.2, 1x0.05 and 1x1.01 (I would need to have 7 coins minimum, I could also choose for 106 coins of 0.01, but thats unweildy).
The annoying problem here is that numbers that aren't integers in programming have the nasty habit of exhibiting rounding errors. Which can apperently be solved by multiplying and dividing by the amount of significant numbers. This problems was another hour.

Credit.C (hard)
So, given a number, tell me if it is one of the given credit cards or invalid. The requirements for the numbers are given : amount of numbers, the first number(s) as well as a stupid sum. The sum of every number plus every second number (so, these count twice) must be divisible by 10 (the last digit must be 0). This problem about as much time as the others here combined.

So the entire problem set was only 4 hours of work.

No comments:

Post a Comment