Functions: Chap 3
1. Mid Val:
User enters three doubles; they're sent to a func, which returns the
middle value (in terms of size).
5 points
2. Caps:
Enter a lower case letter, send it to a func, and return the capital
version of that letter. 5
points
3. d-v-t program:
Prompt the user for a distance traveled, average speed, and travel
time. The user will enter 2 of the 3,
entering a zero for the quantity that is unknown. Call one of three functions (depending on the unknown) to compute
the unknown. 5 points
4. Prime numbers:
The user enters two numbers, n1 and n2, and a function is called that
prints all the prime numbers, p, such that n1 <= p <= n2. Determine if a number is prime using a loop
and a test for divisibility. For full
credit the program must be efficient.
10 points
5. Voting program:
Create at least four candidates for whom an unlimited number of voters
can cast ballots. To cast a ballot for
a particular candidate, a certain number or letter should be entered. After each vote, call a tally function that
updates the vote count for the candidates.
Enter D or ten to display the current status of each candidate via a
function call. When Q or zero is
entered another function should be called that tells us that the polls have
closed. This function should also call
the display function so we can see the final results, and it should declare a
winner. Note: Although it is not considered a good programming practice, you
may use global variables to represent the # of votes for each candidate. 20 points
6. Sum of squares:
Make an iterative (normal) func with a loop that prints it for all even
#'s from 1 to an input val. Make
another func (without a loop) that does this recursively (pg 172). 10 points
7. Peanut butter:
Enter the size of the pb jar in ounces and right next to it the price in
dollars. On the next line the user
enters the same data for another brand.
Do this until the user enters zero for size or price. After each pair of numbers is entered a unit
price func should be called that contains a static var (pg 169). The static var will contain the price per
ounce and will only be overwritten if the new price per ounce is the lowest so
far. Print which brand is the best deal. 10 points
8. Guessing game:
Computers thinks of a rand num from 1 up to some max that the human
chooses. Human guesses. Computer says too high or too low, AND hot
or cold (hot means within 10% of right answer), and counts how many guesses the
human needs. 15 pts.
9. Free throw simulation: Human tells computer how good a free throw shooter he is (as a
%). Computer estimates the probability
of human making at least N free throws out of ten, where human enters N. It should do so by calling a trial function
that generates 10 rand #'s, one for each shot.
Each shot should be tested to see whether it is a basket or a miss,
based on the human's shooting %. The
trial func counts the # of baskets & returns a value that represents
whether the trial was successful or not (based on N). Using a loop, run about 10 thousand trials. The approx probability is the # of
successful trials over the total # of trials.
Note: This technique, called the
Monte Carlo method, is very powerful, especially in highly complex situations
in which calculating the theoretical probability is extremely difficult. Good math students should calculate the
theoretical probability and compare it to their simulated results (see me for
help). 25 pts.