CSCI 102 - Fall 2023 Fundamentals of Computation

Week 9 Lab

Q&A on Material

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;
}

Solution

Coding exercises

cpp/cs102/lab9/gcd-nested

Install an Editor on your PC

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.