CPSC170A
Fundamentals of Computer Science II

Lab 13

Vectors

Print Vector

There is no built-in, easy way to print out a C++ vector. So let’s create a function that makes it easy. Create the function print_vector(std::vector<int> vector) that prints a vector of ints like the following:

[0, 2, 1, 3]

Note that the vector’s elements are surround by opening and closing square brackets, that each element in the vector is separated by a comma and a space, and that there is no comma and space after the last element. Be sure to test your code on an empty vector.

This function only prints vectors of ints. To also print vectors of doubles, overload the function by defining the function print_vector(std::vector<double> vector).


Birthday Paradox

Write the function shared_probability(int group_size) that computes the probability that two people in a group share a birthday. The parameter group_size is a positive integer that specifies the number of people in a group. The function should return a floating point value in the range [0, 1], the probability that there is a shared birthday in a group of size group_size. A probability of 0.0 means that there is a 0% chance and a probability of 1.0 means that there is a 100% chance.

The function should not compute the exact probability, instead it should approximate the probability with repeated simulation. Run the simulation many times. The more times it runs the more accurate it will be. The probability is the average number of times that there are two people that share a birthday.