CSCI 102 - Fall 2023 Fundamentals of Computation

Week 5 Lab

Q&A on Material

Vocareum

Sign in to lab!

Q&A on Material

Exercises

Walk through these exercises and complete them as a group (if time allows).

Tracing

#include <iostream>
using namespace std;
double withdraw(double balance, double amount);
double deposit(double balance, double amount);
bool isOverdrawn(double balance);

int main()
{
    double balance = 100.0;
  
    // deposit two paychecks and try to withdraw 
    balance = deposit(balance, 500.0);
    if ( !isOverdrawn(withdraw(deposit(balance, 700.0), 400.0)) )
    {
         balance = withdraw(deposit(balance, 700.0), 400.0); 
    }
    return 0;
}
double withdraw(double balance, double amount) {
    balance -= amount;
    return balance;
}
double deposit(double balance, double amount) {
    balance += amount;
    return balance;
}
bool isOverdrawn(double balance) {
    return balance < 0.0;
}

Solution

Coding

cpp/cs102/lab6/polygon-area
cpp/cs102/lab6/isosceles

Coding Practices

We hope that as you learn the basics of programming you can also start to learn good coding practices to make your code more readable and well-organized. Below are some helpful links to improving your coding of conditionals that also involve functions. Take some time to read over them and see if you can improve any of your code either from last week’s or this week’s lab.

Open-ended Help

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