Problem Solving and Program Design in C

Similar documents
THE UNIVERSITY OF TRINIDAD & TOBAGO

QUIZ-II QUIZ-II. Chapter 5: Control Structures II (Repetition) Objectives. Objectives (cont d.) 20/11/2015. EEE 117 Computer Programming Fall

What is a Loop? Pretest Loops in C++ Types of Loop Testing. Count-controlled loops. Loops can be...

Chemistry Worksheet: Matter #1

Math Journal HMH Mega Math. itools Number

Unit 13 Handling data. Year 4. Five daily lessons. Autumn term. Unit Objectives. Link Objectives

ALGORITHMS AND FLOWCHARTS

Welcome to the topic on creating key performance indicators in SAP Business One, release 9.1 version for SAP HANA.

DC Circuits (Combination of resistances)

Final Software Tools and Services for Traders

Excel Tutorial. Bio 150B Excel Tutorial 1

9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements

CSCI 1100 Computer Science 1 Homework 1 Calculations and Functions

Pantone Matching System Color Chart PMS Colors Used For Printing

An easy way to remember the relationship between reminders and recurring tasks is illustrated below. FLEETMATE Reminder System Hierarchy

PROG0101 Fundamentals of Programming PROG0101 FUNDAMENTALS OF PROGRAMMING. Chapter 3 Algorithms

I PUC - Computer Science. Practical s Syllabus. Contents

Photosynthesis. Grade-Level Expectations The exercises in these instructional tasks address content related to the following grade-level expectations:

UEE1302 (1102) F10 Introduction to Computers and Programming

EasyC. Programming Tips

Question: Do all electrons in the same level have the same energy?

Instructor Özgür ZEYDAN (PhD) CIV 112 Computer Programming

Algorithm and Flowchart Structured Programming 1

Name: Class: Date: 10. Some substances, when exposed to visible light, absorb more energy as heat than other substances absorb.

TIMESHEET EXCEL TEMPLATES USER GUIDE. MS-Excel Tool User Guide

Charlesworth School Year Group Maths Targets

PHYSICAL AND CHEMICAL PROPERTIES AND CHANGES

what operations can it perform? how does it perform them? on what kind of data? where are instructions and data stored?

Assessment Management

Lesson/Unit Plan Name: Patterns: Foundations of Functions

Applied. Grade 9 Assessment of Mathematics LARGE PRINT RELEASED ASSESSMENT QUESTIONS

Creating Advanced Reports with the SAP Query Tool

Section 3-3 Approximating Real Zeros of Polynomials

Logarithmic and Exponential Equations

Excel 2003 Tutorials - Video File Attributes

7 th Grade Integer Arithmetic 7-Day Unit Plan by Brian M. Fischer Lackawanna Middle/High School

Repetition Using the End of File Condition

Five daily lessons. Page 23. Page 25. Page 29. Pages 31

Commission Accounting User Manual

Part 1 Foundations of object orientation

Week 2 Practical Objects and Turtles

Prism 6 Step-by-Step Example Linear Standard Curves Interpolating from a standard curve is a common way of quantifying the concentration of a sample.

4 Mathematics Curriculum

Job Cost Report JOB COST REPORT

DATA 301 Introduction to Data Analytics Microsoft Excel VBA. Dr. Ramon Lawrence University of British Columbia Okanagan

Two-way selection. Branching and Looping

EXAMPLES OF ASSIGNING DEPTH-OF-KNOWLEDGE LEVELS ALIGNMENT ANALYSIS CCSSO TILSA ALIGNMENT STUDY May 21-24, 2001 version 2.0

Exponential Notation and the Order of Operations

Physics 210 Q ( PHYSICS210BRIDGE ) My Courses Course Settings

HR Diagram Student Guide

Writing Control Structures

Test Bank - Chapter 3 Multiple Choice

Indicator 2: Use a variety of algebraic concepts and methods to solve equations and inequalities.

Visual Logic Instructions and Assignments

PROTONS AND ELECTRONS

Federal Income Tax Information January 29, 2016 Page Federal Income Tax Withholding Information - PERCENTAGE METHOD

ABOUT THIS DOCUMENT ABOUT CHARTS/COMMON TERMINOLOGY

Factoring Trinomials of the Form x 2 bx c

Database Programming with PL/SQL: Learning Objectives

EMC Publishing. Ontario Curriculum Computer and Information Science Grade 11

Physical and Chemical Changes Pre Test Questions

POLARITY AND MOLECULAR SHAPE WITH HYPERCHEM LITE

(Least Squares Investigation)

States of Matter and the Kinetic Molecular Theory - Gr10 [CAPS]

To Multiply Decimals

This class deals with the fundamental structural features of proteins, which one can understand from the structure of amino acids, and how they are

OPERATING INSTRUCTIONS Model ST-888 DTMF ANI/ENI Display Decoder

Welcome to Introduction to programming in Python

OEM Manual MODEL 2350 ELECTRONIC DUAL CYLINDER SCALE

VIRTUAL LABORATORY: MULTI-STYLE CODE EDITOR

Study the following diagrams of the States of Matter. Label the names of the Changes of State between the different states.

WEDNESDAY, 4 MAY AM AM. Date of birth Day Month Year Scottish candidate number

Systems of Linear Equations in Three Variables

3. In what part of the chloroplast do the light-dependent reactions of photosynthesis take place? Chloroplast. Name Class Date

Oracle Database: SQL and PL/SQL Fundamentals

Algebra Unit Plans. Grade 7. April Created By: Danielle Brown; Rosanna Gaudio; Lori Marano; Melissa Pino; Beth Orlando & Sherri Viotto

Chapter 2: The Chemical Context of Life

Using Excel as a Management Reporting Tool with your Minotaur Data. Exercise 1 Customer Item Profitability Reporting Tool for Management

Python Lists and Loops

2010 Software Development GA 3: Written examination

Lab 3 - DC Circuits and Ohm s Law

The Pointless Machine and Escape of the Clones

Lecture 2 Mathcad Basics

Formulas and Problem Solving

COMP 250 Fall 2012 lecture 2 binary representations Sept. 11, 2012

Have questions? Feel free to contact us to help you create your custom suite order. WEDDING COLLECTION PRICING HOW TO BUILD YOUR CUSTOM SUITE:

KS3 Computing Group 1 Programme of Study hours per week

Lesson 4: Solving and Graphing Linear Equations

14:440:127 Introduction to Computers for Engineers. Notes for Lecture 06

UNIT V. Earth and Space. Earth and the Solar System

MATHEMATICS GRADE 2 Extension Projects

CNC Transfer. Operating Manual

KINDERGARTEN 1 WEEK LESSON PLANS AND ACTIVITIES

1. Use the class definition above to circle and identify the parts of code from the list given in parts a j.

EHR: Scavenger Hunt I

Introduction to LogixPro - Lab

If you require more information about any of the services mentioned in this guide, please visit bell.ca/wirelessprepaid.

Lab 2 Biochemistry. Learning Objectives. Introduction. Lipid Structure and Role in Food. The lab has the following learning objectives.

Transcription:

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 QI V Sample lines of output: (-1.0, -2.5) is in quadrant III (0.0, 4.8) is on the y-axis

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 5 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

loop a control structure that repeats a group of steps in a program In 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. Were there any steps I repeated as I solved the problem? If so, which ones? 2. If the answer to question 1 is yes, did I know in advance how many times to repeat the steps? 3. 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.