Notes week of Dec 5

mult-table.cpp

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
    const int SIZE = 12;
    int table[SIZE][SIZE];
    // Populate the table with values
    for(int i = 0; i < SIZE; i++) { // For each row, i
        for(int j = 0; j < SIZE; j++) { // For each col j
            table[i][j] = (i+1) * (j+1);
        }
    }
    // Print the table
    for(int i = 0; i < SIZE; i++) {
        for(int j = 0; j < SIZE; j++) {
            if(i == 0 || j <= i) {
                cout << setw(4) << table[i][j];
            }
        }
        cout << endl;
    }

    return 0;
}

small-walk.cpp

// small-walk: walk randomly down or right
// (starting from upper left)
#include <iostream>
using namespace std;
const int SIZE = 14;
int main()
{
    srand(time(NULL));
    char grid[SIZE][SIZE];
    // Start out with '.' (DOT) in each space
    for(int i = 0; i < SIZE; i++) {
        for(int j = 0; j < SIZE; j++) {
            grid[i][j] = '.';
        }
    }
    // Start walk in upper left
    int currentI = 0;
    int currentJ = 0;
    char currentChar = 'A';
    while(currentChar <= 'Z' && currentI < SIZE
          && currentJ < SIZE)
    {
        // Mark our position
        grid[currentI][currentJ] = currentChar;
        currentChar++;
        // Update by randomly walking right or down.
        switch(rand()%2) {
        case 0: // Right
            currentJ++;
            break;
        case 1: // Down
            currentI++;
            break;
        }
    }
    // Print the grid
    for(int i = 0; i < SIZE; i++) {
        for(int j = 0; j < SIZE; j++) {
            cout << grid[i][j];
        }
        cout << endl;
    }
}

roll-again.cpp

#include <iostream>
#include <vector>
using namespace std;

void roll_these_again(vector<int>& dice, string which)
{
    // Loop through string of choices
    for(int i = 0; i < which.size(); i++)
    {
        char position = which.at(i);
        int index = position - 'a';
        cout << "We want to re-roll position "
             << position
             << " which is index "
             << index << endl;
        // TODO: simulate re-rolling the die
        if(index < dice.size()) {
            dice.at(index) = 99;
        }
        else {
            cout << "Error: ignoring " << position << endl;
        }
    }
}

int main()
{   //                   a  b  c  d  e
    vector<int> dice = { 1, 2, 3, 4, 5 };
    roll_these_again(dice, "acq");
    for(int i = 0; i < dice.size(); i++)
    {
        cout << dice.at(i) << "  ";
    }
    cout << endl;
    return 0;
}