Monday, March 18, 2019

CS - week 3 - Pointers

CS50 - week 3 - Pointers

lecture 4
This lecture is about pointers in C. Which is honestly a really confusing subject. The concept pointer as david explains it is easy enough: It points to where some value is stored in memory. But using it is another level of hard, especially as when using pointers you must also allocate memory to store stuff in and later free it.

After introducing pointers, David next explains that strings actually don't exist in C. it's a char*. A pointer that points to a series of chars in memory.

During the forthcoming problem set, image manipulation is the subject. So two more concepts are introduced: data persistence (files) and RGB pixels (values how red, green and blue a certain pixel must be).

problem set 4

WhoDunIt?
There's a message hidden in a an image. You start with some code to copy the file. Just adding some code to change one color out of three RGB trio to 0 or 255 eventually reveals the message. Not so hard.

Resize, easy
The objective here is to resize an image, multiplying by a natural number. You again start with the same code to copy the file. The BMP-files that get copied consist of two parts, a header and the pixels. During copying, the header must be changed slightly to reflect the new image size. The bitmap consists of lines of pixels, each lines must be a multiple of four pixels. If the numbers is not a multiple of four, some "padding" is included. This has to be accounted for after the resize. Each line of pixels has to be stored in memory and then outputted X times, where X is the multiplying factor. With padding added after, which is not necessary the same amount for the output as the input. Took just 4 hours.

Resize, hard
Same objective, except this time it can be multiplied by a rational number. So you have to account for the fact that it can shrink the image instead of grow. For shrinking, I first multiplied by the rational number times 10, then copy every tenth pixel. This was a difficult task that took 6 hours to complete.

Recover
You have one file full of deleted JPG files. The task is to recover all of them. The begin and end of these files have to be detected and their content copied and writed into a JPG file. this task was another 4 hours.

This entire problem set took 15 hours and was the longest Problem Set of them all.

No comments:

Post a Comment