1. Week 0 Thursday: Getting Started
Table of Contents | 2. Week 1 Tuesday: Errors ≫Welcome to PIC 10A! First thing’s first, make sure you have an integrated development environment (IDE) set up. This is a program that lets you edit, compile, and run code, basically everything you’ll need to do in this class, all from one place. If you’re on Windows, please install Microsoft Visual Studio Community 2022 (you’ll have to scroll down a bit). If you’re on Mac, please install XCode. If you’re using another operating system like ChromeOS, you may use an online IDE of your choosing, e.g. OnlineGDB.
When installing Visual Studio, make sure you’re also installing the “Desktop Development with C++” thingie under “workloads”. I don’t have access to a Mac, so hopefully XCode comes with C++ by default.
In both Visual Studio and XCode, you’ll need to create a new project every time you want to write a new program. One project per program! Otherwise things will stop working.
On Visual Studio, follow these instructions on the PIC website. Select New Project > Console App
, then choose a project name and location.
On XCode, create a new project by selecting File > New > Project...
, then choosing “MacOS” and “Console Application” on the popup menu. Choose a location to store the file, and make sure the language is C++! The default language is sometimes set to Swift.
Remark 1. Practise Good File Organisation
Your code is always stored in a file somewhere on your computer, and later on, we’ll be writing complex programs containing code across multiple files. These are in turn stored in folders, alongside additional information your IDE needs to organise your projects. Make sure you name your projects something sensible and keep them all in one folder throughout the duration of this quarter — something like PIC10A
on your desktop is great. Even better, you should make subdirectories for homework, discussions, and lecture.
Since this discussion takes place before the very first lecture, there’s two fundamental questions I’d like you to consider.
What is programming?
I think the very first thing that comes to mind is the action of writing code, or “coding”. But saying that programming and coding are the same thing is akin to saying that teaching and lecturing are the same thing. In the latter, lecturing is a part of teaching, perhaps the most notable part. But outside the classroom, educators must prepare lesson plans, write exams, grade coursework, handle disciplinary conflicts, etc., and these are all still a part of “teaching”.
In much the same way, the action of coding may be the image we have in mind when someone says the word “programming”, but there is a lot more that goes on outside the text editor. One must be able to…
- design and plan code, especially when multiple programs are interacting with each other;
- actually write said code (this is “coding”);
- read and use other people’s code and documentation, or make one’s own code accessible to collaborators;
- test programs to ensure they work correctly;
- squash bugs and fix mistakes, sometimes (unfortunately) in someone else’s code.
I’m probably forgetting a few things here, but the point remains: each of these are elements of “coding”, yet only one of them involves actively writing code.
The point I’m trying to make here is that this class is called “Introduction to Programming”, not “Introduction to Coding”. It’s natural to focus on the coding aspect of programming (indeed, lectures and discussions often do), but the other skills involved are at least as important.
How does C++ code become a computer program?
After installing Visual Studio or XCode and creating a C++ project, you’ll likely be faced with the following default program:
1#include <iostream>
2
3using namespace std;
4
5int main() {
6 cout << "Hello world!" << endl;
7
8 return 0;
9}
If you press the play button, you’ll see a little window that says Hello world!
. But how does the computer know what to do by reading the code you wrote? One can probably guess that the word “cout
” means “print this to the screen”, but the other 5 nonempty lines of code don’t have an obvious purpose.
You’ll learn more about what the words in this program mean during the first lecture, but I’d like to address at a high level the question of what the computer does to interpret the code. Surely, your computer wasn’t manufactured knowing what the words “int
”, “using
”, “include
”, etc. all mean.
There are three main steps to be aware of:
- Compilation — a program called a “compiler” reads the code in your project and translates it into an incomplete set of instructions for your computer to run. These instructions are things your computer instinctually understands, though some instructions were “included” in the very first line. So parts of your program are hiding somewhere else on your computer.
- Linking — another program called a “linker” organises the instructions produced in step 1 into a full program. In particular, it weaves in the instructions that were “included” from elsewhere.
- Execution — the instructions from step 2 are run, producing the beautiful
Hello world!
in your console.
Steps 1 and 2 together are often called “building” a program, though often people will refer to them together as “compiling”. Once you’ve built your code once, you can run it many times, but if you make changes to your code, you’ll have to build it again to see those changes. This is not necessary knowledge for exams, but I think it’s important vocabulary and context.