08.01.2023 Views

Learn to Program with C_ Learn to Program using the Popular C Programming Language ( PDFDrive )

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 8 ■ Arrays

16. Write a function removethat, given a string str and a character c,

removes all occurrences of c from str. For example, if str contains

"brother,"remove(str,'r') should change str to "bothe."

17. Write a program to read English words and their equivalent Spanish words into

two arrays. Request the user to type several English words. For each, print the

equivalent Spanish word. Choose a suitable end-of-data marker. Modify the

program so that the user types Spanish words instead.

18. The number 27472 is said to be palindromic since it reads the same forwards or

backwards. Write a function that, given an integer n, returns 1 if n is palindromic

and 0 if it is not.

19. Write a program to find out, for a class of students, the number of families with

1, 2, 3, ... up to 8 or more children. The data consists of the number of children in

each pupil’s family, terminated by 0. (Why is 0 a good value to use?)

20. A survey of 10 pop artists is made. Each person votes for an artist by specifying

the number of the artist (a value from 1 to 10). Write a program to read the names

of the artists, followed by the votes, and find out which artist is the most popular.

Choose a suitable end-of-data marker.

21. The children’s game of ‘count-out’ is played as follows. n children (numbered 1 to n)

are arranged in a circle. A sentence consisting of m words is used to eliminate one

child at a time until one child is left. Starting at child 1, the children are counted

from 1 to m and the m th child is eliminated. Starting with the child after the one

just eliminated, the children are again counted from 1 to m and the m th child

eliminated. This is repeated until one child is left. Counting is done circularly and

eliminated children are not counted. Write a program to read values for n (assumed

<= 100) and m (> 0) and print the number of the last remaining child.

22. The prime numbers from 1 to 2500 can be obtained as follows. From a list of the

numbers 1 to 2500, cross out all multiples of 2 (but not 2 itself). Then, find the next

number (n, say) that is not crossed out and cross out all multiples of n (but not n).

Repeat this last step provided that n has not exceeded 50 (the square root of 2500).

The numbers remaining in the list (except 1) are prime. Write a program that uses

this method to print all primes from 1 to 2500. Store your output in a file called

primes.out. This method is called the Sieve of Eratosthenes, named after the

Greek mathematician, geographer, and philosopher.

23. There are 500 light bulbs (numbered 1 to 500) arranged in a row. Initially, they are

all OFF. Starting with bulb 2, all even numbered bulbs are turned ON. Next, starting

with bulb 3, and visiting every third bulb, it is turned ON if it is OFF, and it is turned

OFF if it is ON. This procedure is repeated for every 4th bulb, then every 5h bulb,

and so on up to the 500th bulb. Write a program to determine which bulbs are OFF

at the end of the above exercise.

There is something special about the bulbs that are OFF. What is it? Can you explain

why it is so?

242

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!