CS 70

Working With C++ Code

Now it's time to check out (and actually compile) a C++ program!

The Hundred-Doors directory contains programs in multiple languages that all solve the same problem:

There are 100 doors in a row that are all initially closed.

You make 100 passes by the doors.

  • The first time through, you visit every door and toggle the door—if the door is closed, you open it, and if the door is open, you close it.
  • The second time, you only visit every second door (door #2, #4, #6, …), and toggle it.
  • The third time, you visit every third door (door #3, #6, #9, …)…
  • …and so on…
  • …until you only visit the 100th door.

Which doors are open at the end of this process?

(original problem seen on rosettacode.org)

Open a Terminal in the Hundred-Doors Directory

Inside VS Code, either right click (or control-click) on the Hundred-Doors folder in the sidebar and select Open in Integrated Terminal, or, if you already have a terminal open in VS Code and the prompt is cs70 Docker>, type cd Hundred-Doors.

Running Hundred Doors in Python

In doors-in-python.py, you'll find a Python program that solves the problem by simulating the opening and closing of doors according to the instructions.

Take a look at the code. Hopefully it will look pretty familiar/comfortable (note that irange is defined at the beginning just to make it slightly simpler to specify an inclusive range).

The program will print out all the doors that are open at the end of the program. You should be able to run this program by typing the following into the terminal:

python3 doors-in-python.py

If you haven't seen the hundred-doors problem before, you may find the output surprising and/or interesting!

Note that, in order to run the Python program, you are actually running the python3 program (the Python interpreter) and giving it the name of the file containing the code (doors-in-python.py). The interpreter reads the source code and executes the program.

Compiling Hundred Doors in C++

In doors-in-c++.cpp, you'll find a C++ program that does the same thing as the Python program.

Take a look at the code. Some of it will probably seem pretty familiar—you can probably see the same logical structure that you saw in the Python code! That said, there are probably some parts that look strange or confusing. But by the end of this course, everything in this program will make sense!

As with the Python example, to run our C++ code, we also have to use another program, but unlike the Python interpreter, the C++ compiler doesn't actually run the code. Instead, it converts the code from the human-readable source code into machine code that consists of instructions to the CPU (the main chip that performs computations for the computer).

In the coming weeks you'll learn a lot more about the compilation process. For now, just type the following “magic incantations” into VS Code's terminal.

First,

clang++ -c doors-in-c++.cpp

That command creates a file called doors-in-c++.o, which is an intermediate stage in the compilation process. Now we feed that file to the compiler by typing

clang++ -o doors-in-c++ doors-in-c++.o

which creates a file called doors-in-c++.

Running doors-in-c++

Note that we haven't seen the program print out any doors—we haven't actually run the program yet, just used the compiler to turn the source code into a program that can be run—an executable file. The file doors-in-c++ is that executable file.

Just like the program python3, or the program ls (which lists the files in a directory), or the program cd (which lets you change to a different directory), or the program VS Code (which you are probably using to view these files), the doors-in-c++ file contains instructions for the CPU and can be run directly, without any other program.

Programs are usually installed in special directories where they are easy for the system to find them. When you typed python3, the shell tried to find an executable file with that name in the usual places, found the first match, and ran it. To run doors-in-c++ we need to specify that the file is not in any of the usual places; instead it is right here. So we type

./doors-in-c++

The ./ here just means "the current directory" so ./doors-in-c++ means execute the file named doors-in-c++ in the current directory.

Because this C++ program does the same thing as the Python program, you should see the same output!

Hundred Doors in Other Languages

We've also provided programs that solve the same problem in other languages, including

  • Java (DoorsInJava.java)
  • Swift (doors-in-swift.swift)
  • C (doors-in-c.c)

Briefly explore those files for similarities and differences. Since they all produce exactly the same printed output, there isn't really any need to run them. (And only the C program can be run on our Docker system, because Java and Swift aren't installed in the image.)

Some Questions

Some of the written questions for this homework (in this Gradescope assignment) relate to this example.

Now might be a good time to answer them! (You can answer some questions, save your responses, and then go back and answer the rest later.)

To Complete This Part of the Assignment…

You'll know you're done with this part of the assignment when you've done all of the following:

(When logged in, completion status appears here.)