Notes week of Nov 14

func-example.cpp

// func-example
#include <iostream>
using namespace std;

// Forward declaration (prototype)
// for function to be defined later:
void say_hello();

// Function definition:
int main()                      // int is the return type
{
    for(int i = 0; i < 5; i++)
    {
        // Function call:
        say_hello();
    }
    // Finish main function:
    return 0;
}

// Another function definition:
void say_hello()                // void is the return type,
{                               // it means returns "nothing".
    cout << "Hello world\n";
    return;                    // optional, but doesn't need value because void
}

param-example.cpp

// param-example
#include <iostream>
using namespace std;

void say_hello(string name);

// Function definition:
int main()                      // int is the return type
{
    string input;
    cout << "Enter your name: " << endl;
    getline(cin, input);
    if(input.size() < 5) {
        cout << "Wow that's too short. I'm calling you Douglas.\n";
        say_hello("Douglas");
    }
    else {
        say_hello(input);
    }

    // Finish main function:
    return 0;
}

// Another function definition:
void say_hello(string name) // name is a parameter
{
    cout << "Hello, " << name << "\n";
    return;
}

non-void.cpp

// non-void example
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

float area_of_circle(float radius);
float circumferenc_of_circle(float radius);

// Function definition:
int main()                      // int is the return type
{
    const int col = 12;
    cout << setw(col) << "Radius"
         << setw(col) << "Area"
         << setw(col) << "Circumf" << "\n";
    for(int i = 1; i < 10; i++)
    {
        cout << setw(col) << i
             << setw(col) << area_of_circle(i)
             << setw(col) << circumferenc_of_circle(i)
             << "\n";
    }

    // Finish main function:
    return 0;
}

float area_of_circle(float radius)
{
    return M_PI * pow(radius, 2);
}

float circumferenc_of_circle(float radius)
{
    return 2 * M_PI * radius;
}

soundex.cpp

// soundex using functions -- incomplete
#include <iostream>
#include <cassert>
using namespace std;

string step2_remove_hw(string s);
string step3_digits(string s);

int main()
{
    // Unit tests
    assert(step2_remove_hw("ralph") == "ralp");
    assert(step2_remove_hw("powwow") == "poo");
    assert(step2_remove_hw("helwarw") == "helar");
    assert(step2_remove_hw("wwwwwww") == "w");

    assert(step3_digits("bibimbap") == "1i1i51a1");
    assert(step3_digits("berry") == "1e66y");
    assert(step3_digits("ralph") == "6a41h");

    // Main program
    cout << "Enter a name: ";
    string name;
    getline(cin, name);
    char firstLetter = name.at(0);
    string result =
        step3_digits(step2_remove_hw(name));
    cout << result << endl;
    return 0;
}

string step2_remove_hw(string s)
{
    for(unsigned int i = 1; i < s.size(); )
    {
        switch(s.at(i))
        {
        case 'h': case 'w':
        case 'H': case 'W':
            s.erase(i,1);
            break;
        default:
            i++;
        }
    }
    return s;
}

string step3_digits(string s)
{
    for(unsigned int i = 0; i < s.size(); i++)
    {
        switch(s.at(i))
        {
        case 'b': case 'p':
            s.at(i) = '1';
            break;
        case 'm':
            s.at(i) = '5';
            break;
        case 'l':
            s.at(i) = '4';
            break;
        case 'r':
            s.at(i) = '6';
            break;
        }
    }
    return s;
}