String and loop exercises

These are from 20 November, about arrays.

a9sol.cpp

// A9 solution.
#include <iostream>
using namespace std;

void print_backwards(string text);
int count_vowels(string text);
bool is_palindrome(string text);

int main()
{
    string line;
    while(1)
    {
        cout << "Enter your text (or end): ";
        getline(cin, line);
        if(line == "end") break;
        print_backwards(line);
        cout << "Your input has ";
        int v = count_vowels(line); // plural or singular?
        if(v == 1) cout << v << " vowel.\n";
        else cout << v << " vowels.\n";
        if(is_palindrome(line)) {
            cout << "Palindrome!\n";
        }
    }
    cout << "Thanks.\n";
    return 0;
}

void print_backwards(string text)
{
    cout << "Backwards: ";
    for(int i = text.length()-1;  i >= 0;  i--)
    {
        cout << text[i];
    }
    cout << "\n";
}

int count_vowels(string text)
{
    int k = 0;
    for(int i = 0; i < text.length(); i++)
    {
        switch(text[i])
        {
        case 'a': case 'e': case 'i': case 'o': case 'u':
        case 'A': case 'E': case 'I': case 'O': case 'U':
            k++;
        }
    }
    return k;
}

// Kind of cool but I didn't expect anything like this.
bool cool_is_palindrome(string text)
{
    string reverse = string(text.rbegin(), text.rend());
    return text == reverse;
}

// This is the algorithm for testing palindromes using a loop.
bool is_palindrome(string text)
{
    int i = 0;
    int j = text.length()-1;
    while(i <= j)
    {
        if(text[i] != text[j]) return false;
        i++;
        j--;
    }
    return true;
}

fizzbuzz.cpp

#include <iostream>
using namespace std;
int main()
{
    for(int i = 1; i <= 100; i++)
    {
        if(i%3 == 0) cout << "Fizz";
        if(i%5 == 0) cout << "Buzz";
        if(i%3 != 0 && i%5 != 0) cout << i;
        cout << "\n";

        // if(i%3 == 0 && i%5 == 0) cout << "FizzBuzz\n";
        // else if(i%3 == 0) cout << "Fizz\n";
        // else if(i%5 == 0) cout << "Buzz\n";
        // else cout << i << "\n";
    }

    return 0;
}

arrays1.cpp

#include <iostream>
using namespace std;

const int SIZE = 10;

void print_array(int a[SIZE]);

int main()
{
    int a[SIZE] = { 3, 5, 8, 10, 2, 2, 0, 9, 24, 16 };
    //SIZE++; // not valid because const
    cout << a << "\n"; // just shows memory address
    print_array(a);
    // a little algorithm that summarize the array
    // this finds the maximum.
    int k = a[0];
    for(int i = 1; i < SIZE; i++)
    {
        if(a[i] > k)
        {
            k = a[i];
        }
    }
    cout << "k is " << k << "\n";
    // a little algorithm that modifies the array
    // (compute a running sum within the array)
    cout << "Modifying...\n";
    for(int i = 1; i < SIZE; i++)
    {
        a[i] += a[i-1];
    }
    // now print it again
    print_array(a);
    return 0;
}

void print_array(int a[SIZE])
{
    // to print contents of array, I have to write a loop.
    for(int i = 0; i < SIZE; i++)
    {
        cout << "a[" << i << "] is " << a[i] << "\n";
    }
}

// Array problems you could try:
// 1. Find minimum
// 2. Find sum
// 3. Find average
// 4. Find how many numbers in array are even
// 5. How many numbers in the array are bigger than 50.
// 6. Are all the numbers in the array equal?

yahtzee.cpp

#include <iostream>
#include <cstdlib>
using namespace std;
// start on a dice game called Yahtzee.

const int NUM_DICE = 5;

void roll_dice(int d[NUM_DICE]);
void print_dice(int d[NUM_DICE]);

int main()
{
    int dice[NUM_DICE]; 
    // Initialize the random number generator
    srand(time(NULL));
    roll_dice(dice);
    print_dice(dice);
    return 0;
}

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

void print_dice(int d[NUM_DICE])
{
    cout << "Dice: ";
    for(int i = 0; i < NUM_DICE; i++)
    {
        cout << d[i] << " ";
    }
    cout << "\n";
}