CS 1500-1, Spring 2008
Solo Program 5



OBJECTIVE:

This assignment is designed to give you practice using arrays.


THE ASSIGNMENT:

Part A:
Write a program that computes the mean and standard deviation for data entered by the user.
The entered data must be stored in an array and the array must be passed to separate functions that compute the mean and standard deviation.
(Recall: we wrote functions to do the for four numbers in Solo3.)

Hint: To set up your array. Declare a global constant called MAX_SIZE to be 100. Declare the array using MAX_SIZE. Then prompt the use to input the number of values they would like to enter, this value will be the "numberUsed" (as discussed in section 5.3 p.196 and in lecture). Then use a for loop to get that many values and store them in the array.

Part B:
The birthday paradox is that there is a surprisingly high probability that two people in the same room happen to share the same birthday.  By birthday, we mean the same day of the year (ignoring leap years), but not the exact birthday including the birth year or time of day.  Write a program that approximates the probability that two people in the same room have the same birthday, for 2 to 50 people in the room. 

The program should use simulation to approximate the answer.  Over many trials (say, 10,000), randomly assign birthdays to everyone in the room.  Count up the number of times at least two people have the same birthday on a trial, and then divide by the number of trials to get an estimated probability that two people share the same birthday for a given room size.

The skeleton code is at the bottom of this page.

Hint:  Try writing a function that can search through an array for a target value, but only search up to a specified number of people.


INPUT AND OUTPUT:

Part A:
The program must prompt the user to input the size of the array and then that number of data elements to fill the array.

The program must output the mean and standard deviation (the must be done from main, or a separate "output" function, and not from the functions that do the computations).

How it should look when you run the program:

mmartin@cs.csustan.edu:(~/cs1500/solo5) g++ solo5AM.cpp
mmartin@cs.csustan.edu:(~/cs1500/solo5) a.out
Enter the number of elements in your data (up to 100): 4

Enter 4 values,
I will compute the mean and standard deviation.
1
2
3
4
The mean is: 2.5
The Standard Deviation is: 1.29099

Y/y continues, any other quits.
y
Enter the number of elements in your data (up to 100): 4

Enter 4 values,
I will compute the mean and standard deviation.
2
4
6
8
The mean is: 5
The Standard Deviation is: 2.58199

Y/y continues, any other quits.
n



Part B:
Your output should look something like the following.  It won’t be exactly the same due to the random numbers:

For 2 people, the probability of two birthdays is about 0.002
For 3 people, the probability of two birthdays is about 0.0082
For 4 people, the probability of two birthdays is about 0.0163

For 49 people, the probability of two birthdays is about 0.9654
For 50 people, the probability of two birthdays is about 0.969


WHAT TO TURN IN:

Here is the list of things you have to turn in:

DUE DATES:

For the due dates, see the class schedule.


CODE FOR PART B

//birthday.cpp
//The birthday paradox is that there is a surprisingly high probability
//  that two people in the same room happen to share the same birthday. 
//By birthday, we mean the same day of the year (ignoring leap years),
//  but not the exact birthday including the birth year or time of day. 
//This program approximates the probability that two people in the same
//  room have the same birthday, for 2 to 50 people in the room. 
//
//This program uses an array of size 50 to hold the birthdays of
//  the people in the room.  Birthdays are just a value from 1-365.
//  If a duplicate value is detected in the array then two people
//  are said to have the same birthday.

#include <iostream>
#include <cstdlib>
#include <time.h>

const int NUM_TRIALS = 10000;

using namespace std;

// Function prototypes
bool SameBirthday(int birthdays[], int numpeople);

// ======================
//     main function
// ======================
int main()
{
// Variable declarations
   int trial;
   int people;
   int numMatches;
   int i;
   int birthdays[50];

// Initialize random number generator using
// the current value of the clock
   srand(time(NULL));

   for (people=2; people<=50; people++)
   {
// ----------------------------------------------------
// ----- ENTER YOUR CODE HERE -----
// ----------------------------------------------------

    // Run trials to see if people have the same birthday
    // reset number of matches

// ---------------------------------------------
// --------- END USER CODE --------
// ---------------------------------------------
    cout << "For " << people << " people, the probability of two " <<
        "birthdays is about " <<
        static_cast<double>(numMatches) / NUM_TRIALS << endl;
   }
}

// ======================
// SameBirthday
// Scans through the array of birthdays, up to numpeople,
// and returns true if any of these numbers are identical.
// ======================

// ----------------------------------------------------
// ----- ENTER YOUR CODE HERE -----
// ----------------------------------------------------


// ---------------------------------------------
// --------- END USER CODE --------
// ---------------------------------------------