Notes from 11/26

dice.cpp

#include <iostream>
#include <stdlib.h>
using namespace std;

const int NUM_DICE = 5;

// If we have three of a kind, return sum of all dice.
// If not three of a kind, return 0.
int threeOfAKind(int rolls[NUM_DICE])
{
    return 0;
}

void rollDice(int rolls[NUM_DICE])
{
    for(int i = 0; i < NUM_DICE; i++) {
        rolls[i] = rand()%6+1;
    }
}

void showDice(int rolls[NUM_DICE])
{
    cout << "Your dice: ";
    for(int i = 0; i < NUM_DICE; i++) {
        cout << rolls[i] <<  " ";
    }
    cout << "\n";
}

int main()
{
    srand(time(NULL)); // Initialize random number generator
    int rolls[NUM_DICE];
    for(int i = 0; i < 20; i++) {
        rollDice(rolls);
        showDice(rolls);
        cout << "Score: " << threeOfAKind(rolls) << "\n";
    }
    return 0;
}

funcarray.cpp

// Passing an array to a function.
#include <iostream>
using namespace std;
void f(int x, int a[3], int& z)
{
    x = x+3;
    a[0] = a[1] + 3;
    z++;
}

int main()
{
    int y = 7;
    int b[3] = {2, 5, 9};
    int q = 4;
    f(y, b, q);
    cout << y << "," << b[0] << "," << q << "\n";
    return 0;
}

rand.cpp

#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
    const int RANGE = 6;
    const int MAX = 13;
    const int COUNT = 1000;
    srand(time(NULL));   // Initialize the Pseudo-RNG.
    int tally[MAX] = { 0 };
    cout << "Producing 0 up to " << RANGE-1 << "\n";

    for(int i = 0; i < COUNT; i++) {
        int a = rand()%RANGE + 1;
        int b = rand()%RANGE + 1;
        int k = a + b;
        cout << k << "\n";
        tally[k]++;
    }

    cout << " Results:\n";
    for(int i = 0; i < MAX; i++) {
        cout << i << ": " << tally[i] << "\n";
    }
    return 0;
}

tally.cpp

#include <iostream>
using namespace std;
int main()
{
    int month;
    // Tally of birth months
    int tally[12] = { 0 };
    cout << "Enter birth month (1-12, or 0 to quit): ";
    cin >> month;
    while(month != 0) {
        if(month > 12 || month < 0) {
            cout << "ERROR: Invalid month.\n";
        } else {
            // Valid month
            tally[month-1]++;
        }
        cout << "Enter birth month (1-12, or 0 to quit): ";
        cin >> month;
    }
    for(int i = 0; i < 12; i++) {
        switch(i) {
        case 0: cout << "Jan"; break;
        case 1: cout << "Feb"; break;
        default: cout << i+1;
        }
        cout << ": " << tally[i] << "\n";
    }
    return 0;
}