Problem Solving and Program Design in C

GLOBAL EDITION For these Global Editions, the editorial team at Pearson has collaborated with educators across the world to address a wide range of s...
Author: Guest
26 downloads 0 Views 4MB Size
GLOBAL EDITION

For these Global Editions, the editorial team at Pearson has collaborated with educators across the world to address a wide range of subjects and requirements, equipping students with the best possible learning tools. This Global Edition preserves the cutting-edge approach and pedagogy of the original, but also features alterations, customization, and adaptation from the North American version.

This is a special edition of an established title widely used by colleges and universities throughout the world. Pearson published this exclusive edition for the beneit of students outside the United States and Canada. If you purchased this book within the United States or Canada, you should be aware that it has been imported without the approval of the Publisher or Author.

Problem Solving and Program Design in C EIGHTH EDITION

Jeri R. Hanly • Elliot B. Koffman

this page intentionally left blank

252

Chapter 4 • Selection Structures: if and switch Statements

4. Write a program that reports the contents of a compressed-gas cylinder based on the first letter of the cylinder’s color. The program input is a character representing the observed color of the cylinder: “Y” or “y” for yellow, “O” or “o” for orange, and so on. Cylinder colors and associated contents are as follows: orange brown yellow green

ammonia carbon monoxide hydrogen oxygen

Your program should respond to input of a letter other than the first letters of the given colors with the message, Contents unknown. 5. An Internet Service Provider charges its subscribers every month based on the information provided in the following table: Data Usage (n), Gbs

Charges

0.0 6 n … 1.0

250

1.0 6 n … 2.0

500

2.0 6 n … 5.0

1000

5.0 6 n … 10.0

1500

n 7 10.0

2000

Given the amount of data used by the subscriber (i.e. n), write a program to calculate the charges to be paid by the subscriber. Print a message to indicate bad data as well. 6. Write a program that takes the x–y coordinates of a point in the Cartesian plane and prints a message telling either an axis on which the point lies or the quadrant in which it is found. y

QI I

QI

x

QIII

Sample lines of output: (-1.0, -2.5) is in quadrant III (0.0, 4.8) is on the y-axis

QI V

Programming Projects

253

7. Write a program that determines the day number (1 to 366) in a year for a date that is provided as input data. As an example, January 1, 1994, is day 1. December 31, 1993, is day 365. December 31, 1996, is day 366, since 1996 is a leap year. A year is a leap year if it is divisible by four, except that any year divisible by 100 is a leap year only if it is divisible by 400. Your program should accept the month, day, and year as integers. Include a function leap that returns 1 if called with a leap year, 0 otherwise. 8. Write a program that interacts with the user like this: (1) First Free Service (2) Second Free Service Enter the Free Service number>> 2 Enter number of Miles>> 3557 Vehicle must be serviced.

Use the table below to determine the appropriate message. Free Services

Miles (k)

First Service

1500 6 k … 3000

Second Service

3001 6 k … 4500

9. Chatflow Wireless offers customers 600 weekday minutes for a flat rate of 39.99. Night (8 p.m. to 7 a.m.) and weekend minutes are free, but additional weekday minutes cost 0.40 each. There are taxes of 5.25% on all charges. Write a program that prompts the user to enter the number of weekday minutes, night minutes, and weekend minutes used, and calculates the monthly bill and average cost of a minute before taxes. The program should display with labels all the input data, the pretax bill and average minute cost, the taxes, and the total bill. Store all monetary values as whole cents (rounding the taxes and average minute cost), and divide by 100 for display of results. 10. Write a program to control a bread machine. Allow the user to input the type of bread as W for White and S for Sweet. Ask the user if the loaf size is double and if the baking is manual. The following table details the time chart for the machine for each bread type. Display a statement for each step. If the loaf size is double, increase the baking time by 50%. If baking is manual, stop after the loaf-shaping cycle and instruct the user to remove the dough for manual baking. Use functions to display instructions to the user and to compute the baking time.

254

Chapter 4 • Selection Structures: if and switch Statements

BREAD TIME CHART Operation

White Bread

Sweet Bread

Primary kneading

15 mins

20 mins

Primary rising

60 mins

60 mins

Secondary kneading

18 mins

33 mins

Secondary rising

20 mins

30 mins

Loaf shaping

2 seconds

2 seconds

Final rising

75 mins

75 mins

Baking

45 mins

35 mins

Cooling

30 mins

30 mins

11. The table below shows the normal boiling points of several substances. Write a program that prompts the user for the observed boiling point of a substance in °C and identifies the substance if the observed boiling point is within 5% of the expected boiling point. If the data input is more than 5% higher or lower than any of the boiling points in the table, the program should output the message Substance unknown. Substance

Normal Boiling Point (°C)

Water

100

Mercury

357

Copper

1187

Silver

2193

Gold

2660

Your program should define and call a function within_x_percent that takes as parameters a reference value ref, a data value data, and a percentage value x and returns 1 meaning true if data is within x% of ref—that is, (ref – x% * ref) data (ref + x% * ref). Otherwise within_x_percent would return zero, meaning false. For example, the call within_x_percent(357, 323, 10) would return true, since 10% of 357 is 35.7, and 323 falls between 321.3 and 392.7.

Repetition and Loop Statements CHAPTER OBJECTIVES • To understand why repetition is an important control structure in programming • To learn about loop control variables and the three steps needed to control loop repetition • To learn how to use the C for, while, and do-while statements for writing loops and when to use each statement type • To learn how to accumulate a sum or a product within a loop body • To learn common loop patterns such as counting loops, sentinel-controlled loops, and flag-controlled loops • To understand nested loops and how the outer loop control variable and inner loop control variable are changed in a nested loop • To learn how to debug programs using a debugger • To learn how to debug programs by adding diagnostic output statements

CHAPTER

5

I

loop a control structure that repeats a group of steps in a program

n your programs so far, the statements in the program body execute only once. However, in most commercial software that you use, you can repeat a process many times. For example, when using an editor program or a word processor, you can move the cursor to a program line and perform as many edit operations as you need to. Repetition, you’ll recall, is the third type of program control structure (sequence, selection, repetition), and the repetition of steps in a program is called a loop. In this chapter, we describe three C loop control statements: while, for, and do-while. In addition to describing how to write loops using each statement, we describe the advantages of each and explain when it is best to use each one. Like if statements, loops can be nested, and the chapter demonstrates how to write and use nested loops in your programs.

5.1 Repetition in Programs

loop body the statements that are repeated in the loop

Just as the ability to make decisions is an important programming tool, so is the ability to specify repetition of a group of operations. For example, a company that has seven employees will want to repeat the gross pay and net pay computations in its payroll program seven times, once for each employee. The loop body contains the statements to be repeated. Writing out a solution to a specific case of a problem can be helpful in preparing you to define an algorithm to solve the same problem in general. After you solve the sample case, ask yourself some of the following questions to determine whether loops will be required in the general algorithm: 1. 2. 3.

Were there any steps I repeated as I solved the problem? If so, which ones? If the answer to question 1 is yes, did I know in advance how many times to repeat the steps? If the answer to question 2 is no, how did I know how long to keep repeating the steps?

Your answer to the first question indicates whether your algorithm needs a loop and what steps to include in the loop body if it does need one. Your answers to the other questions will help you determine which loop structure to choose for your solution. Figure 5.1 diagrams the relationship between these questions and the type of loop you should choose. Table 5.1 defines each of the kinds of loops you may need and refers you to the sections(s) of this chapter where you will find implementations of these loops.