In this lab you'll learn to write pseudo code and then translate it to real code.
By the end of this lab, you should feel comfortable:
- following directions
- writing a C++ program in Visual Studio Code
- compiling and running a C++ program on the command line
- writing pseudo code
- translating simple pseudo code to C++ code
- submitting multiple files to Canvas
Part 1: Install a text editor and C++ compiler
See the instructions for downloading Visual Studio Code and a C++ compiler linked to from the Programming environments page. After installing the compiler, be sure that you test it before moving on by closing then reopening your terminal and typing "g++". If it installed correctly, you should get an error message that no input files were specified.
Part 2: Pseudo Code
Get a sheet of grid paper from me and orient it so that there are numbers at the top (the long side) and left (short side). Draw something with 20–30 straight lines connected at dots on the grid paper (your initials, a house, a car, etc.). Write your name on the top of this paper. Take a picture of your paper with your smartphone (you'll submit it to Canvas in a little bit).
In Visual Studio Code, open a new plain text file and save it to a file named lab1-part2.txt. Write pseudo code (a series of English instructions) to describe how to recreate the drawing exactly as it appears on your handout. You should be able to hand your pseudo code to another human with a pen and graph paper and they should be able to follow your pseudo code to create an image identical to the one you have.
Be sure to include your name, date, and the assignment name at the top of your pseudo code.
Part 3: The hand off
Find a classmate who is finished their pseudo code and exchange your code with them. On a fresh sheet of graph paper, follow their instructions exactly. Write your name at the top of this paper and hand it back to your classmate.
Once your classmate has handed you back the picture they drew following your pseudo code, take a picture of their drawing and upload it as lab1-part3.png (or lab1-part3.jpg).
If the drawing your classmate created using your pseudo code looks off, check why. Is there something incorrect with or missing in your pseudo code? Or did your classmate not follow directions? If the former, copy your pseudo code to a new file named lab1-part3.txt and modify it as necessary.
Part 4: Transcribing pseudo code to C++
You will now transcribe your pseudo code to C++. To do this, you will modify lab1.cpp (again, use Visual Studio Code). Before you do anything else with the file, add the date and your name (you're the author) to the header.
You will place your code in the main function (see where it says TODO in the code). You cannot use your pseudo code directly—that isn't valid C++. However, I have added a number of C++ functions that will make it easier to map what you want to do to C++. You can see these functions described in comments below main in the download, or you can see the application programming interface (API) described here. API documentation is extremely important to learning how to use new libraries and functions, so take time to look it over.
You will need to make sure the first line of main is start(); and the second-to-last line, just above return 0; is end();
Rather than transcribing your entire program all at once, engage in incremental programming: write one or two lines of code, then compile and run to make sure everything is working as you expect. This will safe you time later!
To compile your program, open the terminal from the Visual Studio Code "View" menu. The terminal opens a command line in whatever directory Visual Studio Code opened; by default this is probably not where you want to be. To find the path to your current file, hover your mouse over the title of your file in the Visual Studio Code tab. You should see something like: "/Users/Pat Smith/Desktop/CSC160/lab1.cpp". You need to change your terminal's directory to the directory that the file lives in. To do that, use the cd command. For the example path I just showed, I would cd into my CSC160 folder by doing:
cd "/Users/Pat Smith/Desktop/CSC160/"
If that doesn't work, call me over and I will help you out. You can also take a look at the "Using the command line" section of the Programming environments page. Once in the correct directory, you can compile from the terminal like this:
g++ lab1.cpp -o lab1
If you encounter errors, then attempt to address them (feel free to ask a classmate or me for help) and recompile. Once it compiles without error, run your program like this:
Everytime you run this program, it will generate/overwrite a file named lab1.html, which you can then open in a web browser (double click it from Finder or Windows Explorer). You have successfully implemented your pseudo code when you see a replica of the picture from your original handout.
Every time you make a change to your code, make sure that you
- re-run your program on the command line
- refresh the page in your browser (since running your program updates the lab1.html file)
Here's what you should submit once you're done all three parts:
- your original pseudo code file (lab1-part2.txt)
- the picture of your handout (call this lab1-part2.png or lab1-part2.jpg; if you can't figure out how to rename the photo, that's okay)
- the picture of your classmate's attempt to follow your pseudo code (lab1-part3.png or lab1-part3.jpg; if you can't figure out how to rename the photo, that's okay)
lab1-part3.txt (only if changes were necessary)