Uploaded by Alex HC

C++ HW3 Part2

advertisement
EE2310 C++程式設計 HW3 (Ch 6 Functions)
Part 1 (40%) To be answered online in eLearn.
A. Choice 選擇題: 50%
B. Multiple choices: 10%
C. True/False 是非題: 40%
Part2 Coding (60%) (Submit your codes to eLearn according to rules
posted)
1. Celsius- Fahrenheit Temperature Table (10%)
The formula for converting a temperature from Fahrenheit to Celsius is
5
∁= 9 (𝐹 − 32)
where F is the Fahrenheit temperature and C is the Celsius temperature. Write two
functions named CtoF and FtoC that accepts a Celsius/Fahrenheit temperature as an
argument and return the temperature, converted to Fahrenheit/Celsius, respectively.
Demonstrate the functions by calling them in a loop that displays two tables of the
Fahrenheit temperatures 0 through 20 and their Celsius equivalents, and vice versa.
2. Present Value (10%)
Suppose you want to deposit a certain amount of money into a savings account, and
then leave it alone to draw interest for the next 10 years. At the end of 10 years you
would like to have $10,000 in the account. How much do you need to deposit today to
make that happen? To find out you can use the following formula, which is known as
the present value formula
𝐹
𝑃=
(1 + 𝑟)𝑛
The terms in the formula are as follows:
• P is the present value, or the amount that you need to deposit today.
• F is the future value that you want in the account (in this case, $10,000).
• r is the annual interest rate (expressed in decimal form, such as .042).
• n is the number of years that you plan to let the money sit in the account.
Write a program with a function named presentValue that performs this calculation.
The function should accept the future value, annual interest rate, and number of years
as arguments. It should return the present value, which is the amount that you need to
deposit today. Demonstrate the function in a program that lets the user experiment
with different values for the formula’s terms
3. Multiple Stock Sales Profit (20%)
The profit from the sale of a stock can be calculated as follows:
Profit = ((NS ∗ SP) − SC) − ((NS ∗ PP) + PC)
where NS is the number of shares, SP is the sale price per share, SC is the sale
commission paid, PP is the purchase price per share, and PC is the purchase
commission paid. If the calculation yields a positive value, then the sale of the stock
resulted in a profit. If the calculation yields a negative number, then the sale resulted
in a loss.
Write a function that accepts as arguments the number of shares, the purchase price
per share, the purchase commission paid, the sale price per share, and the sale
commission paid of a stock. The function should return the profit (or loss) from the
sale of that stock.
Then use this function in a program that calculates the total profit or loss from the sale
of multiple stocks. The program should ask the user for the number of stock sales, and
the necessary data for each stock sale. It should accumulate the profit or loss for each
stock sale and then display the total.
4. Transient Population (20%)
In a population, the birth rate is the percentage increase of the population due to births
and the death rate is the percentage decrease of the population due to deaths. The
population will also be effected by people moving into or out of a geographic area..
Write a program that asks for the following:
• The starting size of a population
• The annual birth rate
• The annual death rate
• The number of individuals that typically move into the area each year (arrivals)
• The number of individuals that typically leave the area each year (departures)
• The number of years to display
The program should then display the starting population and the projected population
at the end of each year. It should use a function that calculates and returns the
projected new size of the population after a year. The formula is
N = P(1 + B)(1 − D) + A - D
Where N is the new population size, P is the previous population size, B is the birth
rate, and D is the death rate. A the number of individuals moving in, and D the
number of individuals leaving.
( Do not accept numbers less than 2 for the starting size.
accept negative numbers for birth rate or death rate. Do not accept numbers less than
1 for the number of years. Do not accept negative numbers for birth rate, death rate,
arrivals or departures.)
The program should project what the population will be numYears from now. You
can either prompt the user to input a value for numYears, or you can set it within the
program.
5. Life/Death Predictor (20%)
The Social Security Administration maintains an actuarial life table that contains the
probability that a person in the United States will die
(http://www.ssa.gov/OACT/STATS/table4c6.html). The death probabilities for 2009
are stored in the file LifeDeathProbability.txt that is included on the website
for the book. There are three values for each row: the age, death probability for a
male, and death probability for a female. For example, the first five lines are:
0
0.006990 0.005728
1
0.000447 0.000373
2
0.000301 0.000241
3
0.000233 0.000186
4
0.000177 0.000150
This says that a 3 year old female has a 0.000186 chance of dying.
Write a program that inputs an age and sex from the keyboard in the main function.
The main function should call a function named simulate (that you must write),
sending in the age and sex as parameters. The function should simulate to what age a
person will live by starting with the death probability for the given age and sex. You
can do this by reading the data from the file row by row. Skip rows that are less than
the input age. Once the input age is reached, generate a random number between 0-1
and if this number is less than or equal to the corresponding death probability then
predict that the person will live to the current age and return that age. If the random
number is greater than the death probability then increase the age by one and repeat
the calculation for the next row in the file.
If the simulation reaches age 120 then stop and predict that the user will live to 120.
The main function should output the simulated age for when the person will die. This
program is merely a simulation and will give different results each time it is run,
assuming you change the seed for the random number generator.
6. Duel (20%)
In the land of Puzzlevania, Aaron, Bob, and Charlie had an argument over which one
of them was the greatest puzzler of all time. To end the argument once and for all,
they agreed on a duel to the death. Aaron was a poor shooter and only hits his target
with a probability of 1/3. Bob was a bit better and hits his target with a probability of
1/2. Charlie is an expert marksman and never misses. A hit means a kill and the
person got hit drops out of the duel.
To compensate for the inequities in their marksmanship skills, it is decided that the
contestants would fire in turns starting with Aaron, followed by Bob, and then by
Charlie. The cycle would repeat until there was one man standing. And that man
would be remembered for all time as the Greatest Puzzler of All Time.
An obvious and reasonably strategy is for each man to shoot at the most accurate
shooter still alive, on the grounds that this shooter is the deadliest and has the best
chance of hitting back.
Write a program to simulate the duel using this strategy. Your program should use
random numbers and the probabilities given in the problem to determine if a
shooter hits the target. You will likely want to create multiple subroutines and
functions to complete the problem. For example, you can define three functions for
the three persons performing their shooting results, respectively. Take Aaron’s
function for example, first generate a random number and then decide whether his
shooting will result in a kill. If yes, then set one of the other two person to deadth state
according to the strategy. Once you can simulate a single duel, add a loop to your
program that simulates 10,000 duels. Count the number of times that each contestant
wins and print the probability of winning for each contestant (e.g. for Aaron your
program might output "Aaron won 3595/10000 duels or 35.95 %").
An alternate strategy is for Aaron to intentionally miss on his first shot. Modify the
program to accommodate this new strategy and output the probability of winning for
each contestant. What strategy is better for Aaron, to intentionally miss on the first
shot or to try and hit the best shooter?
Download