Week 9 Lab
Q&A on Material
- Nested loops
- Installing your own editor
Sign in to lab!
Nested-Loop Exercises
Tracing exercise
Walk through these exercises and complete them as a group (if time allows):
Determine what the following code will print.
#include <iostream>
using namespace std;
int main()
{
int a = 24, b = 9, x, y;
for(int i=2; i <= 4; i++){
x = a;
y = b;
while( y != 0 ){
int t = y;
y = x % y;
x = t;
}
cout << x << endl;
a += 3;
b *= i;
}
return 0;
}
Coding exercises
Install an Editor on your PC
-
In the Debugging lecture we discussed installing an editor on your own PC (as well as compiling and debugging). In preparation for the Midterm 2 and for future classes, you will need some way to download, edit, save, and then re-upload code files (
.cpp
files). To that end, we ask/require all students to install a code editor on your personal computer. While many great editors exist (Sublime, Atom, emacs, etc), we recommend Visual Studio Code as a nice cross-platform, extensible editor. Please download and install it. -
Your discussion leaders may be able to show a few simple commands for
- Saving (Ctrl-S)
- Indenting multiple lines (Select the lines and press Tab)
- Dedenting multiple lines (Select the lines and press Shift-Tab)
- Comment/Uncomment (Select the lines and press Ctrl-/ to toggle between commented and not) )
Note This editor (and many others) do NOT come with a compiler to conver the code into an executable (i.e. what the “Build” button does on Vocareum). There are compilers you can install on a Windows or Mac, though we don’t require it. Feel free to experiment and work with your TAs to create a local environment on your PC that allows you to write, compile, and run your own C++ programs.
As an exercise, we recommed you go back to the Lab 2
assignment on Vocareum and practice downloading the code file, opening it in Visual Studio Code, editing it some and saving it. Then you can copy/paste back to Vocareum to run the program or copy/paste to cpp.sh to run the program.
Open-ended Help
Any students with questions may stay after to get help. Anyone else may leave at this stage.