November 10th Lecture Notes

Please note that the code given below is the exact same code that was developed “live” in the lectures. So it could contain bugs or mistakes!

What is pi?

How can you calculate the value of pi without using any tricky mathematics?

// pi.cpp

#include "cmpt_error.h"
#include <iostream>
#include <cstdlib>
#include <cmath>

using namespace std;

double rand1() {
        return rand() / double(RAND_MAX);
}

double dist(double a, double b, double x, double y) {
        double dx = a - x;
        double dy = b - y;
        return sqrt(dx * dx + dy * dy);
}

int main() {
        srand(time(NULL));

        int hits = 0;
        int misses = 0;

        for(int i = 0; i < 10000000; ++i) {
                double x = rand1();
                double y = rand1();
                if (dist(0, 0, x, y) <= 1.0) {
                        hits++;
                } else {
                        misses++;
                }
        }
        cout << "hits = " << hits << "\n"
             << "misses = " << misses << "\n";
        double area_est = 4.0 * hits / (hits + misses);
        double pi_est = area_est / (1.0 * 1.0);
        cout << "area_est = " << area_est << "\n"
             << "pi_est = " << pi_est << "\n";

        // cout << rand1() << "\n"
        // << rand1() << "\n"
        // << rand1() << "\n"
        // << rand1() << "\n";
} // main

Some Word Puzzles

What is the longest English word?

What English words have no vowels?

What English words have all the vowels?

in the lecture, the enable1.txt word list was used

you can enable1.txt on the web, e.g. http://code.google.com/p/dotnetperls- controls/downloads/detail?name=enable1.txt

it’s a 1.8MB text file containing over 170,000 English words sorted in alphabetical order, one word per line

note that the program reads from cin

so to use it in Linux/Unix, run it like this at the command line

$ ./word_puzzles.cpp < enable1.txt
// word_puzzles.cpp

#include "cmpt_error.h"
#include <iostream>

using namespace std;

bool is_vowel(char c) {
        return c == 'a' || c == 'e' || c == 'i'
            || c == 'o' || c == 'u' || c == 'y';
}

bool has_char(const string& s, char c) {
        for(int i = 0; i < s.size(); ++i) {
                if (s[i] == c) {
                        return true;
                }
        }
        return false;
}

int count_chars(const string& s, char c) {
        int count = 0;
        for(int i = 0; i < s.size(); ++i) {
                if (s[i] == c) {
                        count++;
                }
        }
        return count;
}

bool has_all_vowels(const string& s) {
        return count_chars(s, 'a') == 1
            && count_chars(s, 'e') == 1
            && count_chars(s, 'i') == 1
            && count_chars(s, 'o') == 1
            && count_chars(s, 'u') == 1
            && count_chars(s, 'y') == 1;
}

bool has_vowel(const string& s) {
        for(char c : s) {
                if (is_vowel(c)) {
                        return true;
                }
        }
        return false;
}

int main() {
        cout << "Enter some words: ";
        string w;
        string longest;
        while (cin >> w) {
                if (has_all_vowels(w)) {
                        cout << w << "\n";
                }
                // if (!has_vowel(w)) {
                //      cout << w << "\n";
                // }
                // if (w.size() > longest.size()) {
                //      longest = w;
                //      cout << longest << "\n";
                // }

        }
} // main