CSCI 102 - Fall 2024 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:

loading…
loading…
loading…

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.

loading…
loading…
loading…

Open-ended Help

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