Goal
By the end of this lab you have written six small C programs and watched them pass real tests. The first three are warm-ups with one line to change each; the last three you write yourself from the building blocks of the week 3 page: do … while for input, nested for loops, if, casting, and the / and % operators. From this lab on we use Classroom 50: one click gives you a private repository, every push is graded within a minute or two, and your score appears on GitHub.
Steps
Join the course organization (once)
⏱ 1 minYou received an email from GitHub: “FiratUniversity-IJDP-SoftEng has invited you to join”. Open it and click Join, or go to github.com/FiratUniversity-IJDP-SoftEng and accept the banner at the top. Make sure you are signed in with the same GitHub account you used for Lab 1.
No email? Check spam, then tell the instructor your GitHub username.
Accept Lab 2 on Classroom 50
⏱ 2 min- Click the orange Accept lab lab02 button at the top of this page. Sign in with GitHub if asked and authorize Classroom 50.
- Click Accept assignment. A checklist runs for about 20 seconds while your repository is created.
- Click Open repository.
You now own a private repository called introduction-to-computer-science-lab02-yourusername. Only you and the instructor can see it.
Open it in Codespaces
⏱ 2–3 min the first timeOn your repository page: green Code button → Codespaces tab → Create codespace on main.
VS Code opens in your browser. The first start takes a couple of minutes while gcc, make, valgrind and the tests are installed; watch the terminal at the bottom until it shows a prompt ending in $.
Look around and run the checks once
⏱ 3 min| File | What it is |
|---|---|
hello.c, greet.c, bits.c | Parts A–C, warm-ups: each has a one-line TODO |
mario.c, calculator.c, cash.c | Parts D–F: you write them; the comment at the top of each file is the specification |
cs50.h, cs50.c | the CS50 library (get_int, get_string); do not edit |
Makefile | lets you type make mario instead of the long compiler command |
lab02.ipynb | playground notebook, not graded |
check.py | the friendly test runner |
tests/ | the tests, one file per part; the autograder runs exactly these |
In the terminal:
$ python3 check.py:| hello.c prints "hello, world"
expected 'hello, world\n', got 'hello, TODO\n'
...
0/14 checks passed Everything fails because nothing is done yet. Your job is to turn the list green, part by part.
Part A · hello.c
⏱ 3 minOpen hello.c. One word is wrong. Make it print exactly hello, world with a newline, then compile and run:
$ make hello$ ./hellohello, world If the compiler complains, read the first error; it names the line.
Part B · greet.c
⏱ 5 mingreet.c asks for a name with get_string and stores it in name. Complete the printf so that it prints hello, followed by the name and a newline. %s is the placeholder, and the value goes after the format string, separated by a comma, exactly like hello2.c in the lecture.
$ make greet$ ./greetWhat's your name? Firat
hello, Firat Part C · bits.c
⏱ 5 minbits.c asks for a number from 0 to 255 and loops over its 8 bits; bit is already 1 or 0. Replace the two ? so that it prints '1' when bit is 1 and '0' otherwise.
$ make bits$ ./bitsNumber (0–255): 73
01001001 That is last week’s bit widget, now in C.
Part D · mario.c
⏱ 20 minWrite a program that asks for a height from 1 to 8, re-prompting until it gets one, and prints a right-aligned pyramid:
$ make mario$ ./marioHeight: 4
#
##
###
#### Plan it before you type. Row 1 has three spaces and one #; row 2 has two spaces and two #; in general row i has height - i spaces and i hashes. So:
- A
do … whilethat asks forheightuntil it is between 1 and 8 (lecture:mario7.c). - An outer
forover the rows,ifrom 1 toheight. - Inside it, one
forthat printsheight - ispaces, then oneforthat printsihashes, thenprintf("\n").
Part E · calculator.c
⏱ 15 minRead two integers x and y and print five lines: the sum, the difference, the product, the quotient as a real number with two decimals, and the remainder. If y is 0, print the word undefined for the last two lines instead of dividing.
$ make calculator$ ./calculatorx: 7
y: 2
9
5
14
3.50
1 Two traps from the lecture: x / y with ints truncates, so cast first, (float) x / (float) y; and printf("%.2f\n", z) prints two decimals. Dividing by zero crashes a C program, so the if (y == 0) must come before the division.
Part F · cash.c
⏱ 15 minAsk for an amount of change in cents (re-prompt while it is negative) and print the minimum number of coins using quarters (25), dimes (10), nickels (5) and pennies (1).
$ make cash$ ./cashChange owed: 41
4 41 = 25 + 10 + 5 + 1. The strategy is greedy: always take the largest coin that still fits. Two ways to write it: a while (cents >= 25) loop per coin, or arithmetic: cents / 25 is how many quarters fit and cents % 25 is what is left. Both are correct; the second is four lines shorter. Try 0, 1 and 99 by hand before you trust the tests.
Submit
⏱ 2 minWhen python3 check.py shows 14/14 checks passed (or when you have done what you can), push. Every push is graded.
$ git add -A$ git commit -m "Lab 2"$ git push Prefer buttons? Source Control icon on the left of VS Code: type a message, click Commit, then Sync Changes.
About two minutes after the push:
- On your repository page, look under Releases on the right. The newest release shows your score out of 10 and a table of the six parts.
- Or on classroom50.org, open the assignment and click My submission.
Push as often as you like before Friday, October 9, 23:59; the last push before the deadline counts. Pushes up to five days late lose 10% per day.
How to submit
Points: hello 1, greet 1, bits 1, mario 3, calculator 2, cash 2. Each part is graded by its own test file, so a part you did not finish costs only its own points.
Other ways to work and submit
Codespaces is the easiest route, but it is not the only one. You can write your C anywhere and hand it in from the browser. Grading is exactly the same: every change that lands in your repository is graded automatically.
Where you can work
| Option | What to do |
|---|---|
| Your own computer | Install a C compiler: macOS xcode-select --install in Terminal; Windows: WSL then sudo apt install gcc make; Linux: sudo apt install gcc make. Download your repository (green Code button → Download ZIP), unzip, and in that folder compile each program with the library: gcc -o mario mario.c cs50.c then ./mario. If make is installed, make mario does the same. For the checks, install Python 3 and pip install pytest, then python3 check.py. |
| cs50.dev | Open cs50.dev. The CS50 library is preinstalled there, so you can create mario.c, paste your code, and run make mario and ./mario exactly as in the lecture. Copy your finished file back into your repository (below). |
| Google Colab | Open the lab notebook with the Open in Colab button below; a cell that starts with %%writefile mario.c saves your code, and !gcc -o mario mario.c cs50.c && ./mario compiles and runs it. Upload cs50.h and cs50.c from your repository into the Colab session first (folder icon on the left → upload). Programs that ask for input need echo 4 | ./mario instead of typing. |
| github.dev | On your repository page press the . key. A light VS Code opens in the browser for editing and committing (it cannot compile C). |
Submit from the browser (no terminal, no git)
When a program is ready in a file on your computer or in another editor, put it into your repository on github.com in one of two ways.
A. Paste into the file
- Open your repository
introduction-to-computer-science-lab02-yourusernameon github.com. - Click
mario.c, then the pencil icon ✎ (Edit this file) at the top right of the file. - Select everything (Ctrl + A, or ⌘ + A on a Mac) and paste your complete file.
- Click Commit changes…, write a short message such as
Lab 2 mario, keep Commit directly to the main branch selected, and click Commit changes. - Do the same for each file you changed.
B. Upload the files
- On your repository page click Add file → Upload files.
- Drag your
.cfiles from your computer into the box. Files with the same name replace the starter files; that is what you want. - Click Commit changes.
Check your grade the same way. About two minutes after each commit, a new Release with your score appears on the right of your repository page. Every commit counts as a submission, the last one before Friday 23:59 is the one that matters, and you may mix methods (for example, push from Codespaces on Monday and paste a fix from home on Wednesday).
Explore: C in a notebook
lab02.ipynb is not graded. It writes C files, compiles them and runs them cell by cell: types, printf placeholders, the bit loop explained, and integer overflow on purpose. Open it in your codespace (choose the Python kernel) or in Colab with the button below; nothing to install.
Lab 2 · Explore: C in a notebook
Read it here, or open it in Colab: it writes, compiles and runs C right in the browser.
Challenges (optional, not graded)
- Mario, two-sided. Print the pyramid with a mirror image and a two-space gap, as in the real game.
- Cash in Turkish lira. Coins of 1, 0.50, 0.25, 0.10 and 0.05 lira: read kuruş as an integer and keep greedy working.
- Calculator with a menu. Ask which operation the user wants with
get_charandif … else if, likeagree2.c.