Notes from 11/19

arrays.cpp

#include <iostream>
using namespace std;

int main()
{
    // Array declaration
    int blocks[7];   // locations numbered 0 to 6
    blocks[3] = 4;
    blocks[5] = blocks[3] + 8;
    // A loop to initialize the array -- right to left
    for(int i = 6;  i >= 0  ; i--) {
        blocks[i] = i;
        cout << "Setting blocks[" << i << "] to " << blocks[i] << "\n";
    }

    // Print contents of the array, separated by commas.
    //cout << blocks << "\n"; // This just prints the address (hexadecimal)

    for(int i = 0; i < 7; i++) {
        cout << blocks[i];
        if(i < 6) // not on last element
        {
            cout << ",";
        }
    }
    cout << "\n";

    // Let's try a different way
    for(int i = 0; i < 7; i++) {
        if(i > 0) // not on first element
        {
            cout << ",";
        }
        cout << blocks[i];
    }
    cout << "\n";

    // Array initializer syntax -- can specify values as part of 
    // declaration.
    int numbers[4] = { 70, 60, 50, 40 };

    for(int i = 0; i < 4; i++) {
        if(i > 0) cout << ",";
        cout << numbers[i];
    }
    cout << "\n";

    // Error: Too many initializers:
    // int tooMany[4] = { 1, 2, 3, 4, 5, 6 };

    // Not enough initializers? The rest are set to zero.
    int notEnough[4] = { 1 , 2 };
    for(int i = 0; i < 4; i++) {
        if(i > 0) cout << ",";
        cout << notEnough[i];
    }
    cout << "\n";

    // Can initialize whole array to zero this way.
    int tally[100] = { 0 };

    return 0;
}

arraysize.cpp

#include <iostream>
using namespace std;
int main()
{
    // Size of the array must be known at compile time.

    // Suppose I want to ask a user for the ages of his/her children.
    int numChildren = 50;
    cout << "How many children? ";
    cin >> numChildren;

    const int MAX_CHILDREN = 10;  // constant
    if(numChildren > MAX_CHILDREN) {
        cout << "ERROR, only " << MAX_CHILDREN << " allowed.\n";
        return 0;
    }

    //int ages1[numChildren]; // not known at compile time, so some
                            // compilers will flag an error here.

    int ages[MAX_CHILDREN]; // okay to use constant, because it's
                            // known at compile-time.
    for(int i = 0; i < numChildren; i++) {
        cout << "Age of child " << i << "? ";
        cin >> ages[i];
    }
    return 0;
}

dice.cpp

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

const int NUM_DICE = 5;

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];
    rollDice(rolls);
    showDice(rolls);
    return 0;
}

reverse.cpp

#include <iostream>
using namespace std;
int main()
{
    const int MAX_SIZE = 1000;
    int numbers[MAX_SIZE];
    int current = MAX_SIZE - 1; // right-most index
    cout << "Enter numbers, -1 to end:\n";
    cin >> numbers[current];
    while( current > 0 && numbers[current] != -1) {
        current--;
        cin >> numbers[current];
    }
    cout << "Current is " << current << "\n";
    for(int i = current+1; i < MAX_SIZE; i++)
    {
        cout << numbers[i];
        if(i < MAX_SIZE-1 ) // not last
        {
            cout << ",";
        }
    }
    cout << "\n";
    return 0;
}