Here are my solutions to the last two assignments.
#include <iostream>
#include <vector>
using namespace std;
void print_vector (vector<int> v);
int find_minimum (vector<int> v);
double compute_average (vector<int> v);
int how_many_evens (vector<int> v);
int main()
{
vector<int> v;
v.push_back( 3); v.push_back( 5); v.push_back( 8);
v.push_back( 2); v.push_back(10); v.push_back(-3);
v.push_back( 7); v.push_back( 6); v.push_back( 4);
v.push_back( 5); // TODO: better initializer?
print_vector(v);
cout << "The minimum is " << find_minimum(v) << "\n";
cout << "The average is " << compute_average(v) << "\n";
cout << "There are " << how_many_evens(v)
<< " even numbers.\n";
return 0;
}
void print_vector(vector<int> v)
{
for(unsigned i = 0; i < v.size(); i++) {
cout << "v[" << i << "] is " << v[i] << "\n";
}
}
int find_minimum(vector<int> v)
{
int k = v[0];
for(unsigned i = 1; i < v.size(); i++) {
if(v[i] < k) {
k = v[i];
}
}
return k;
}
double compute_average(vector<int> v)
{
double sum = 0;
for(unsigned i = 0; i < v.size(); i++) {
sum += v[i];
}
return sum/v.size();
}
int how_many_evens(vector<int> v)
{
int k = 0;
for(unsigned i = 0; i < v.size(); i++) {
if(v[i]%2 == 0) k++;
}
return k;
}
// Yahtzee game
#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>
using namespace std;
// Function prototypes: see documentation for each below.
int roll_one_die();
vector<int> roll_all_dice(int num);
void roll_these_again(vector<int>& dice, string which);
void print_dice(vector<int> dice);
void print_best_hand(vector<int> dice);
bool n_of_a_kind(vector<int> tally, int n);
int num_pairs(vector<int> tally);
// Main program: you shouldn't change this very much.
// You may temporarily replace what's here with some
// test code.
int main()
{
cout << "WELCOME TO Yahtzee!" << endl;
srand(time(NULL)); // Initialize PRNG
const int NUM_DICE = 5;
vector<int> dice = roll_all_dice(NUM_DICE);
int rolls_left = 2;
while(true)
{
print_dice(dice);
print_best_hand(dice);
if(rolls_left == 0)
{
break;
}
cout << "Which to roll again? ";
string selected;
getline(cin, selected);
roll_these_again(dice, selected);
rolls_left--;
}
cout << "GAME OVER" << endl;
return 0;
}
int roll_one_die()
{
return rand() % 6 + 1;
}
vector<int> roll_all_dice(int num)
{
vector<int> dice(num);
for(int i = 0; i < num; i++) {
dice.at(i) = roll_one_die();
}
return dice;
}
void print_dice(vector<int> dice)
{
cout << "Dice:" << endl;
char c = 'a';
for(int i = 0; i < dice.size(); i++, c++) {
cout << " (" << c << ") " << dice.at(i) << endl;
}
}
void roll_these_again(vector<int>& dice, string which)
{
for(int i = 0; i < which.size(); i++) {
int j = which.at(i) - 'a';
if(j >= 0 && j < dice.size()) {
dice.at(j) = roll_one_die();
}
else {
cout << "Invalid input: '" << which.at(i) << "'" << endl;
}
}
}
bool n_of_a_kind(vector<int> tally, int n)
{
for(int i = 0; i < tally.size(); i++) {
if(tally[i] == n) {
return true;
}
}
return false;
}
int num_pairs(vector<int> tally)
{
int count = 0;
for(int i = 0; i < tally.size(); i++) {
if(tally[i] == 2) {
count++;
}
}
return count;
}
void print_best_hand(vector<int> dice)
{
vector<int> tally(7);
for(int i = 0; i < dice.size(); i++) {
tally.at(dice.at(i))++;
}
// cout << "Tallies: ";
// for(int j = 1; j <= 6; j++) {
// cout << j << ":" << tally.at(j) << " ";
// }
// cout << endl;
if(n_of_a_kind(tally, 5)) {
cout << "Yahtzee!" << endl;
}
else if(n_of_a_kind(tally, 3) && n_of_a_kind(tally, 2)) {
cout << "Full house." << endl;
}
else if(n_of_a_kind(tally, 4)) {
cout << "Four of a kind." << endl;
}
else if(n_of_a_kind(tally, 3)) {
cout << "Three of a kind." << endl;
}
else if(num_pairs(tally) == 2) {
cout << "Two pair." << endl;
}
else if(num_pairs(tally) == 1) {
cout << "One pair." << endl;
}
else {
cout << "Nothing!" << endl;
}
}