[1.1] When you use an automated teller machine (ATM) with your bank card, you need to use a personal identification number (PIN) to access your account. If a user fails more than three times when entering the PIN, the machine will block the card. Assume that the user’s PIN is “1234” and write a program that asks the user for the PIN no more than three times, and does the following:
- If the user enters the right number, print a message saying, “Your PIN is correct”, and end the program.
- If the user enters a wrong number, print a message saying, “Your PIN is incorrect” and, if you have asked for the PIN less than three times, ask for it again.
- If the user enters a wrong number three times, print a message saying “Your bank card is blocked” and end the program.
[1.2] French country names are feminine when they end with the letter e, masculine otherwise, except for the following which are masculine even though they end with e:
- le Belize
- le Cambodge
- le Mexique
- le Mozambique
- le Zaïre
- le Zimbabwe
Write a program that reads the French name of a country and adds the article: le
for masculine or la
for feminine, such as le Canada
or la Belgique
. However, if the country name starts with a vowel, use l’
; for example, l’Afghanistan
.
For the following plural country names, use “les”:
- les Etats-Unis
- les Pays-Bas
[1.3] Write a program that asks the user for an integer and then prints out all its factors. For example, when the user enters 150, the program should print
2
3
5
5
[1.4] Write an application to pre-sell a limited number of cinema tickets. Each buyer can buy as many as 4 tickets. No more than 100 tickets can be sold. Implement a program that prompts the user for the desired number of tickets and then displays the number of remaining tickets. Repeat until all tickets have been sold, and then display the total number of buyers.
[2.1] A simple "random" generator is obtained by the formula
-
$\alpha$ = 32310901 -
$\beta$ = 1729 -
$m$ = 224
[2.2] The Drunkard’s Walk. A drunkard in a grid of streets randomly picks one of four directions and stumbles to the next intersection, then again randomly picks one of four directions, and so on. You might think that on average the drunkard doesn’t move far because the choices cancel each other out, but that is actually not the case. Represent locations as integer pairs (x, y). Implement the drunkard’s walk over 100 intersections, starting at (0,0), and print the ending location.
[2.3] Predator-Prey Simulation. The Lotka-Volterra equations describe a predator-prey ecological model that is based on a set of fixed positive constants:
- The growth rate of prey
- The rate of killing of prey by predators
- The mortality rate of predators
- The rate of increase of predators through the consumption of prey
Considering these constants, the following conditions hold in this model:
- The population of prey
$x$ :- increases at rate
$dx=Ax \ dt$ (proportional ot the number of prey) - is simultaneously destroyed by predators at a rate
$dx=-B\ xy\ dt$ (proportional to the product of the numbers of prey and predators)
- increases at rate
- A population of predators
$y$ :- decreases at a rate
$dy = -C \ y \ dt$ (proportional to the number of predators) - increases at a rate
$dy = D \ xy \ dt$ (proportional to the product of the number of prey and predators)
- decreases at a rate
From these conditions, the following system of equations is derived:
This means that considering two time periods n
and n+1
the variation in the number of populations of prey (x
) and predators (y
) from one period to another is given by
$x_{n+1} = x_n \times (1+A - B\times y_n)$ $y_{n+1} = y_n \times (1-C + D\times x_n)$
Write a program that asks the user the input values of the four constants, the initial number of prey and predators’ populations, and the number of periods to be simulated. After, the program calculates and display the number of the two populations in each of the periods considered. As test input, use
[2.4] Electrical transformers. Transformers are often built by winding coils of wire around a ferrite core. The figure illustrates a situation that occurs in various audio devices such as cell phones and music players and music players. In this circuit, a transformer is used to connect a speaker to the output of an audio amplifier to the output of an audio amplifier.
R0 = 20Ω 1:n
.----/\/\/\-----*------. .-------*------------.
| \ || / | /|
| | || | ___ / | ~~~
----- Vs = 40V / || \ Rs = 8Ω | |/ | ~~~
--- \ || / | |\ | ~~~
| | || | |___| \ | ~~~
| / || \ | \|
.---------------*------. .-------*------------.
Amplifier Transformer Speaker
The symbol used to represent the transformer is intended to suggest two coils of wire. The parameter n of the transformer is called the “turns ratio” of the transformer. (The number of times that a wire is wrapped around the core to form a coil is called the number of turns in the coil. The turns ratio is literally the ratio of the number of turns in the two coils of wire.) When designing the circuit, we are concerned primarily with the value of the power delivered to the speakers—that power causes the speakers to produce the sounds we want to hear. Suppose we were to connect the speakers directly to the amplifier without using the transformer. Some fraction of the power available from the amplifier would get to the speakers. The rest of the available power would be lost in the amplifier itself. The transformer is added to the circuit to increase the fraction of the amplifier power that is delivered to the speakers. The power, Ps, delivered to the speakers is calculated using the formula
Write a program that models the circuit shown and varies the turns ratio from 0.01 to 2 in 0.01 increments, then determines the value of the turns ratio that maximizes the power delivered to the speakers.