Goal
Three programs, each one a step deeper into this week’s lecture. In scores.c you put numbers into an array and hand the array to functions. In readability.c you walk a string character by character and turn what you count into a school grade. In caesar.c you take a key from the command line, check it the way a real tool would, and encrypt text. Every part is scaffolded: the prototypes are written, the specification sits in the comment at the top of each file, and the tests tell you exactly what is expected.
Steps
Accept Lab 3 on Classroom 50
⏱ 2 minClick the orange Accept lab lab03 button at the top of this page, then Accept assignment, wait for the checklist, and click Open repository. Your repository is introduction-to-computer-science-lab03-yourusername.
Still not a member of the organization? Accept the invitation at github.com/FiratUniversity-IJDP-SoftEng first; the setup guide has the details.
Open it in Codespaces and run the checks once
⏱ 3 minGreen Code button → Codespaces → Create codespace on main. When the terminal shows a $ prompt:
$ python3 check.py:| scores.c: 72, 73, 33 → Min 33, Max 73, Average 59.33
got 'How many scores? Score: Score: Score: '
...
0/17 checks passed | File | What it is |
|---|---|
scores.c, readability.c, caesar.c | Parts A–C; the comment at the top of each is the specification |
cs50.h, cs50.c | the CS50 library; do not edit |
Makefile | make scores, make readability, make caesar |
lab03.ipynb | playground notebook, not graded |
check.py, tests/ | the checks, one test file per part |
Part A · scores.c (3 points)
⏱ 20 minAsk how many scores there are (1 to 10, re-prompt otherwise), read that many integers into an array, and print the minimum, the maximum and the average with two decimals:
$ make scores$ ./scoresHow many scores? 3
Score: 72
Score: 73
Score: 33
Min: 33
Max: 73
Average: 59.33 The three functions are declared for you: lowest, highest and average, each taking the length and the array, exactly like average(int length, int array[]) in the lecture’s scores5.c. Fill them in, then complete main.
Two things to notice. First, int scores[n]; after reading n is allowed in C: the size does not have to be a constant. Second, the smallest value in an array is found by assuming the first element is the smallest and then letting every other element challenge it; the same with the largest.
Part B · readability.c (3 points)
⏱ 25 minRead a text and print the U.S. school grade needed to read it, with the Coleman–Liau index:
index = 0.0588 × L − 0.296 × S − 15.8where L is the average number of letters per 100 words and S the average number of sentences per 100 words. Round the index to the nearest whole number; print Before Grade 1 if it is below 1, Grade 16+ if it is 16 or more, otherwise Grade N.
$ make readability$ ./readabilityText: Congratulations! Today is your day. You're off to Great Places! You're off and away!
Grade 3 Counting rules, the same as CS50’s: a letter is anything for which isalpha is true; words are separated by single spaces, so the number of words is the number of spaces plus one; a sentence ends with ., ! or ?. Write the three counting functions first and test each with a printf before you compute the index. For the example above you should count 65 letters, 14 words and 4 sentences.
Part C · caesar.c (4 points)
⏱ 25 minA Caesar cipher whose key comes from the command line:
$ make caesar$ ./caesar 13plaintext: HELLO, world
ciphertext: URYYB, jbeyq The rules, in the order you should implement them:
- Validate. Exactly one argument, made only of digits. Otherwise print
Usage: ./caesar keyandreturn 1.only_digitsis declared for you;isdigitfrom<ctype.h>checks one character. Test it before anything else:./caesar,./caesar 1 2,./caesar abcmust all print the usage line, andecho $?must show1. - Convert the key with
atoi(argv[1])from<stdlib.h>. - Read the plaintext with
get_string("plaintext: "), printciphertext:, then every character passed throughrotate. - Rotate. An uppercase letter’s position is
c - 'A'; add the key, take% 26, add'A'back. The same with'a'for lowercase. Anything that is not a letter is returned unchanged. Keys larger than 26 must work;% 26takes care of that.
$ ./caesar 3$ ./caesar 27plaintext: xyz XYZ
ciphertext: abc ABC
plaintext: abc
ciphertext: bcd Encrypt something with 13, then encrypt the result with 13 again. You get the original back; ROT13 is its own inverse, and the notebook below shows why.
Submit
⏱ 2 minWhen python3 check.py is green (or when you have done what you can):
$ git add -A$ git commit -m "Lab 3"$ git push About two minutes later your score appears under Releases in your repository and on classroom50.org. Push as often as you like before Friday, October 16, 23:59; the last push before the deadline counts, and late pushes lose 10% per day for up to five days.
How to submit
Points: scores 3, readability 3, caesar 4. Each part has its own test file, so an unfinished part costs only its own points. The other ways to work and to submit from the browser are the same as in Lab 2: remember that cs50.h and cs50.c must travel with your .c files, and that readability.c needs -lm (the math library) when you compile it by hand: gcc -o readability readability.c cs50.c -lm.
Explore: a string, byte by byte
lab03.ipynb is not graded. It prints the four bytes of "HI!" including the zero you never typed, does Caesar arithmetic on characters, and then deletes a string’s terminator to show what undefined behavior looks like. Open it in your codespace or in Colab.
Lab 3 · Explore: a string, byte by byte
Read it here, or open it in Colab: it writes, compiles and runs C right in the browser.
Challenges (optional, not graded)
- Decrypt. Add a second argument,
./caesar 13 -d, that shifts backwards. Careful with negative remainders in C. - Vigenère. Instead of one key, a keyword:
./vigenere lemonshifts the first letter byl, the second bye, and so on, skipping non-letters. - Reading levels in Turkish. Coleman–Liau was tuned for English. Run
readabilityon a Turkish paragraph and think about why the grade is off: what counts as a sentence, and how long Turkish words are.