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:
- 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: 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 15–20 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.
In Visual Studio Code, open a new plain text file and save it to a file named lab1-part1.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 2: 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, call me over so I can see it.
If the drawing your classmate created using your pseudo code looks different from your original drawing, 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-part2.txt and modify it as necessary.
Part 3: Transcribing pseudo code to C++
You will now transcribe your pseudo code to C++. To do this, download lab1.cpp and open it in 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 Software 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:
Every time 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)
You don't need to submit anything, but you do need to show me what you've done by the end of the lab. Please call me over when you've completed the lab and show me your C++ program and the result.