During session 6
Exercises
1. Odd or even
Create a text file which contains integers in separate lines. Write a program that reads the numbers one by one from the file and displays the numbers with some additional information, whether they are even or odd.
2. Summing
Upgrade the previous program so that the program calculates the sum of the odd numbers, the sum of the even numbers and the sum of all numbers.
3. Filtering names
The text file names.txt contains some names of the characters in Game of Thrones. Write a program that prompts the user for the last name and outputs all names (lines) of the characters with the same last name. The output lines must be enumerated. The last output line has to show the total number of characters with the same last name. For example, if the input is Stark, the output should be:
Enter family name: Stark 1. Sansa Stark 2. Arya Stark 3. Catelyn Stark 4. Bran Stark 5. Robb Stark 6. Talisa Stark 7. Ned Stark All together: 7
4. Upgraded budget
Upgrade the exercise budget as follows.
The list of invited people is stored in the text file. The host has made some remarks near each name:
? Anna + Peeter + Ursula ? Eva ? Juhan + Maria + Epp + Anu
where the '+' means that the person is coming and the '?' means that the person has not responded yet. Create a similar text file yourself!
Next write a program which prompts the user for the file name and calculates:
- the minimum budget for the party - take into account the people who have approved their participation
- the maximum budget for the party - take into account all guests regardless of their response.
Importantly, the party budget consists of the expenses on food (10 euros per person) and a room rent (a fixed price of 55 euros regardless of the number of people).
Hint: use your previous solution (including the function). Integrate the following functionalities into the program:
- the program prompts the user for the file name;
- the program reads information about the guests from the file;
- the program outputs the total number of the guest;
- the program outputs the number of people who have approved their participation in the party;
- the program uses the function to calculate the maximum budget for the party, and outputs the result;
- the program uses the function to calculate the minimum budget for the party, and outputs the result;
An example of the program output (in the file guests.txt there is a list of guests shown above):
Please enter the file name: guests.txt The number of invited people: 8 The number of people attending: 5 Maximum budget: 135 EUR Minimum budget: 105 EUR
5. Horizontal bars
Write a program which reads the numbers from the file and displays textual, horizontal bar diagram on the basis of the numbers.
For example, if the content of the file is as follows:
3 2 13 6 9 4 7
then the program output should be as follows:
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
At the end of the session go to Moodle and submit all programs written during the session.