burntraisin

C++ Project Design Workshop: Part 3

During March and April of this year, I coordinated and taught a C++ Project Design workshop at Bergen Community College. This serves as an introduction to programming in C++, as well as some introduction to Linux and software development ideologies, such as Agile.

This was a four-part workshop, each lesson an hour long.

This post contains the third lesson of the workshop.


Workshop Outline

  1. Setup
  2. Programming fundamentals
  3. Project introduction (today!)
  4. Software methodologies

Today’s Outline


Loops

A loop is a control structure that repeats a statement or group of statements until the condition it’s testing is false.

The main loop structures are:

Each execution of a loop is called an iteration.

Types of Loops

Basic Parts of a Loop

The anatomy:

Terminology:

int x = 0;
while (x < 5) { // Header
    cout << x << endl;
    x++; // LCV/counter
}

while Loop

while (condition) {
    // Statements
}

do-while Loop

do {
    // Statements
} while (condition);

for Loop

for (initialization; test; update) {
    // Statements
}

width:700px

^ From “Starting Out with C++: Early Objects” (10th Ed.) by Tony Gaddis, Judy Walters, and Godfrey Muganda

How do I know what loop to use?

Breaking Out of a Loop

Aside: Nesting Ifs and Loops

If blocks and loops can be nested, but limit the number of nesting you do. It may impact performance.


Functions

Function Definition

alt text

^ From “Starting Out with C++: Early Objects” (10th Ed.) by Tony Gaddis, Judy Walters, and Godfrey Muganda

The void Function

void sayHello(); // Function prototype

int main() {
    sayHello(); // Function call
    return 0;
}

// Function definition
void sayHello() {
    cout << "Hello world!\n";
}

Calling Functions

Function Prototypes

int calculateSum(int num1, int num2); // Function prototype with 2 parameters

int main() {
    int sum = calculateSum(4, 8); // Function call, passing 2 arguments
    cout << sum << endl;
    return 0;
}

// Function definition
int calculateSum(int num1, int num2) {
    return num1 + num2;
}

Sending Data to Functions

Pass by Value vs. Pass by Reference

void showStatic();

int main() {
    for (int count = 0; count < 5; count++) {
        showStatic();
    }

    return 0;
}

void showStatic() {
    static int numCalls = 0;

    cout << "This function was called " << ++numCalls << " times.\n";
}

#include <iostream>
using namespace std;

// Function prototypes
void getNum(int &);   
int doubleNum(int);

int main() {
    int value;

    // Call getNum to get a number and store it in value
    // The & does not appear in the function call
    getNum(value);   

    // Call doubleNum, passing it the number stored in value
    // Assign value the number returned by the function
    value = doubleNum(value);

    // Display the resulting number
    cout << "That value doubled is " << value << endl;
    return 0;
}

void getNum(int &userNum) {
    cout << "Enter a number: ";
    cin  >> userNum;
}

int doubleNum (int number) {
    return number * 2;
} 

When to Pass Args by Reference or Value

alt text

Scope: Local and Global Variables

Try: Write a Function

Task: Write a function that calculate the area of a triangle. Take the base and height as parameters and return the area.


Arrays

// Syntax
int hours[6]; // Declaration

const int SIZE = 6;
int hours[SIZE];

hours[0] = 20; // Stores 20 in the first element of array hours

alt text

^ From GeeksForGeeks.

Initializing Arrays

 string cars[5] = {"Mazda", "Volvo", "Audi", "Mercedes", "Aston Martin" };

Cycling Through an Array

for (int i = 0; i < 5; i++) {
    cout << "Car make: " << cars[i] << endl;
}

Try: Creating an Array

Task: Create an array of at least 3 integers and sum them. Display the result.

Arrays as Function Arguments

#include <iostream>
using namespace std;

void showValues(int intArray[], int size); // Function prototype

int main() {
    const int ARRAY_SIZE = 8;
    int collection[ARRAY_SIZE] = {5, 10, 15, 20, 25, 30, 35, 40};

    cout << "The array contains the values\n";
    showValues(collection, ARRAY_SIZE);
    return 0;
}

void showValues (int nums[], int size) {
    for (int index = 0; index < size; index++) {
        cout << nums[index] << "  ";
    }
    cout << endl;
}

Project Introduction

Objective: Write a pomodoro timer in C++. We expect the user to input the number of work sessions they want to do, and our program should terminate after all of the work sessions are complete. A work session is 25 minutes and a short break is 5 minutes. After 4 sessions, a longer break of 15 minutes is taken.

For example, if a user writes ./timer 6, 4 work sessions occur with short breaks in between, and then a long break occurs the 4th work session followed by 2 work sessions with a short break in between and after.

To take arguments from the terminal…

You use the following parameters in main():

int main(int argc, char *argv[]) {
    // argc is the number of arguments passed to the program
    // argv is a C-string of arguments
    return 0;
}

You may need to convert a string to an integer

Different data types can’t be mixed!

Special characters

You will likely need chrono and thread

Other notes

#Guide