• Home
  • Textbooks
  • You Can Do It!: A Beginners Introduction to Computer Programming
  • How Many ...? in Which Set and Map Lend a Hand

You Can Do It!: A Beginners Introduction to Computer Programming

Francis Glassborow

Chapter 13

How Many ...? in Which Set and Map Lend a Hand - all with Video Answers

Educators


Chapter Questions

Problem 1

The process of sieving numbers has some interesting features in number theory. Another sieve starts as for the Sieve of Eratosthenes but this time sieves only numbers that have not been eliminated. It starts very much the same: you remove every second number after two. Next you remove every third number that is left apart from three itself. Next you look for the next number (let us call it $n$ ) that has not been eliminated so far and then eliminate every $n$th still un-eliminated number and so on.

Try to write a program to carry out this process. Be warned that it is considerably less elegant than the one for primes.

Check back soon!

Problem 1

Write a program that collects a list of items from the user and writes them out to a file in alphabetical order.

Check back soon!

Problem 2

Read the names of twenty objects (such as "cat", "chair", etc.) from a file into a std: : set<string> container one by one. Display each object in the console window scrolling the screen up by five lines between each object and allowing an interval of half a second between each line scroll (fgw: :Wait (500); will provide a half second pause). Continue scrolling after the last object until the console is blank. Ask the user to type in the names of the objects. Remove each correct answer from the container. Allow two minutes for their answers. At the end, print a list of any remaining items.

For this exercise you will need the clock $\mathrm{O}$ function that is declared in the ectimes header. It returns a value that depends on how long a program has been running. The return type is clock_t and, for MinGW, it measures time in thousandths of a second. So the following statement outputs the time the program has been running, in thousandths of a second:
cout $\ll$ clockO $\ll{ }^{\prime} \backslash n '$;

The following statement saves the program time in the variable called start:
clock_t const start(clock $\mathrm{O})$;

Do not confuse this with facilities you may read about to extract and manage calendar time. clockO is entirely to do with measuring time since program start.

There are many ways that you can polish this program. You could store the user's answers in another container and display them at the end. You could tell them when they have got one right (one way to detect that is by checking the return value of erase $\mathrm{O}$ which will be true if an element was removed).
Keep the objects simple so that typing errors are not going to be a problem.

Check back soon!

Problem 3

Modify your program for Exercise 2 so that it displays 20 pictures from .png files (remember 512 by 512 pixels in a 256-color palette) whose names (without the .png suffix) are provided by reading the file objects.txt. The names of the files are also the names of the objects. Display each picfure for a fixed time. Complete the program as before.

Check back soon!

Problem 4

Try one or more of your programs on a friend, colleague or relation. Get some feedback from them and try to improve the program in response to the user comments.

Check back soon!

Problem 5

Modify the program for Task 41 so that your telephone book is written out to a file. Then write a program that will read the telephone book in from a file.

Check back soon!

Problem 6

Write a menu-driven telephone-book program. Your program should eventually provide for reading and writing the data to a file, looking up someone's phone number, adding new people, adding more data from another file, removing someone and updating a telephone number. The erase $\mathrm{O}$ member function may be useful for removing entries.

Check back soon!

Problem 7

Create a file that contains the tifles of pictures and the names of the files containing them. The files will have to be ones that are suitable for loading into the Playpen window (i.e. they will have to be .PNG files of $\mathbf{5 1 2}$ by $\mathbf{5 1 2}$ pixels with a 256 -color palette).

Write a program that reads the file into a std: :map<string, string>, displays the picture titles and then invites the user to choose one, which is then displayed.

Check back soon!

Problem 8

Improve your telephone-book program so that it checks that a name you try to add isn't already there (which could lead to accidental overwriting of data if two people have the same name). If the name is already there, the program should ask you how to proceed.

Check back soon!

Problem 9

Write a program that uses a map<string, int> and reads words from a text file which it stores in the map, updating the value part to keep track of how many times the word has been used. It then outputs an alphabetical list of all the words used in the file together with a count of how many times each word is used. The output should be a word followed by the number of times it was used.

Check back soon!

Problem 10

Think of at least one more example of a map and implement it. If you think of an idea that is not already listed on this book's website please write the program and then send both the idea and the program in and get yourself credited with it.

Check back soon!