CSCE 235

Homework Assignment 7 (Programming) – Solution

Assigned:  March 4, 2009

Due: 12:30 p.m. March 13, 2009

 (Homework 5 minutes late will not be accepted)

 

Principle of Inclusion-Exclusion

 

1.      (15 points)  How many elements are in  if there are 20 elements in , 13 elements in , and

(a)  (2 points)

(b)  (3 points)

(c)    (5 points)

(d)   (5 points)

 

2.   (10 points)  Of the 125 teams auditioning for the Wombat Best Dance Crew contest:

·         61 teams have training in classical dance

·         70 teams have training in modern dance

·         52 teams have training in ballroom dance

A number of teams will be picked to compete in the Finals.  To qualify for the Finals, a team must have at least training in one of the above dance styles.   If there are:

·         34 teams that have training in both classical dance and modern dance,

·         34 teams that have training in both classical dance and ballroom dance,

·         39 teams that have training in both modern dance and ballroom dance,

·         24 teams that have training in all three dances.

How many teams do NOT qualify for the Finals?

 

3.   (15 points)  A flower shop (that sells only roses, tulips, and lilies) releases a report of its 100 customers who bought flowers in March 2009.  Every customer bought at least one type of flowers. 

·         43 customers bought roses

·         50 customers bought tulips

·         13 customers bought roses and tulips

·         10 customers bought roses and lilies

·         15 customers bought lilies and tulips

·         6 customers bought roses, tulips and lilies

Determine the numbers of customers who bought

(a)    (2 points) lilies

(b)   (3 points) roses and tulips, but not lilies

(c)  (5 points) roses or tulips, but not both

(d) (5 points) more than one type of flowers

 

The Addition and Multiplication Rules

 

4.   (10 points)  You are the manager of a talk show and you are in charge of selecting Oscar-nominated actors/actresses to appear on the show. 

 

      You are given the following list of actors:  Richard Jenkins, Frank Langella, Sean Penn, Brad Pitt, and Mickey Rourke. 

 

      You are given the following list of actresses:  Anne Hathaway, Angelina Jolie, Melissa Leo, Meryl Streep, Kate Winslet

 

(a)    (3 points) In how many ways can an actor and an actress be selected to appear on the show?

(b)   (2 points) In how many ways can an actor or an actress selected to appear on the show? 

 

5.   (10 points) Using only the alphabet letters A, B, C, D, and E, 

(a)    How many two-letter combinations can be formed? 

(b)   How many three-letter combinations can be formed? 

(c)    How many four-letter combinations can be formed? 

(d)   How many two- or three- or four-letter combinations can be formed?

(e)    How many four-letter combinations can be formed if the second letter must be either D or E?

 

6.   (20 points)  A standard deck of 52 playing cards has four suits (spades, hearts, clubs, and diamonds).  Each suit has 13 cards, from 2 to 10 and J, Q, K, and Ace.  In how many ways can one draw from a standard deck of 52 playing cards:

      (a)  (3 points) A club or a diamond or a heart? 

(b)   (3 points) A king (K) or a queen (Q) or a jack (J)? 

(c)    (3 points) A card numbered 6 through 10? 

(d)   (3 points) A card numbered 6 through 10 or a king?

(e)    (5 points) Two cards of different suits?

(f)    (8 points) Two cards: the first card an ace or a king; and then without replacing the first card back into the deck, the second card an ace or a king again? 

 

The Pigeon-Hole Principle

 

7.   (15 points)  Forty buses are to be used to transport 2500 fans from Lawrence, Kansas to Lincoln, Nebraska for a college football game. Each bus has 80 seats.  Assume one seat per passenger.

(a)    Prove that one of the buses will carry at least 63 passengers.

(b)   Prove that one of the buses will have at least 18 empty seats.

 

8.   (10 points)  You are a magician in front of an audience who are exclusively residents of Nebraska.  You tell the audience that there are at least four people in the audience who live in the same county in Nebraska!  What is the minimum number of people must there be in the audience for your “magic” to work?  (Note: You will need to find out the number of counties in Nebraska.)

 

Programming

 

9.   (50 points)  Toolbox/Application.  An online bookstore is building an accounting program to keep track of the number of customers that buy different genres of books.  They have a text-based database.   

 

 

3 100 80 60 20 10 10 5

1 50

2 40 10 10

0                    

3 99 100 1000 50 40 30 25 

999

 

      In the above, if a line starts with the number 999, then it is the end of file.  Otherwise, a line is defined as follows.  Each line is the purchase patterns of for a period of time for the bookstore.  The first number of each line indicates the number of genre types of books sold.  Suppose that number is n.  Then, it is followed by  entries.  Each entry is the number of customers who bought certain genre types of books and the entries following the order of the number of genre types.  For example, in the above, for the first period of time, there are three genre types, 100 customers bought books of type 1, 80 of type 2, 60 of type 3, 20 of types 1 and 2, 10 of types 1 and 3, 10 of types 2 and 3, and finally 5 of all three types.  In the second period of time, there is only one genre type, with 100 customers who bought books of type 1.  And so forth.

 

      As you can see, this is a poorly designed database as it is only book-centric, instead of customer-centric.  The bookstore is now having trouble determining the actual number of customers for each time period.  For example, for the first period, how many customers are there in total? 

 

      Your task is to write a program that reads an input file of the above format, generates an output file that each line states the number of customers in total for each time period.  So, for the above examples:

 

  

205

50

40

0

1104                 

 

      You may assume that the number of genre types will not exceed 998.

 

      Important: 

(a)    Your program must compile and run on CSE platforms (Windows or Linux).

(b)   Your program should be documented well.

(c)    Make sure that you have a README file to describe how to compile and run your program.

 

Problems based on (Rosen 2003, Goodaire and Parmenter 2002).