Notes from 11/28

references.cpp

#include <iostream>
using namespace std;
// Passing arrays by reference, and reference parameters.

void f(int a[], int x, int& y) // & indicates a reference parameter
{
    // Function can change either of its parameters,
    // but x is a VALUE parameter, so it doesn't affect
    // the value at the call site.
    x++;
    cout << x << "\n";          // 100

    // The array a is a REFERENCE parameter (because it's an array)
    // so changing it DOES affect the call site.
    a[3]++;
    cout << a[3] << "\n";       // 8

    y++;
    cout << y << "\n";          // 61
}

int main()
{
    int b[5] = { 3, 4, 1, 7, 2 };
    int x = 99;
    int y = 60;
    f(b, x, y);
    cout << x << "\n";          // 99
    cout << b[3] << "\n";       // 8
    cout << y << "\n";          // 61
    //f(b, x, 24); // error, need a variable to point to
    f(b, x, b[2]);

    int k[2] = { 1, 2 };
    f(k, x, y); // PROBLEM: k is too short, f accesses a[3]
    return 0;
}

scores.cpp

#include <iostream>
using namespace std;

int main()
{
    const int NUM_STUDENTS = 5;
    const int NUM_ASSIGNS = 4;
    int scores[NUM_STUDENTS][NUM_ASSIGNS] = {
        { 30, 40, 40, 21 },
        { 40,  0, 38, 28 },
        { 40, 40, 40, 38 },
        { 20, 18, 40, 35 },
        { 20, 20, 0, 0 }
    };

    // Average for each student:
    for(int s = 0; s < NUM_STUDENTS; s++) { // for each student
        int sum = 0;
        for(int a = 0; a < NUM_ASSIGNS; a++) { // for each assignment
            sum += scores[s][a];
        }
        double avg = sum / (double)NUM_ASSIGNS;
        cout << "Student " << s << " average " << avg << "\n";
    }

    // Average for each assignment:
    for(int a = 0; a < NUM_ASSIGNS; a++) {
        int sum = 0;
        for(int s = 0; s < NUM_STUDENTS; s++) {
            sum += scores[s][a];
        }
        double avg = sum / (double)NUM_STUDENTS;
        cout << "Assignment " << a << " average " << avg << "\n";
    }
}

yahtzee.cpp

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

const int NUM_DICE = 5;

int fiveOfAKind(int dice[NUM_DICE]) {
    int tally[7] = { 0 };
    for(int i = 0; i < NUM_DICE; i++) {
        tally[dice[i]]++;
        if(tally[dice[i]] == 5) {
            return 50;
        }
    }
    return 0;
}

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

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

int main()
{
    srand(time(NULL)); // Initialize random number generator
    int dice[NUM_DICE];
    rollDice(dice);
    showDice(dice);
    cout << "Fiveofakind: " << fiveOfAKind(dice) << "\n";

    // Test case for five of a kind:
    int d5[NUM_DICE] = { 3, 3, 3, 3, 3 };
    showDice(d5);
    cout << "Fiveofakind: " << fiveOfAKind(d5) << "\n";

    int dnot5[NUM_DICE] = { 3, 3, 3, 4, 3 };
    showDice(dnot5);
    cout << "Fiveofakind: " << fiveOfAKind(dnot5) << "\n";

    // Roll UNTIL we get five of a kind!
    for(int i = 0; fiveOfAKind(dice) == 0; i++) {
        rollDice(dice);
        cout << i+1 << "\n";
        showDice(dice);
    }
}