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 5 ■ Programs with Repetition Logic

11. A contest was held for the promotion of SuperMarbles. Each contestant was

required to guess the number of marbles in a jar. Write a program to determine the

Grand Prize winner (ignoring the possibility of a tie) based on the following:

The first line of data contains a single integer (answer, say) representing the actual number of

marbles in the jar. Each subsequent line contains a contestant’s ID number (an integer), and an

integer representing that contestant’s guess. The data is terminated by a line containing 0 only.

The Grand Prize winner is that contestant who guesses closest to answer without exceeding it.

There is no winner if all guesses are too big.

Assume all data are valid. Print the number of contestants and the ID number of the winner,

if any.

12. The manager of a hotel wants to calculate the cost of carpeting the rooms in

the hotel. All the rooms are rectangular in shape. He has a file, rooms.in, which

contains data for the rooms. Each line of data consists of the room number, the

length, and breadth of the room (in meters), and the cost per square meter of the

carpet for that room. For example, the data line:

325 3.0 4.5 40.00

means that room 325 is 3.0 meters by 4.5 meters, and the cost of the carpet for that

room is $40.00 per square meter. The last line of the file contains 0 only, indicating the

end of the data.

Write a program to do the following, sending output to the file rooms.out:

• Print a suitable heading and under it, for each room, print the room number, the

area of the room, and the cost of the carpet for the room;

• Print the number of rooms processed;

• Print the total cost of carpeting all the rooms;

• Print the number of the room that will cost the most to carpet (ignore ties).

13. The price of an item is p dollars. Due to inflation, the price of the item is expected

to increase by r% each year. For example, the price might be $79.50 and inflation

might be 7.5%. Write a program which reads values for p and r, and, starting with

year 1, prints a table consisting of year and year-end price. The table ends when

the year-end price is at least twice the original price.

14. A fixed percentage of water is taken from a well each day. Request values for W

and P where

• W is the amount (in liters) of water in the well at the start of the first day

• P is the percentage of the water in the well taken out each day

138

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!