Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/adrianhenry15/cs50-bulbs
HarvardX CS50's Introduction To Computer Science
https://github.com/adrianhenry15/cs50-bulbs
Last synced: about 5 hours ago
JSON representation
HarvardX CS50's Introduction To Computer Science
- Host: GitHub
- URL: https://github.com/adrianhenry15/cs50-bulbs
- Owner: AdrianHenry15
- Created: 2023-07-18T14:32:32.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-07-18T15:31:46.000Z (over 1 year ago)
- Last Synced: 2023-07-18T15:56:17.250Z (over 1 year ago)
- Language: C
- Size: 3.91 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# HarvardX CS50's Introduction To Computer Science
## Week 2 Problem Set: Bulbs
### The Basics
The simplest *base* is base-1, or *unary*; to write a number, *N*, in base-1, we would simply write N consecutive `1`s. So the number `4` in base-1 would be written as `1111`, and the number `12` as `111111111111`. Think of it like counting on your fingers or tallying up a score with marks on a board.You might see why base-1 isn’t used much nowadays. (The numbers get rather long!) Instead, a common convention is base-10, or decimal. In base-10, each *digit* is multiplied by some power of 10, in order to represent larger numbers. For instance, 123 is short for 123 = 1 * 102 + 2 101 + 3 100.
.Changing base is as simple as changing the 10 above to a different number. For instance, if you wrote `123` in base-4, the number you’d really be writing is 123 = 1 * 42 + 2 41 + 3 40, which is equal to the decimal number 27.
Computers, though, use base-2, or *binary*. In binary, writing `123` would be a mistake, since binary numbers can only have `0`s and `1`s. But the process of figuring out exactly what decimal number a binary number stands for is exactly the same. For instance, the number `10101` in base-2 represents 1 * 24 + 0 * 23 + 1 * 22 + 0 * 21 + 1 * 20, which is equal to the decimal number 21.
### Encoding a Message
Light bulbs can only be on or off. In other words, light bulbs represent two possible states; either the bulb is on, or the bulb is off, just as binary numbers are either 1 or 0. We’ll have to find a way to encode text as a sequence of binary numbers.Let’s write a program called `bulbs` that takes a message and converts it to a set of bulbs that we could show to an unsuspecting audience. We’ll do it in two steps:
- The first step consists of turning the text into decimal numbers. Let’s say we want to encode the message `HI!`. Luckily, we already have a convention in place for how to do this, [ASCII](https://www.asciitable.com/). Notice that `H` is represented by the decimal number `72`, `I` is represented by `73`, and `!` is represented by `33`.
- The next step involves taking our decimal numbers (like `72`, `73`, and `33`) and converting them into equivalent binary numbers, which only use 0s and 1s. For the sake of having a consistent number of bits in each of our binary numbers, assume that each decimal is represented with 8 bits. `72` is `01001000`, `73` is `01001001`, and `33` is `00100001`.Lastly, we’ll interpret these binary numbers as instructions for the light bulbs on stage; 0 is off, 1 is on. (You’ll find that bulbs.c includes a print_bulb function that’s been implemented for you, which takes in a `0` or `1` and outputs emoji representing light bulbs.)
Here’s an example of how the completed program might work. Unlike the Sanders stage, we’ll print one byte per line for clarity.
```
# ./bulbs
Message: HI!
⚫🟡⚫⚫🟡⚫⚫⚫
⚫🟡⚫⚫🟡⚫⚫🟡
⚫⚫🟡⚫⚫⚫⚫🟡
```
To check our work, we can read a bulb that’s on (🟡) as a 1 and bulb that’s off (⚫) as a 0. Then HI! became
```
01001000
01001001
00100001
```
which is precisely what we’d expect.Another example:
```
# ./bulbs
Message: HI MOM
⚫🟡⚫⚫🟡⚫⚫⚫
⚫🟡⚫⚫🟡⚫⚫🟡
⚫⚫🟡⚫⚫⚫⚫⚫
⚫🟡⚫⚫🟡🟡⚫🟡
⚫🟡⚫⚫🟡🟡🟡🟡
⚫🟡⚫⚫🟡🟡⚫🟡
```
Notice that all characters are included in the lightbulb instructions, including nonalphabetical characters like spaces (00100000).
### Getting Started1. Make sure you have a compiler for C programs. Some popular compilers include GCC, Clang, and Microsoft Visual C++.
2. Clone the repo.
3. `cd` into the respective directory.
4. Compile the code `gcc -o caesar caesar.c`.
5. Start the program `./caesar`