Week 5 Lab
Q&A on Material
- Calling and writing basic functions
Vocareum
- Review HW05
- Submit and receive confirmation and grading report for 80% of points (test passing). Review how to examine the submission report.
- 20% of your score will be awarded based on code inspection (we will provide in-line comments)
Sign in to lab!
Q&A on Material
- Defining and calling simple functions
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;
}
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.
- Extract variable
- Split temporary variable
- Extract Method
- Decompose Conditional
- Consolidate If Statements
- Consolidate Duplicate Fragments
Open-ended Help
Any students with questions may stay after to get help. Please ask questions! Anyone else may leave at this stage.