CSCI 102 - Fall 2023 Fundamentals of Computation

Week 4 Lab

Sign in to lab!

Warmup (Division and Modulo Idioms)

Perform one or more of the exercises below (specified by your lab instructor) related to use of division and modulo operations:

Then try to apply the appropriate idiom to the following exercises:

cpp/division-modulo/birth-year
cpp/division-modulo/sections
cpp/division-modulo/convert

Q&A on Material

Tracing

Trace the execution of this program and predict what this program will output when the users types in 3. Then repeat the exercise if the user types in 9.

#include <iostream>
#include <cmath>
using namespace std;
int main() 
{
    int i;
    char color = 'r'; 
    cin >> i;

    if (i % 5 != 0) 
    {
        i++;
        color = 'b';
    }
    if (i % 5 == 0) 
    {
        i -= 2;
    } 
    else if (i % 4 == 0) 
    {
        i++;
        if ((color == 'r') || (i == 9)) 
        {
            color='c';
        }
    }
    else 
    {
        i += 5;
        color='g';
    }
    return 0;
}

Take a look at the idioms related to if (conditional) statements:

Exercises

Walk through these exercises and complete them as a group. Identify what idiom is most appropriate for each exercise.

cpp/cs102/practice6/tippingCalculator
cpp/cs102/decision-tree/menu
cpp/cs102/lookup-table/sales-tax

Open-ended Help

Any students with questions may stay after to get help. Please ask questions! Anyone else may leave at this stage.