Example programming exercises
1. Snowflake
Write a program that prompts the user for an odd number n. Create a data structure which holds a matrix of size n x n. Fill each element of the matrix with character ".". Then the program has to fill the middle row, the middle column and the diagonals with character "*" (the result should resemble a snow flake). The program should output the matrix in rows and columns (as shown in the example). The elements of the matrix must be delimited by a single space.
An example of the program output:
Please enter an odd number: 5 * . * . * . * * * . * * * * * . * * * . * . * . *
One more example of the program output:
Please enter an odd number: 11 * . . . . * . . . . * . * . . . * . . . * . . . * . . * . . * . . . . . * . * . * . . . . . . . * * * . . . . * * * * * * * * * * * . . . . * * * . . . . . . . * . * . * . . . . . * . . * . . * . . . * . . . * . . . * . * . . . . * . . . . *
2. Countries and cities
Countries and cities are listed in file countries.txt. On each line, there is the name of a country and some cities of the country. For example,
USA Boston Pittsburgh Washington Seattle UK London Edinburgh Cardiff Belfast
Write a program that repeatedly prompts the user for the name of a city and outputs the country in which the city is located. If the user enters a city that is not mentioned in the file, then the program outputs "No information about the city ...". The program continues asking the user for a city until the user enters “done”.
An example of the program output (using the above-mentioned file):
Please enter the name of a city: Cardiff Cardiff is in UK Please enter the name of a city: Seattle Seattle is USA Please enter the name of a city: Moscow No information about Moscow Please enter the name of a city: London London is in UK Please enter the name of city: done
3. Children height
Nowadays, it's possible to calculate the expected height of a child on the basis of parents' height. The paediatricians use the following formulas to predict the height of a child:
for a boy:
for a girl:
First, write a function child_height, which
- takes mother's height in centimeters as the first argument;
- takes father's height in centimeters as the second argument;
- the third argument is child's sex (M or F);
- returns the expected height of the child in centimeters.
The data must be held in a list of tuples. The first element of a tuple is mother's height, the second element is father's height, and the third element is child's gender.
Example:
[(163, 175, 'F'), (170, 180, 'M'), (153, 184, 'M'), (177, 165, 'F'), (166, 183, 'M')]
Write a program which
- finds the expected heights of children in centimeters using function child_length,
- outputs children's heights,
- outputs an average height of all the children.
An example of the program output:
1. Child's height is 162.5 cm. 2. Child's height is 181.5 cm. 3. Child's height is 175.0 cm. 4. Child's height is 164.5 cm. 5. Child's height is 181.0 cm. An average height of all the children is 172.9 cm.