C For Engineers and Scientist
C For Engineers and Scientist
-~
r</>~
:'0
f.., r"'\
'>
...,
----
\..
.~"
~.y~
I "
',;r
,~ .-
('
:...
\ '- r"\
<', (-
,"
,.
'",
[.
~~
A~
~~-
~"
Cr<
(. ." I'
t',
C';-,
'~;
-,',;
~I) ~r
!~(',..., r
/);
'-0-
/('
(' '\ \ ..
/OA
I,
~-/<...\,
,,(' y
'0
~/'
"
f'\
r'
/:.
" C; ,
"
" .1('
1_
'<'(c' r
;'('
f ,
r"\
('/"
(
"
''''f'<<.'"
~rartab{lity., Portability determines 'the degrelt() whlcl).a prog;am written. on A) /.;1." \. " ." ," ( ,,/.(-' It: ,one computet &stem e.at;!pe m:,?vedto ally. other sxstem> ~enerally,C source 'f ~ 'code canl?e moved acrosstpmputer:systemswith little or no modification. For :<::).-C C X~ ~:ample, C~ource rode written oria""UNIl based \hl~chine'c~.usually be \c ,t: compil~~ on (~~.'~ PS<or mairt.tr~me wit~ alm6~no modificat'iclns required <.. ' (' %')<, in:~he code. Withtpe, availaBility of inexpensive compi1~rsth~t run on personal "', com.puters, this, portability has,meant that programs d~veloRed at home can be used\~Il'PCS, ~mmis,andinainfr~mes atW6rk. ~ , ,. r" ;;f~ A ,.~ Ve;sdiiiity. D'ep~i;l4ing6~'Cthe uset's orientation, e:'prograrris ~an be written ,', ,-' ~S: for real-time'process'control, fOx. engineering analysis work,.for statistical appli~ (C~:(;. '/catioris/~9 develqp, database.ap~li2ations;to c(msb1ict,.op~&ting systeI)lS,or to A' r wt)!eFOR~ c~mpi1ers~n:? BASIC)nterPreters. \..C~.... (: ~., .(-;~ %)<S A" For thesJ.reasons, lasmore'aIl9.more;:people h~ve triedC, they have found, " (f'rl /''(qt to be a~profesSional langu~ge haVing an"~nusual'-;ange of capabilities that is': vA.: "-.,.-'" ('(, [~ r: )-l/~ enjowble t<;>(work With. Thus, C.has'jqined FQR~ and COBQL as a m~jor /"'/"*"_ /"~,~ ,~ z""" <: ,,< r progl'aming language'for Jfdusfry: .-1',.,... .;.r, :,,, (','C': ~~, ~'", In reeognition)6fthe ifnpqrtant';lnd wid~pread s,ci7~tificrengineerin:g,and, ~ C'n cOOF;..rciaI11S5~pttncelf e:fl.\e Ameri~~n Nati~~al StaJ,1dardsIn~ti~te (ANSI) '":/~~ :' J.-' _j: began ~t,abliShii\g.a standard for the language, simy.~rto"sta~dards 'previously,{ . 1';),%, !Ci:' "established/or FO~~~N:"ap.d COBP}' As noted in the forew()r~ to th~1989z' C'(\ draf(~f the AI;!SI sta~dard, '~1J.'~ nee~ for a's!I1gle, cleaFly-defined standard '" .... IJ,<), ,,)'~ad a~s~~ in/t~e, C co~~unity,.due hi' a,:}apidly ,~xpanq.ing use:'ot.the' C !C;,:' r 'programming lang~ge and;th~ varie,ty,.. of differing translator implementations' /': AI' ~.., that ha~ beenJnd we:e}>eing develop~d,:'~The st~dard was officially rati(ied ' C'~, ('~a.s,Amer~~ .Nati~~al St~nd~rd X3.}59-198~'~~D~mber 14,1989 'and ,subse''j,,"'/:. qU~I\tly pUD!J~hed ~.t~e SPIJt'8 of 1990. "~;.'; (" C:/,. ' A r 'Because bfte's gr6tving importance in-the applications;and academic areas, /;.. ~th~ predecessor':b';.this t~X!,~4 Fi~rt'!3aoka/t2f~as wri'tt;~. The primarfl?urpo~,~, ::~ C~, ~":" .. " o:<.t~s eetrli!f editi~p.,", was~to maKe::\C;: m:o~'~~ccessible as applications,t:':j'\ y \.X' _programmirig(l~nguage~thfn wa~~possible with,texts thaf focused OIl C as/a~l1 f A/)",; advanced"operatihg.syst6ns langu~fge. A second edition of~A First Book a(e" ~~'- ('0 irtililded ~xpand~~fcoveraie:of.soft\.vare engirl'eering roncepts: This pertItitted C~ .,the boo,k,to be;l1J~as .~~ introauction( to ,:progra~ing.rn' ?-dditi6h,.to b~ing ~.",". A'/" )-~ / ail introduction 'tb .c;:. Thi~;t~xt retains the' style ~nd' teachingorierttation of' ~ . '~"(., /<;.:~/. its predeces~or but. is"spedfical~ o;i~~ted tow.~r,! engine~ring"and scien~ic ., (r:: applicati'ons. r. >_. v, (.\ "" ( \-'''~./2 r .;(). . ,(,.. '~ /.7'. /' !~ \ if. 1'-. ("" /' r ' ~t'l. \ ,.., 'r:.:. /0,,'"'' '\ ~~ / Disth;ctive Fe{titres of This A'" .r~. "'"'.. ,/,'...... . ~'" .. ~~ ~'>~ c,,_ ~-'r-~._ K> \. Wntmg Style. I firmly.,beheve that mtroductory texts' do not teach students r \,;.:-. i. profesJors te<ach, studefits. An iritJ;odudory textb601<,if is. to be' ~seful, 6\ust ~ r A/;~/. 9~~the/primarY ('spppO?ti(la~ct~?:t~ the III~~dingrolf" ot th~ profeS$~f'O~te \... I),. <,' ~' the -profesS9r/.sets the stage,;,.t\Owever,~thetextbook 1n~t encour;age, nurture, r\ and' assist the, .student .-in acquiring and. ,'~ owning'~ the' nlat~ria( presented. in '%J./'~ r- ~ ," class. To go thi~ the ~text'~l1st be Wtl!!en in a,manne}' .thatrr~a~es s~se to the. < t'
. ~:z
r""<:
-: ~",',
/'~,I'\
,.
,.'\
~0 <. ~.;:'
.....""
. 'A';
A/""
~..,.
\",./~
,~)(
~
'..~:,
'~ , \. ~
/'
.<
r /'
I
-b
'-:-/,
y'
/:'
'. ~ ~
f"
.i."
'>01
'r"'
\.
eN
0f
'"'f.
<~.
('. ~f'
an
(b
/"
'i
,~
I""*<
.........
:~
B~Sk
I'
"
~ ::.;
f"
it
"/' ('
/"
,.. /..
,".,
f
j--..."
..,.......
/'
r'
\.''/"'.
t< .
.'
I'
'
'\
<'~ (r
~,;
,.-
j-"
AA
r
/'-:r~
C'~ )
"'-. .. j~J
f/('
.<
C't~.\
~~
r,
,'-"
">
"/\ 'A~........
\.
<: (':.
~X;
r--
<'';:' 'Of'"'
\ .X:/
Preface
student. One of the distinctive features of this book is that it has been written for the student. As one of the reviewers has said, "This book addresses the student and not the professional." Thus, first and foremost, I feel the writing style used to convey the concepts presented is the most important aspect of the text. Modularity. C, by its nature, is a modular langUage. Thus, the connection between C functions and modules is made early in the text, in Section 1.2, and continues throughout the book. To stress the modular nature of C, the first complete main () function illustrates calling four other functions. The first program that can be compiled is then presented, which calls the printf () function. The idea of argument passing into modules is also made early, in Section 1.3, with the use of the printf () function. In this manner, students are introduced to functions and argument passing as a natural technique of programming. Software Engineering. Rather than simply introduce students to C, this text introduces students to the fundamentals of software engineering. This introduction begins with Section 1.1, which introduces algorithms and the various ways that an algorithm can be described. An example illustrating three algorithms for summing the numbers from. 1 to 100 (Figure 1-4) is used to make the discussion of algorithms more tangible to students. The increased emphasis on software engineering is supported by a section (Section 1.5) on top-down program development. Here the importance of understanding the problem and selecting an appropriate algorithm is highlighted and the relationship between analysis, design, coding, and testing introduced. Problem solving within this context is stressed throughout the text. Applications. Engineering and scientific examples are used throughout the text to illustrate the concepts presented. In addition, the majority of the chapters have a section consisting of two specific applications relating to the material presented in the chapter. Many of the applications are of the "tried and true" variety and are not unique to this book. However, some interesting new applications have been added, such as the study of acid rain, the calculation of pollen counts, the operation of telephone switching networks, and the construction of a user-written random number generator that are not typically found in introductory texts. Additionally, Chapter 9 is completely devoted to numerical applications and is a mini-introduction to numerical techniques in and of itself. Emphasis on ANSI C. Although ANSI C is emphasized throughout the text, pre-ANSI C constructs are also shown for those of you who will be using a non-ANSI compiler. Generally, the major difference of note is in Chapter 6, where user-written functions are introduced. For these functions ANSI C requires a single function header line that includes argument declarations, while pre-ANSI compilers require argument declarations on a separate line. Also, the declaration of functions in ANSI C, called function prototypes, includes the data types of all arguments, while pre-ANSI C omits the argument declarations. Introduction to Pointers. One of the unique features of this text is its method of introducing pointers. This is done by first using the printf () function to
ix
Preface
display a variable's address so that the student can "see" what an address is. This approach, which was used in a A First Book of C, always seemed a more logical and intuitive method of understanding pointers than the alternative indirection description with no display. Since the publication of A First Book of C, the use of the print f () function to display addresses has become a standard way of introducing pointers. Although this approach, therefore, is no longer a unique feature of the book, I am very proud of its presentation, and continue to use it in this text. Program Testing. Every single C program in this text has been successfully compiled and run under Borland's Turbo C Compiler. The programs have been written using features fully supported under ANSI C. A source diskette of all programs is available to adopters. Pedagogical Features To facilitate my goal of making C accessible as a first level course, I have continued to use the following pedagogical features: End of Section Exercises. Almost every section in the book contains numerous and diverse skill builder and programming exercises. Additionally, solutions to selected exercises are provided in an appendix. Pseudocode and Flowchart Descriptions. As in A First Book of C, pseudocode is stressed throughout the text. Although flowcharts were used in A First Book of C, no explicit definition or introduction to flowchart symbols was presented. In this edition we have added additional material on flowchart symbols and the use of flowcharts in visually presenting flow-of-control constructs. Common Programming Errors and Chapter Review. Each chapter ends with a section on common programming errors and a review of the main topics covered.
Appendices and Supplements An expanded set of appendices has been provided in C for Engineers and Scientists. In addition to the three appendices taken from A First Book of C on Operator Precedence, ASCII codes, and I/O-Standard Error Redirection, the new appendices contain material on Program Life Cycle; using the DOS, UNIX, VAX, and PRIME operating systems; using Borland's Turbo C Compiler; and using Microsoft's C Compiler. Additionally, a printed solutions manual is available containing solutions (with comments) to programming exercises not included in Appendix H of the text. A so~rce diskette of solutions to all programs in the book is available to adoptors of the text.
Acknowledgments
Acknowledgments
C for Engineers and Scientists is a direct result of the success (and the limitations) of its predecessor, A First Book of C. In this regard, my most heartfelt acknowledgment and appreciation goes to the instructors and students who found the earlier edition to be of service to them in their respective quests to teach and learn C. Once an edition devoted to engineering and scientific applications was planned, its completion depended on many people other than myself. For this I especially want to thank the staff of West Publishing Company for their many contributions. These included the continuous faith and encouragement of my editor, Richard Mixter, and developmental editor, Keith Dodson, and the many suggestions and enthusiastic work of the production editor, Thomas ModI, and promotion manager, Ellen Stanton. The direct encouragement and support of my dean at Fairleigh Dickinson University, Dr. Paul Lerman, must also be acknowledged. Without his support and the support of my chairman, Dr. G. Naadimuthu, this text could not have been written. I also wish to express my gratitude to the individual reviewers listed on the next page. Each of these people provided suggestions and critical comments that have made an invaluable contribution to the quality of this text. Finally, I wish to acknowledge the patience, understanding, and love provided by my wife and partner, Rochelle.
Gary Bronson
xi
Acknowledgments
The author thanks these reviewers for their knowledgeable help in the completion of this book. Farzan Abdolsalami Trinity University Farrokh Atlarzadeh University of Houston-University Lorraine Callahan Park Paul I-Hai Lin Indiana University/Purdue at Fort Wayne Robert D. Logcher Massachusetts Institute of Technology Luegina C. Mounfield Louisiana State University David Richards Loyola College
University
(formerly) Northern Arizona University Katy Disney Mission College David Eberly
University of North Carolina at Chapel Hill Howard Silver Rhonda Ficek . Fairleigh Dickinson University Moorhead State University Roy J. Fuller University of Arkansas at Fayetteville Gordon W. Hoagland Ricks College Susan M. Simons Memphis State University Neil R. Sorensen Weber State University Donald Yee Mesa Community College
xii
Applications
Applications
Pendulum Clocks Telephone Switching Networks Acid Rain Sine Approximations Solving Quadratic Equations Data Validation Curve Plotting Data Scaling Random Network Generation Coin Toss Simulation Pollen Counting Master jTransaction File Updating Root Finding Numerical Integration Average and RMS Values Battery Charging Linked List Maintenance Dynamic Storage Allocation Data Encryption
xiii
Contents
Contents
CHAPTER ONE Getting Started 3
1.1 Introduction to Programming 4 Algorithms 6 From Algorithms to Programs 8 Program Translation 9 1.2 Introduction to Modularity 12 Functions 13 The main () Function 15 1.3 The printf () Function 18 1.4 Programming Style 22 Comments 23 1.5 Common Programming Errors 25 1.6 Chapter Summary 26 1.7 Enrichment Study: Computer Hardware and Storage 27
xv
I
Contents
2.2 Variables and Declaration Statements 45 Declaration Statements 47 Multiple Declarations 49 Specifying Storage Allocation 51 2.3 Assignment Statements 55 Assignment Variations 59 Accumulating 60 Counting 62 2.4 Formatted Output 69 Format Modifiers 72 Other Number Bases 73 2.5 Top-Down Program Development 78 Step 1: Determine the Desired Output 79 Step 2: Determine the Input Items 79 Step 3a: Determine an Algorithm 80 Step 3b: Do a Hand Calculation 80 Step 3c: Select Variable Names 80 Step 4: Write the Program 80 Step 5: Test the Output 81 Modularity and Top-Down Design 81 2.6 Applications 85 Application 1: Pendulum Clocks 85 Application 2: Telephone Switching Networks 87 2.7 Common Programming Errors 92 2.8 Chapter Summary 93 2.9 Enrichment Study: Variable Addresses and Memory Storage 94
xvi
'j
Contents
4.2 The if-else Statement 150 Compound Statements 152 One-Way Selection 154 Caution 155 4.3 Nested if Statements 161 The if-else Chain 162 4.4 The switch Statement 172 4.5 Applications 177 Application 1: Solving Quadratic Equations 177 Applic~tion 2: Data Validation 181 4.6 Common Programming Errors 185 4.7 Chapter Summary 187 4.8 Enrichment Study: A Closer Look at Errors, Testing, and Debugging 189
xvii
I Contents
357
Contents
9.3 Numerical Integration 388 Rectangular Approximations 388 Modified Rectangular Approximations 392 Trapezoidal Approximations 395 Simpson's Method 398 Application: Finding Average and RMS Values 402 9.4 Common Programming Errors 407 9.5 Chapter Summary 408
xix
Contents
Dynamic Storage Allocation 505 Unions 512 Common Programming Errors 514 Chapter Summary 515
Appendixes
551
A. Program Entry, Compilation, and Execution under the DOS, UNIX, VAX-VMS,and PRIME Operating Systems 552 B. Using Borland's Turbo C Compiler 559 C. Using Microsoft's Quick C Compiler 563 D. Living with an Old Compiler 567 E. Operator Precedence Table 574 F. ASCII Character Codes 576 G. Floating Point Number Storage 577
Index 637
xx
,)
Getting Started
Chapter One
1.1
Introduction to Programming 1.2 Introduction to Modularity 1.3 The printf ( ) Function 1.4 Programming Style 1.5 Common Programming Errors 1.6 Chapter Summary 1.7 Enrichment Study: Computer Hardware and Storage
Chapter One
Getting Started
guage.
On a fundamental level, all computer programs do the same thing (Figure
1-1). They direct a computer to accept data (input), to manipulate the data (pro-
cess), and to produce reports (output). This implies that all computer programming languages must provide essentially the same capabilities for performing these operations. These capabilities are provided either as specific instruction types or as "prepackaged" groups of instructions that can be called to do specific tasks. In C/ the "prepackaged" groups of instructions are called library functions. Table 1-1 lists the fundamental set of instructions and library functions provided by FORTRAN, BASIC, COBOL, Pascal, and C for performing input, processing, and output tasks. If all programming languages provide essentially the same features, why are there so many of them? The answer is that there are vast differences in the types of input data, calculations needed, and output reports required by various applications. For example, scientific and engineering applications require high-precision numerical outputs/ accurate to many decimal places. In addition, these applications typically use many algebraic or trigonometric formulas to produce their results. For example, the determination of a rocket' s reentry point, as illustrated in Figure 1-2/ requires a trigonometric formula and a high degree of numerical accuracy. For such applications, the FORTRAN programming language, with its algebralike instructions, was initially developed. FORTRAN, whose name is an acronym derived from FORmula TRANslation, was introduced in 1957. Business applications usually deal in whole numbers, representing inventory quantities, for example, or dollars and cents data accurate to only two decimal places. These applications require simpler mathematical calculations than are needed for scientific applications. The outputs required from business programs frequently consist of reports containing extensive columns of neatly formatted dollars and cents numbers and totals (see Figure 1-3). For these applications the
FIGURE 1-1
Input Data
Process the
Output
Results
Data
1.1
Introduction to Programming
TABLE 1-1
Operation
Fundamental
Programming
BASIC
FORTRAN
READ
INPUT READ/DATA
READ ACCEPT
READ READLN
IF/ELSE
IX)
WRITE PRINT
WRITE DISPLAY
COBOL programming language, with its picture output formats, is an ideal language. COBOL, which was commercially introduced in the 1960s, stands. for COmmon Business Oriented Language. Teaching programming to students has its own set of requirements. Here, a relatively straightforward, easy-to-understand language is needed that does not require detailed knowledge of a specific application. Both the BASIC and Pascal programming languages were developed for this purpose. BASIC, which stands for Beginners All-purpose Symbolic Instruction Code, was developed in the 1960s at Dartmouth College. BASIC is ideal for creating small, easily developed, interactive programs. Pascal was developed in the late 1970s to provide students with a firmer foundation in modular and structured programming than could be provided by
FIGURE 1-2 FORTRAN Was Developed for Scientific and Engineering Applications
y
Chapter One
Getting Started
INVENTORY Item No. 10365 10382 10420 10436 10449 10486 Description 360KB 720KB 1.2MB 1.44MB 20MB 40MB Diskette Diskette Diskette Diskette Cartridge Cartri
FIGURE 1-3
BASIC. Modular programs consist of many small subprograms, each of which performs a clearly defined and specific task that can be tested and modified without disturbing other sections of the program. The name Pascal is not an acronym, like the words FORTRAN, COBOL, and BASIC;the language is named after the seventeenth-century mathematician Blaise Pascal. The Pascal language is so rigidly structured, however, that there are no escapes from the structured modules when such escapes would be useful. This is unacceptable for real-world projects, and is one of the reasons that Pascal has not become widely accepted in the scientific, engineering, and business fields. The design philosophy called structured programming that led to the development of Pascal does have relevance to us as C programmers, however. Using a structured programming approach results in readable, reliable, and maintainable programs. We introduce the elements of this program design philosophy in the next section and continue to expand upon it and use it throughout the text. The C language was initially developed in the 1970s by Ken Thompson, Dennis Ritchie, and Brian Kernighan at AT&T Bell Laboratories. C evolved from a language called B, which was itself developed from the language BCPL. C has an extensive set of capabilities and is a true general-purpose programming language. As such, it can be used for creating simple, interactive programs or highly sophisticated and complex engineering and scientific programs, within the context of a truly structured language. An indication of C's richness of library and instruction capabilities is clearly evident in Table 1-1. Not only does C initially provide many "tools" to build programs with, but as we shall see, it also provides the programmer with the ability to easily create new "tools" to add to the existing library routines. For this reason C has become known as the professional programmer's language. Algorithms Before a program is written, the programmer must have a clear understanding of what the desired result is and how the proposed program is to produce it. In this regard, it is useful to realize that a computer program describes a computational procedure. In computer science, a computational procedure is called an algorithm. More specifically, an algorithm is defined as a step-by-step sequence of instructions that describes how a computation is to be performed. In essence, an algorithm answers the question, "What method will you use to solve this computational problem?" Only after we clearly understand the algorithm and know the specific
1.1
Introduction to Programming
steps required to produce the desired result can we write the program. Seen in this light, programming is the translation of the selected algorithm into a language that the computer can use. To illustrate an algorithm, we shall consider a simple requirement. Assume that a program must calculate the sum of all whole numbers from 1 through 100. Figure 1-4 illustrates three methods we could use to find the required sum. Each method constitutes an algorithm. Clearly, most people would not bother to list the possible alternatives in a detailed step-by-step manner, as is done in Figure 1-4, and then select one of the algorithms to solve the problem. But then, most people do not think algorithmically; they tend to think intuitively. For example, if you had to change a flat tire on your car, you would not think of all the steps required-you would simply change the tire or call someone else to do the job. This is an example of intuitive thinking. Unfortunately, computers do not respond to intuitive commands. A general
FIGURE 1-4
Method 1. Columns: Arrange the numbers from 1 to 100 in a column and add them: 1
2 3 4
98 99
+100 5050 Method 2. Groups: Arrange the numbers in convenient groups that sum to 100. Multiply the number of groups by 100 and add in any unused numbers: 0+ 100 = 100 1 + 99 = 100 2 + 98 = 100 3 + 97 = 100 50 groups
49 + 50 +
2
where
n = number of terms to be added (100) a = first number to be added (1) b = last number to be added (100)
Sum
100 (1 + 100)
5050
Chapter One
Getting Started
statement such as "add the numbers from 1 to 100" means nothing to a computer, because the computer can only respond to algorithmic commands written in an acceptable language such as C. To program a computer successfully, you must clearly understand this difference between algorithmic and intuitive commands. A computer is an "algorithm-responding" machine; it is not an "intuitive-responding" machine. You cannot tell a computer to change a tire or to add the numbers from 1 through 100. Instead, you must give the computer a detailed step-by-step set of instructions that, collectively, forms an algorithm. For example, the set of instructions
Set n equal to 100 Seta = 1 Set b equal to 100 Calculate sum Print the sum
_n_(a_+_b_) 2
forms a detailed method, or algorithm, for determining the sum of the numbers from 1 through 100. Notice that these instructions are not a computer program. Unlike a program, which must be written in a language the computer can respond to, an algorithm can be written or described in various ways. When English-like phrases are used to describe the algorithm (the processing steps), as in this example, the description is called pseudocode. When mathematical equations are used, the description is called a formula. When pictures that employ specifically defined shapes are used, the description is referred to as a flowchart. A flowchart provides a pictorial representation of the algorithm using the symbols shown in Figure 1-5. Figure 1-6 illustrates the use of these symbols in depicting an algorithm for determining the average of three numbers. Because flowcharts are cumbersome to revise, the use of pseudocode to express the logic of an algorithm has gained increasing acceptance in recent years among programmers. Unlike flowcharts, where standard symbols are defined, there are no standard rules for constructing pseudocode. In describing an algorithm using pseudocode, any short English phrases may be used. For example, acceptable pseudocode for describing the steps needed to compute the average of three numbers is:
Input the three numbers into the computer Calculate the average by adding the numbers and dividing the sum by three Display the average
Only after an algorithm has been selected and the programmer understands the steps required can the algorithm be written using computer-language statements. When computer-language statements are used to describe the algorithm, the description is called a computer program. From Algorithms to Programs After an algorithm has been selected, it must be converted into a form that can be used by a computer. The conversion of an algorithm into a computer program, using a language such as C, is called coding the algorithm (see Figure 1-7). Much of the remainder of this text is devoted to showing you how to develop algorithms and express those algorithms in C.
1
1.1 Introduction to Programming
FIGURE 1-5
FlowchartSymbols
SYMBOL
NAME
DESCRIPTION
(~-)
Terminal
Input/Output
Process
Flow Lines
Used to connect the flowchart symbols and indicates the logic flow
Decision
Loop
Predefined Process
Connector
Program Translation Once a program is written in C it still cannot be executed on a computer without further translation. This is because the internal language of all computers consists of a series of Is and Os, called the computer's machine language. To generate a machine-language program that can be executed by the computer requires that the C program, which is referred to as a source program, be translated into the computer's machine language (see Figure 1-8).
Chapter One
Getting Started
End
FIGURE 1-6
The translation into machine language can be accomplished in two ways. When each statement in the source program is translated individually and executed immediately, the programming language used is called an interpreted language, and the program doing the translation is called an interpreter. , When all of the statements in a source program are translated before anyone statement is executed, the programming language used is called a compiled language. In this case, the program doing the translation is called a compiler. C is a compiled language. Here, the source program is translated as a unit into machine
FIGURE 1-7
Coding an Algorithm
Algorithm A step-by-step procedure Coding Translate into a computer language
Requirements
Program
1.2
Introduction to Programming
C
language source program
---"~I
Translation program
FIGURE 1-8
language. The machine-language version of the original source program is a separate entity; it is called the object program. (See Appendix A for a complete description of entering, compiling, and running a C program.)
Exercises 1.1
1. Define the terms: a. computer program b. programming c. programming language d. algorithm e. pseudocode
f.
1/
(Note: There is no one single correct answer for each of these tasks. This exercise is designed to give you practice in converting intuitive commands into equivalent algorithms and making the shift between the thought processes involved in the two types of thinking.)
10
Chapter One
Getting Started
Module 1
Module 2
Module 3
Module 4
Module 5
Module 6
1.2
Introduction to Modularity
11
Data In
Operations
on the
Data
./
Result Out FIGURE 1-10 A Module Must Accept Data, Process the Data, and Produce a Result
Functions It is useful to think of a function as a small machine that transforms the data it receives into a finished product. For example, Figure 1-11 illustrates a function that accepts two numbers as inputs and multiplies the two numbers to produce one output. One important requirement for designing a good function is to give it a name that conveys to the reader some idea about what the function does. Function names can be made up of any combination of letters, digits, or underscores (J selected according to the following rules: 1. The function name must begin with a letter or underscore. 2. Only letters, digits, or underscores may follow the initial letter. Blank spaces are not allowed; use the underscore to separate words in a name consisting of multiple words.
FIGURE 1-11
12
Chapter One
Getting Started
TABLE 1-2
Keywords do double else enum extern float for goto if int long register return short sizeof static struct switch typedef union unsigned void while
3. A function name cannot be one of the keywords listed in Table 1-2. (A keyword is a word that is set aside by the lanRuage for a special purpose and should only be used in a specified manner. ) 4. The maximum number of characters in a function name is computer dependent. However, all systems recognize at least eight characters. (The American National Standards Institute (ANSI) standard requires recognition of at least 31 characters) 5. All function names are followed by a single set of parentheses. Examples of valid C function names, including the required parentheses, are: !
deg_to_rad( bessell ( ) intersct ( ) mult_two ( ) add_nums ( ) find_max( ) slope ( ) density ( )
(begins with a number, which violates Rule 1) (contains a special character, which violates Rule 2) (this is a keyword, which violates Rule 3)
Besides conforming to the rules for naming functions, a good function name should also be a mnemonic. A mnemonic is a word or name designed as a memory aid. For example, the function name deg_to_rad ( ) is a mnemonic if it is the name of a function that converts degrees to radians. Here, the name itself helps to identify what the function does. Examples of valid function names that are not mnemonics are:
easy( ) c3po ( ) r2d2 ( ) the force ( ) mike(
Nonmnemonic function names should not be used because they convey no information about what the function does.
1 Keywords in C are also reserved words, which means they can only be used for a specified purpose.
1.2
Introduction to Modularity
13
taxes ()
gross_pay ( )
FIGURE 1-12
Notice that all function names have been typed in lowercase letters. This is traditional in C, although it is not absolutely necessary. Uppercase letters are usually reserved for named constants, a topic covered in Chapter 3. Note that C is a case-sensitive language. This means that the compiler distinguishes between uppercase and lowercase letters. Thus, in C, the names TOTAL, total, and Total represent three distinct and different names. For this reason, we will type all names in the same case, which traditionally is lowercase in C. The main ( ) Function Once functions have been named, we need a way to combine them into a complete program (see Figure 1-12). Notice that we have not yet described the actual writing of the functions. One of the nice features of C is that we can plan a program by first deciding what functions are needed and how they are to be linked together. Then we can write each function to perform the task it is required to do. To provide for the orderly placement and execution of functions, each C program must have one function called main ( ). The main ( ) function is sometimes referred to as a driver function, because it tells the other functions the sequence in which they are to operate (see Figure 1-13). Figure 1-14 illustrates a completed main ( ) function. The word main identifies the start of each C program. The braces, { and }, determine the beginning and end of the function body and enclose the statements making up the function. The statements inside the braces determine what the function does. Each statement inside the function must end with a semicolon (i ). The main ( ) function illustrated in Figure 1-14 consists of four statements. In this case, each statement is a command to execute another function. First the grossJ)ay ( ) function is called for execution. When grossJ)ay ( ) is finished, the taxes ( ) function is called. After the taxes ( ) function is completed, the netJ)ay ( ) function is called. Finally, the output ( ) function is executed. Although the functions grossJ)ay ( ), taxes ( ), netJ)ay ( ), and output ( ) must still be written, the main ( ) function is completed. After the four other functions are written, the program, consisting of main ( ), grossJ)ay ( ), taxes ( ), netJ)ay ( ), and output ( ), is complete. You will be naming and writing many of your own C functions. In fact, the rest of this book is primarily about the statements required to construct useful
14
Chapter One
Getting Started
main()
You go first
gross_ pay ( )
taxes ()
net-pay
()
output()-
FIGURE 1-13
. -
main ( )
{
FIGURE 1-14
functions and about how to combine the functions to form useful programs. Fortunately, however, many useful functions have already been written for us. In the next section we will use one of these functions to create our first working C program.
Exercises 1.2
1. State whether the following are valid function names. If they are valid, state whether they are mnemonic names (recall that a mnemonic function name conveys some idea about what the function's purpose). If they are invalid names, state why.
1.2
Introduction to Modularity
15
A12345 ( )
do( )
) )
12345 ( ) amount ( )
a. Write a C program that calls these functions in the order that they are listed. b. From the functions' names, what do you think each function might do? 3. Assume that the following functions have been written:
input ( ), salestax( ), balanee( ), ealebill( )
a. Write a C program that calls these functions in the order that they are listed. b. From the functions' names, what do you think each function might do?
4. Create valid mnemonic names for functions that do the following: a. Find the average of a set of numbers. b. Find the area of a rectangle. c. Find the value of a polynomial. d. Find the density of a steel door. e. Find the maximum value of a set of numbers. f. Sort a set of numbers from lowest to highest.
16
Chapter One
Getting Started
lOa. A national electrical supply distribution company desires a computer system to prepare its customer invoices. The system must, of course, be capable of creating each day's invoices. Additionally, the company wants the capability to retrieve and output a printed report of all invoices that meet certain criteria; for example, all invoices sent in a particular month with a net value of more than a given dollar amount, all invoices sent in a year to a particular client, or all invoices sent to firms in a particular state. Determine three or four major program units into which the system could be separated. (Hint: One program unit is "Prepare Invoices" to create each day's invoices.) b. Suppose someone enters incorrect data for a particular invoice, which is discovered after the data has been entered and stored by the system. What program unit is needed to take care of correcting this problem? Discuss why such a program unit might or might not be required by most business systems. c. Assume a program unit exists that allows a user to alter or change data that have been incorrectly entered and stored. Discuss the need for including an "audit trail" that would allow for a later reconstruction of the changes made, when they were made, and who made them.
One of the most popular and useful prewritten functions in C is named print f ( ). This function, as its name suggests, is a print function that sends data given to it to the standard system display device. For most systems this display device is a video screen. This function prints out whatever is given to it. For example, if the message Hello there world! is given to printf ( ), this message is displayed on your terminal by the printf ( ) function. Inputting data or messages to a function is called passing data to the function. The message Hello there world! is passed to the printf ( ) function by simply putting the message inside the parentheses in the function's name (see Figure 1-15).
FIGURE 1-15
FIGURE 1-16
{ }
Body of Function
\
1.3 Theprintf( ) Function 17
The purpose of the parentheses in all function names is to provide a funnel through which information can be passed to the function (see Figure 1-16). The items that are passed to the function through the parentheses are called arguments of the function. Now let's put all this together into a working C program that can be run on your computer. Consider Program 1-1.
1, Program 1-1
#include main( )
{
<stdio.h>
printf("Hello
there
world!");
is a preprocessor command. Preprocessor commands begin with a pound sign, #, and perform some action before the compiler translates the source programinto machine code. Specifically, the #include preprocessor command causes the contents of the named file, in this case stdio. h, to be inserted where the #include command appears. The file stdio.h is referred to as a header file because it is placed at the top, or head, of a C program using the #include command. In particular, the stdio. h file provides a proper interface to the print f ( ) function, and must be included in all programs using printf ( ).2 As indicated in Program 1-1, preprocessor commands do not end with a semicolon. Following the preprocessor command is the start of the program's main ( ) function. The main ( ) function itself has only one statement. Remember that statements end with a semicolon (;). The statement in main ( ) calls the function printf ( ) and passes one argument to it. The argument is the message
Hello there world!
Since printf ( ) is a prewritten function, we do not have to write it; it is available for use just by calling it correctly. Like all C functions, printf ( ) was written to do a specific task, which is to print results. It is a very versatile function that can print results in many different forms. When a message is passed to printf ( ), the function sees to it that the message is correctly printed on your terminal, as shown in Figure 1-17. Messages are called strings in C because they consist of a string of characters made up of letters, numbers, and special characters. The beginning and end of a
Use of this statement is required under ANSI-standard C. In non-ANSI C the #include statement for stdio.h can usually be omitted.
2
18
Chapter One
Getting Started
Hello there world! FIGURE 1-17 The Output from Program 1-1
string of characters is marked by using double quotes ("message in here") around the string. Thus, to pass a message to printf ( )/ the string of characters making up the message must be enclosed in double quotes, as we have done in Program I-I. Let us write another program to illustrate printf ( )/s versatility. Read Program 1-2to determine what it does.
,101\
Program 1-2
#include main( )
{
<stdio.h>
When Program 1-2is run, the following is displayed: Computers, computers everywhere as far as I can C You might be wondering why the \n did not appear in the output. The two characters \ and n, when used together, are called a newline escape sequence. They tell printf ( ) to start on a new line. In C/ the backslash (\) character provides an "escape" from the normal interpretation of the character following it by altering the meaning of the next character. If the backslash was omitted from the second printf ( ) call in Program 1-2/ the n would be printed as the letter n and the program would print out: Computers, computers everywheren as far as I can C
Newline escape sequences can be placed anywhere within the message passed to pr in t f ( ). See if you can determine what the following program prints: #include main( )
{
printf("Computers
everywhere\n
as far
as\n\nI
can see");
1.3
The printf
( ) Function
19
Exercises 1.3
1 a. Using the pr in t f ( ) function, write a C program that prints your name on one line, your street address on a second line, and your city, state, and zip code on the third line. b. Compile and run the program you have written for Exercise 1a.
(Note: To do this you must understand the procedures for entering, compiling, and running a C program on the particular computer you are usingJ
2 a. Write a C program to print out the following: THE COSECANT OF AN ANGLE IS EQUAL TO ONE OVER THE SINE OF THE SAME ANGLE.
b. Compile and run the program you have written for Exercise 2a. 3 a. How many pr int f ( ) statements should be used to display the following:
DEGREES RADIANS 0.0000 1.5708 3.1416 4.7124 6.2832
o
90
180 270 360
b. What is the minimum number of printf ( ) statements that could be used to print the table in Exercise 3a? Why would you not write a program using the minimum number of printf ( ) function calls? c. Write a complete C program to produce the output illustrated in Exercise 3a. d. Run the program you have written for Exercise 3c on a computer. 4. In response to a newline escape sequence, printf ( ) positions the next displayed character at the beginning of a new line. This positioning of the next character actually represents two distinct operations. What are they?
5 a. Most computer operating systems provide the capability for redirecting the output produced by print f ( ) either to a printer or directly to a floppy or hard disk file. If
your computer supports output redirection, run the program written for Exercise 2a using this feature. Have your program's display redirected to a file named poem. b. If your computer supports output redirection to a printer, run the program written for Exercise 2a using this feature.
20
Chapter One
Getting Started
1.4 ProgrammingStyle
The word main in a C program tells the computer where the program starts. Since a program can have only one starting point, every C language program must contain one and only one main ( ) function. As we have seen, all of the statements that make up the main ( ) function are then included within the braces { }following the function name. Although the main ( ) function must be present in every C program, C does not require that the word main, the parentheses ( ), or the braces { } be placed in any particular form. The form used in the last section
main(
{
) statements in here;
program
was chosen strictly for clarity and ease in reading the program.3 For example, the following general form of a main ( ) function would also work:
main
(
statement;
Notice that more than one statement can be put on a line, or one statement can be written across lines. Except for messages contained within double quotes, function names, and reserved words, C ignores all white space (white space refers to any combination of one or more blank spaces, tabs, or new lines). For example, changing the white space in Program 1-1 and making sure not to split the message Hello there world! or the function names printf and main across two lines results in the following valid program:
#include main
(
<stdio.h>
Although this version of main ( ) does work, it is an example of extremely poor programming style. It is difficult to read and understand. For readability, the main ( ) function should always be written in standard form as:
If one of the program statements was a call to printf preprocessor command would have to be used.
( ), the #include
<stdio. h>
1.4
Programming Style
21
main( )
{
program
statements
in here;
In this standard form the function name starts in column 1 and is placed with the required parentheses on a line by itself. The opening brace of the function body follows on the next line and is placed under the first letter of the function name. Similarly, the closing function brace is placed by itself in column 1 as the last line of the function. This structure serves to highlight the function as a single unit. Within the function itself, all program statements are typically indented at least two spaces, although indentation is not required. Indentation is another sign of good programming practice, especially if the same indentation is used to align similar groups of statements. Review Program 1-2 to see that the same indentation was used for both pr in t f ( ) function calls. As you progress in your understanding and mastery of C, you will develop your own indentation standards. Just keep in mind that the final form of your programs should be consistent and should always serve as an aid to the reading and understanding of your programs. Comments Comments are explanatory remarks made within a program. When used carefully, comments can be very helpful in clarifying what the complete program is about, what a specific group of statements is meant to accomplish, or what one line is intended to do. Any line of text bounded by asterisks and enclosed within slashes U) is a comment. For example,
/* this is a comment */ /* this program prints out a message */ /* this program calculates a square root
*/
are all comment lines. The symbols / *, with no white space between them, designate the start of a comment. Similarly, the symbols * / , as a single unit with no intervening white space, designate the end of a comment. Comments can be placed anywhere within a program. They have no effect on program execution. The computer ignores all comments-they are there strictly for the convenience of anyone reading the program. A comment can be written either on a line by itself or on the same line containing a program statement. Program 1-3 illustrates the use of comments within a program.
}O),
Program 1-3
#include main()
{
prints
a message
*/ ) */
printf("Hello
there world!");
/* a call to printf(
22
Chapter One
Getting Started
The first comment appears on the same line as the function name and describes what the program does. This is generally a good location to include a short comment describing the program's purpose. If more comments are required, they can be placed, one per line, between the function name and the opening brace that encloses the function's statements. If a comment is too long to be contained on one line, it can be continued across two or more lines as illustrated below:
/* this comment is used to illustrate a comment that extends over two lines */
Note, however, that under older C compilers this usually results in an error. For these compilers the comment must be split into two or more comments, with each separate comment enclosed within its own comment symbol set / * */ as follows:
/* this comment is used to illustrate a */ /* comment that is split into two comments */
Under no circumstances (may comments be nested (one comment contains another comment within itself). For example,
/* this nested
comment
is /* always
*/
invalid
*/
Typically, many comments are required when using nonstructured programming languages. These comments are necessary to clarify either the purpose of the program itself or individual sections and lines of code within the program. In C, the program's structure is intended to make the program readable, making the use of excessive comments unnecessary. This is reinforced if both function names and variable names, described in the next chapter/are carefully selected to convey their meaning to anyone reading the program. However, if the purpose of a function or any of its statements is still not clear from its structure, name, or context, include comments where clarification is needed.
Exercises 1.4
la. Will the following program work?
#include <stdio.h> main ( ) {printf ("Hello there world!") ; }
b. Why is the program given in Exercise la not a good program? 2. Rewrite the following programs to conform to good programming practice. a. #include <stdio. h>
main (
){
printf
(
1.5
23
b. #include <stdio.h> main ( ) {printf ("Newark is a city\n") "In New Jersey\n" )1; printf ("It is also a city\n" ); printf ("In Delaware\n"
) ;}
;printf
c. #include <stdio.h> main () {printf (Reading a program\n") ;printf ( "is much easier\n" ) ;printf (" if a standard form for main is used\n") ;printf ("and each statement is written\n") "on a line by itself\n")
;}
;printf
d. #include
<stdio.h> main ( ) {printf ("EVery C program" ) ;printf ("\nmust have one and only one"
) ;
printf
) ;
("main function"
printf( "\n the escape sequence of characters") ;print f( "\nfor a newline can be placed anywhere" ) ;printf ("\n within the message passed to printf()"
) ;}
3 a. When used in a message, the backslash character alters the meaning of the character immediately following it. If we wanted to print the backslash character, we would have to tell printf ( ) to escape from the way it normally interprets the backslash. What character do you think is used to alter the way a single backslash character is interpreted? b. Using your answer to Exercise 3a, write the escape sequence for printing a backslash. 4 a. A token of a computer language is any sequence of characters that, as a unit, with no intervening characters or white space, has a unique meaning. Using this definition of a token, determine whether escape sequences, function names, and the reserved words listed in Table 1-1 are tokens of the C language. b. Discuss whether adding white space to a message alters the message. Discuss whether messages can be considered tokens of C. c. Using the definition of a token given in Exercise.4a,determine whether the statement "Except for tokens of the language, C ignores all white space" is true.
1.5
Part of learning any programming language is making the elementary mistakes commonly encountered as you begin to use the language. These mistakes tend to be quite frustrating, since each language has its own set of common programming errors waiting for the unwary. The more common errors made when initially programming in Care:
24
Chapter One
Getting Started
1. Omitting the parentheses after main. 2. Omitting or incorrectly typing the opening brace { that signifies the start of a function body. 3. Omitting or incorrectly typing the closing brace } that signifies the end of a function. 4. Misspelling the name of a function; for example, typing pintf ( ) instead of printf ( ). 5. Forgetting to close the message to printf ( ) with a double quote symbol. 6. Omitting the semicolon at the end of each C statement. 7. Adding a semicolon at the end of the # inc 1ude preprocessor command. 8. Forgetting the \n to indicate a new line. 9. Incorrectly typing the letter 0 for the number zero (0), or vice versa. 10. Incorrectly typing the letter I for the number 1, or vice versa. The third, fifth, sixth, seventh and eighth errors in this list are initially the most common. It is worthwhile for you to write a program and specifically introduce each of these errors, one at a time, to see what error messages are produced by your compiler. Then, when these error messages appear due to inadvertent errors, you will have had experience in understanding the messages and correcting the errors. On a more fundamental level, a major programming error made by all beginning programmers is the rush to code and run a program before the programmer fully understands what is required and the algorithms and procedures that will be used to produce the desired result. A symptom of this haste to get a program entered into the computer is the lack of either an outline of the proposed program or a written program itself. Many problems can be caught just by checking a copy of the program, either handwritten or listed from the computer, before it is ever compiled.
All
program
statements
in here;
1.7
25
6. All C statements must be terminated by a semicolon. 7. The print f ( ) function is used to display text or numerical results. The first argument to pr in t f ( ) can be a message, which is enclosed in double quotes. The text in the message is displayed directly on the screen and may include newline escape sequences for format control.
1.7
All computers, from large supercomputers costing millions of dollars to smaller desktop personal computers, must perform a minimum set of functions and provide the capability to: 1. 2. 3. 4. 5. Accept input. Display output. Store information in a logically consistent format (traditionally binary). Perform arithmetic and logic operations on either the input or stored data. Monitor, control, and direct the overall operation and sequencing of the system.
Figure 1-18 illustrates the computer hardware components that support these capabilities. Specifically, this hardware consists of arithmetic and logic, control, memory, and input/output units. In the first commercially available computers of the 1940s and 1950s, all hardware units were built using relays and vacuum tubes. The resulting computers were extremely large pieces of equipment, capable of making thousands of calculations per second, and costing millions of dollars. With the commercial introduction of transistors in the 1960s both the size and cost of computer hardware was reduced. The transistor was approximately onetwentieth the size of its vacuum tube counterpart, which allowecl manufacturers to combine the arithmetic and logic unit with the control unit into a single new unit. This combined unit is called the central processing unit (CPU). The combination of the ALU and control units into one CPU made sense because a majority of control signals generated by a program are directed to the ALU in response to arithmetic and logic instructions within the program. Combining the ALU with the control unit simplified the interface between these two units and provided improved processing speed. The mid-1960s saw the introduction of integrated circuits (ICs) that resulted in still another significant reduction in the space required to produce a CPU. Initially, integrated circuits were manufactured with up to 100 transistors on a single 1 cm2 chip of silicon. Such devices are referred to as small-scale integrated (SSI) circuits. Current versions of these chips contain hundreds of thousands to over a million transistors and are referred to as very large-scale integrated (VLSI) chips. VLSI chip technology has provided the means of transforming the giant computers of the 1950s into today's desktop personal computers. Each individual unit required to form a computer (CPU, memory, and I/O) is now manufactured on an individual VLSI chip, and the single-chip CPU is referred to as a micropro-
26
Chapter One
Getting Started
CPU
ALU
I
Input Control Output
I I
Memory
Memory Unit: This unit stores information in a logically consistent format. Typically, both instructions and data are stored in memory, usually in separate and distinct areas. Control Unit: The control unit directs and monitors the overall operation of the computer. It keeps track of where in memory the next instruction resides, issues the signals needed to both read data from and write data to other units in the system, and executes all instructions. Arithmetic and Logic Unit (ALU): The ALU performs all the arithmetic and logic functions, such as addition, subtraction, comparison, etc., provided by the system. Input/Output (I/O) Unit: This unit provides access to and from the computer. It is the interface to which peripheral devices such as keyboards, cathode ray screens, printers, and card readers are attached.
cessor. Figure 1-19 illustrates how these chips are connected internally within current personal computers, such as the IBM-pes. Concurrent with the remarkable reduction in computer hardware size has been an equally dramatic decrease in cost and increase in processing speeds. The equivalent computer hardware that cost over a million dollars in 1950 can now be purchased for less than five hundred dollars. If the same reductions occurred in the automobile industry, for example, a Rolls-Royce could now be purchased for ten dollars! The processing speeds of current computers have also increased by a factor of a thousand over their 1950s-predecessors, with the computational speeds of current machines being measured in both millions of instructions per second (MIPS) and billions of instructions per second (BIPS).
1.7
27
Microprocessor (CPU)
Memory
Input
Output
FIGURE 1-19
Computer Storage It would be very convenient if a computer stored numbers and letters inside its memory and arithmetic and logic units the way that people do. The number 126, for example, would then be stored as 126 and the letter A stored as the letter A. Unfortunately, due to the physical components used in building a computer, this is not the case. The smallest and most basic data item in a computer is called a bit. Physically, a bit is really a switch that can be either open or closed. By convention, the open and closed positions of each switch are represented as a 0 and a 1, respectively. A single bit that can represent the values 0 and 1, by itself, has limited usefulness. All computers, therefore, group a set number of bits together, both for storage and transmission. The grouping of eight bits to form a larger unit is an almost universal computer standard. Such groups are commonly referred to as bytes. A single byte consisting of eight bits, where each bit is either 0 or 1, can represent anyone of 256 distinct patterns. These consist of the pattern 00000000 (all eight switches open) to the pattern 11111111(all eight switches closed), and all possible combinations of Osand Is in between. Each of these patterns can be used to represent either a letter of the alphabet, other single characters, such as a dollar sign, comma, etc., a single digit, or numbers containing more than one digit. The patterns of Osand Is used to represent letters, single digits, and other single characters are called character codes (two such codes, called the ASCII and EBCDIC codes, are presented in Section 2.1). The patterns used to store numbers are called number codes, one of which is presented below. Two's Complement Numbers The most common number code for storing integer values inside a computer is called the two's complement representation. Using this code, the integer equivalent of any bit pattern, such as 10001101,is easy to determine and can be found for either positive or negative integers with no change in the conversion method. For convenience we will assume byte-sized bit patterns consisting of a set of eight bits each, although the procedure carries directly over to larger size bit patterns. The easiest way to determine the integer represented by each bit pattern is first to construct a simple device called a value box. Figure 1-20 illustrates such a box for a single byte.
28
Chapter One
Getting Started
-1281
64
16
__ 4
2_~
Mathematically, each value in the box illustrated in Figure 1-20 represents an increasing power of two. Since two's complement numbers must be capable of representing both positive and negative integers, the leftmost position, in addition to having the largest absolute magnitude, also has a negative sign. Conversion of any binary number, for example 10001101, simply requires inserting the bit pattern in the value box and adding the values having ones under them. Thus, as illustrated in Figure 1-21, the bit pattern 10001101represents the integer number -115. The value box can also be used in reverse, to convert a base 10 integer number into its equivalent binary bit pattern. Some conversions, in fact, can be made by inspection. For example, the base 10 number -125 is obtained by adding 3 to -128. Thus, the binary representation of -125 is 10000011,which equals -128 + 2 + 1. Similarly, the two's complement representation of the number 40 is 00101000,which is 32 plus 8. Although the value box conversion method is deceptively simple, the method is directly related to the underlying mathematical basis of two's complement binary numbers. The original name of the two's complement code was the weighted-sign code, which correlates directly to the value box. As the name weighted sign implies, each bit position has a weight, or value, of two raised to a power and a sign. The signs of all bits except the leftmost bit are positive and the sign of the leftmost bit is negative. In reviewing the value box, it is evident that any two's complement binary number with a leading 1 represents a negative number, and any bit pattern with a leading 0 represents a positive number. Using the value box it is easy to determine the most positive and negative values capable of being stored. The most negative value that can be stored in a single byte is the decimal number -128, which has the bit pattern 10000000.Any other nonzero bit will simply add a positive amount to the number. Additionally, it is clear that a positive number must have a 0 as its leftmost bit. From this you can see that the largest positive 8-bit two's complement number is 01111111or 127. Words and Addresses One or more bytes may themselves be grouped into larger units called words, which facilitate faster and more extensive data access. For example, retrieving a word consisting of four bytes from a computer's memory results in more infor-
64
32
16
~
-128 +
o
+
o
+
8 1
4
1
o
o +
1
=
4 +
-115
1.7
29
mation than that obtained by retrieving a word consisting of a single byte. Such a retrieval is also considerably faster than four individual byte retrievals. This increase in speed and capacity, however, is achieved by an increase in the computer's cost and complexity. Early personal computers, such as the Apple lIe and Commodore machines, internally stored and transmitted words consisting of single bytes. AT&T 6300 and IBM-PC/XTs use word sizes consisting of two bytes, while Digital Equipment, Data General, and Prime minicomputers store and process words consisting of four bytes each. Supercomputers, such as the CRAY-l and Control Data 7000, have 6- and 8-byte words, respectively. The arrangement of words in a computer's memory can be compared to the arrangement of suites in a very large hotel, where each suite is made up of rooms of the same size. Just as each suite has a unique room number to locate and identify it, each word has a unique numeric address. In computers that allow each byte to be individually accessed, each byte has its own address. Like room numbers, word and byte addresses are always positive, whole numbers that are used for location and identification purposes. Also, like hotel rooms with connecting doors for forming larger suites, words can be combined to form larger units for the accommodation of different size data types.
Chapter Two
2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 Data Constants and Arithmetic Operations Variables and Declaration Statements Assignment Statements Formatted Output Top-Down Program Development Applications Common Programming Errors Chapter Summary Enrichment Study: Variable Addresses and Memory Storage
30
2.1
31
As these examples illustrate, integers may be signed (have a leading + or - sign) or unsigned (no leading + or - sign). No commas, decimal points, or special symbols, such as the dollar sign, are allowed. Examples of invalid integer constants are: $255.62 2,523
3.
6,243,892
1,492.89
+6.0
The largest (most positive) and smallest (most negative) integer values that can be used in a program depend on the amount of storage each computer sets aside for an integer. The more commonly used storage allocations are listed in Table 2-1. By referring to your computer's reference manual or using the s i z eo f operator introduced in Section 2.9, you can determine the actual number of bytes allocated by your computer for each integer value. For IBM 370, DEC-VAX, and PRIME computers the most positive integer allowed is 2147483647 and the most negative integer is -2147483648.1 Floating Point and Double Precision Constants Both floating point and double precision constants are any signed or unsigned numbers having a decimal point. Examples of floating point and double precision numbers are: +10.625
5.
-6.2
3251.92
0.0
0.33
-6.67
+2.
Notice that the numbers 5., 0.0, and +2. are classified as floating point or double precision constants in C, while the same numbers written without a decimal point (5, 0, +2) are classified as integer constants. The difference between floating point and double precision numbers is the amount of storage that a computer uses for each type. Most computers use twice the amount of storage for double precision numbers than for floating point numbers, which allows a double precision number to have approximately twice the
1 It is interesting to note that in all cases the magnitude of the most negative integer allowed is always one more than the magnitude of the most positive integer. This is due to the method most commonly used to represent integers, called two's complement representation. For an explanation of two's complement representation see the Enrichment Section at the end of Chapter 1.
32
Chapter Two
precision of a floating point number (for this reason floating point numbers are sometimes referred to as single precision numbers). The actual ~torage allocation for each data type, however, depends on the particular computer. The sizeof operator introduced in Section 2.9 allows you to determine the amount of storage reserved by your computer for each of these data types. , Although floating point and double precision constants can be signed or unsigned, no special symbols, such as the dollar sign and the comma, are permitted. Examples of invalid floating point and double precision constants are: 5,326.25 24 123 6,459 $10.29
Exponential Notation
Floating point and double precision numbers can be written in exponential notation, which is similar to scientific notation and is useful in expressing both very large or very small numbers in compact form. The following examples illustrate how numbers with decimals can be expressed in exponential notation.
In exponential notation the letter e stands for exponent. The number following the e represents a power of 10 and indicates the number of places the decimal point should be moved to obtain the standard decimal value. The decimal point is moved to the right if the number after the e is positive, or moved to the left if the number after the e is negative. For example, the e3 in the number 1.625e3 means move the decimal point three places to the right, so that the number becomes 1625. The e-3 in the number 7.31e-3 means move the decimal point three places to the left, so that 7.31e-3 becomes .00731.
2.1
33
Character Constants The fourth basic data type recognized by C is the character constant. Characters are the letters of the alphabet, the ten digits 0 through 9, and special symbols such as + $ . , - !. A single character constant is anyone letter, digit, or special symbol enclosed by single quotes. Examples of valid character constants are: 'A' '$' 'h'
'7'
'y'
'!'
'M'
'q'
Character constants are typically stored in a computer using either the ASCII or EBCDIC codes. ASCII, pronounced AS-KEY, is an acronym for American Standard Code for Information Interchange. EBCDIC, pronounced EBB-SAHDICK, is an acronym for Extended Binary Coded Decimal Interchange Code. These codes assign individual characters to a specific pattern of Os and Is. Table 2-2 lists the correspondence between bit patterns and the uppercase letters of the alphabet used by the ASCII and EBCDIC codes. Using Table 2-2, we can determine how the character constants 'J', 'a', 'N', 'E', and'S', for example, are stored inside a computer that uses the ASCII character code. Using the ASCII code, this sequence of characters requires five bytes of storage (one byte for each letter) and would be stored as illustrated in Figure 2-1.
Letter
A B
C
ASCII Code
01000001 01000010 01000011 01000100 01000101 01000110 01000111 01001000 01001001 01001010 01001011 01001100 01001101
EBCDIC Code
11000001 11000010 11000011 11000100 11000101 11000110 11000111 11001000 11001001 11010001 11010010 11010011 11010100
Letter
N 0
P Q
ASCII Code
01001110 01001111 01010000 01010001 01010010 01010011 01010100 01010101 01010110 01010111 01011000 01011001 01011010
EBCDIC Code
11010101 11010110 11010111 11011000 11011001 11100010 11100011 11100100 11100101 11100110 11100111 11101000 11101001
D E F
G
S
T
U
H
I
V
W
J
K
X Y
Z
L M
34
Chapter Two
5 Bytes of Storage
01001010:01001111:01001110:01000101:01010011
I I I I
J FIGURE 2-1
Escape Sequences When a backslash (\) is used directly in front of a select group of characters, the backslash tells the computer to escape from the way these characters would normally be interpreted. For this reason, the combination of a backslash and these specific characters is called an escape sequence. We have already encountered an example of this in the newline escape sequence, \n. Table 2-3 lists other common escape sequences.
TABLE 2-3 Escape Sequences Meaning
move back one space move to next page move to next line carriage return move to next tab setting backslash character single quot~
Escape sequence
\b \f \n \r \t \\ \' \" \nnn
"
double quote
TABLE 2-4
C Escape sequence
\b
\f
Computer code
\n \r \\ \' \"
2.1
35
Although each escape sequence listed in Table 2-3 is made up of two distinct characters, the combination of the two characters with no intervening white space causes the computer to store one character code. Table 2-4 lists the ASCII code byte patterns for the escape sequences listed in Table 2-3. Arithmetic Operations Integers, floating point numbers, and double precision numbers may be added, subtracted, multiplied, and divided. The symbols for performing these arithmetic operations are called arithmetic operators:
Operator
/
%
A simple arithmetic expression consists of an arithmetic operator connecting two arithmetic operands in the form: operand operator operand Examples of arithmetic expressions are: 3+7 18 - 3 12.62 + 9.8 .08 * 12.2 12.6/2. The spaces around the arithmetic operators in these examples are inserted strictly for clarity and may be omitted without affecting the value of the expression. The value of any arithmetic expression can be displayed using the printf ( ) function. Doing this requires passing two items to pr in t f ( ), a control string that tells the function where and in what form the result is to be displayed, and the expression that we wish to evaluate. For example, the value of the expression 6 * 14 can be displayed using the statement
printf("The value of 6 times 14 is %d", 6
* 14);
This statement passes two arguments to the priIltf () function. The first argument is the message The value of 6 times 14 is %d.The second argument is the value of the expression 6 * 14. The first argument passed to printf ( ) must always be a message. A message that also includes a conversion control sequence, such as %d,is termed a control
36
Chapter Two
( ) function. They tell the function what type of value is to be displayed and where . to display it. Conversion control sequences are also referred to as conversion specifications and format specifiers. A conversion control sequence always begins with a %symbol and ends with a conversion character (c, d, f, etc.). As we will see, additional formatting characters can be placed between the %symbol and the conversion character. The percent sign %in a conversion control sequence tells pr in t f ( ) to print a number at the place in the message where the %is located. The d, placed immediately after the %,tells printf ( ) that the number should be printed as an integer. When pr in t f ( ) sees the conversion control sequence in its control string, it substitutes the value of the next argument in place of the conversion control sequence. Since this next argument is the expression 6 * 14, which has a value of 84, it is this value that is displayed. Also, as indicated in the example, all arguments passed to the printf ( ) function must be separated by commas. Thus, the statement printf("The causes the printout The value of 6. times 14 is 84 value of 6 times
14 is
%d", 6 * 14);
Just as the %d conversion control sequence alerts printf ( ) that an integer value is to be displayed, the conversion control sequence % f (the f stands for floating point) indicates that a number with a decimal point is to be displayed. For example, the statement printf("The value of 0.06 times 14.8 is %f.", 0.06
* 14.8);
As this display shows, the %f conversion control sequence causes printf ( ) to display six digits to the right of the decimal place. If the number does not have six decimal digits, zeros are added to the number to fill the fractional part. If the number has more than six decimal digits, the fractional part is rounded to six decimal digits. One caution should be mentioned here: The printf ( ) function does not check the values it is given. If an integer conversion control sequence is used (%d, for example) and the value given the function is either a floating point or double precision number, the display will be machine dependent. Similarly, if a floating point conversion control sequence is used and the corresponding number is an integer, an unanticipated result will occur.
2 More formally, a control string is referred to as a control specifier. We will use the more descriptive term, control string, to emphasize that a string is being referenced.
2.1
37
Character data is displayed using the %c conversion control sequence. For example, the statement printf("The causes the display The first letter of the alphabet is an a. first letter of the alphabet is an %c.", 'a');
Program 2-1 illustrates using printf ( ) to display the results of an expression within the statements of a complete program.
gIl
#include main()
{
Program 2-1
<stdio.h>
plus %f equals %f\n", 15.0, 2.0, 15.0 + 2.0); minus %f equals %f\n",15.0, 2.0, 15.0 - 2.0); times %f equals %f\n",15.0, 2.0, 15.0 * 2.0); divided by %f equals %f",15.0, 2.0, 15.0 / 2.0);
The output of Program 2-1 is: 15.000000 15.000000 15.000000 15.000000 plus 2.000000 equals 17.000000 minus 2.000000 equals 13.000000 times 2.000000 equals 30.000000 divided by 2.000000 equals 7.500000
Notice that each statement in Program 2-1passesfourargumentstotheprintf ( ) function consisting of one control string and three values. Within each control string there are three % f conversion control sequences (oDefor each value that is to be displayed).
Expression Types
An expression that contains only integer operands is called an integer expression, and the result of the expression is an integer value. Similarly, an expression containing only floating point. operands (single and double precision) is called a floating point expression, and the result of such an expression is a floating point value. An expression containing .both integer and floating point operands is called a mixed-mode expression. Although it is better not to mix integer and floating point operands in an arithmetic operation, the resulting data type of an operation is determined by the following rules:
38
Chapter Two
1. If both operands are integers, the result of the operation is an integer. 2. If one operand is a floating point or double precision value, the result of the operation is a double precision number. Notice that the result of an arithmetic expression is never a floating point number because the computer temporarily converts all floating point numbers to double precision numbers when arithmetic is being done. Integer Division The division of two integers can produce rather strange results for the unwary. For example, dividing the integer 15 by the integer 2 yields an integer result. Since integers cannot contain a fractional part, the expected result, 7.5, is not obtained. In C, the fractional part of the result obtained when dividing two integers is dropped (truncated). Thus, the value of 15/2 is 7, the value of 9/4 is 2, and the value of 19/5 is 3. There are times when we would like to retain the remainder of an integer division. To do this C provides an arithmetic operator that captures the remainder when two integers are divided. This operator, called the modulus operator, has the symbol %. The modulus operator can be used only with integers. For example, 9%4isl 17 % 3 is 2 14 % 2 is 0 A Unary Operator (Negation) Besides the binary operators for addition, subtraction, multiplication, and division, C also provides unary operators. One of these unary operators uses the same symbol that is used for binary subtraction (-). The minus sign used in front of a single numerical operand negates (reverses the sign of) the number. Table 2-5 summarizes the six arithmetic operations we have described so far and lists the data type of the result produced by each operator based on the data type of the operands involved. Operator Precedence and Associativity Besides such simple expressions as 5 + 12 and .08 * 26.2, we frequently need to create more complex arithmetic expressions. C, like most other programming languages, requires that certain rules be followed when writing expressions containing more than one arithmetic operator. These rules are: 1. Two binary arithmetic operator symbols must never be placed side by side. For example, 5 * %6 is, invalid because the two operators * and % are placed next to each other. 2. Parentheses may be used to form groupings, and all expressions enclosed within parentheses are evaluated first. For example, in the expression (6 + 4) / (2 + 3), the 6 + 4 and 2 + 3 are evaluated first to yield 10 / 5. The 10 / 5 is then evaluated to yield 2. Sets of parentheses may also be enclosed by other parentheses. For example, the expression (2 * (3 + 7) ) / 5 is valid. When parentheses are used within
2.1
39
TABLE 2-5
Operation Addition
Subtraction
Binary
Multiplication
Binary
Division
Binary
Modulus Negation
Binary Unary
Both integers One integer One floating point or double precision operand
parentheses, the expressions in the innermost parentheses are always evaluated first. The evaluation continues from innermost to outermost parentheses until the expressions of all parentheses have been evaluated. The number of right-facing parentheses, (, must always equal the number of left-facing parentheses, ), so that there are no unpaired sets. 3. Parentheses cannot be used to indicate multiplication. The multiplication operator, *, must be used. For example, the expression (3 + 4) (5 + 1) is invalid. The correct expression is (3 + 4) * (5 + 1). As a general rule, parentheses should be used to specify logical groupings of operands and to indicate clearly to both the computer and programmers the intended order of arithmetic operations. In the absence of parentheses, expressions containing multiple operators are evaluated by the priority, or precedence, of each operator. Table 2-6 lists both the precedence and associativity of the operators considered in this section.
TABLE 2-6
Operator unary -
* / %
+ -
40
Chapter Two
The precedence of an operator establishes its priority relative to all other operators. Operators at the top of Table 2-6 have a higher priority than operators at the bottom of the table. In expressions with multiple operators, the operator with the higher precedence is used before an operator with a lower precedence. For example, in the expression 6 + 4 / 2 + 3, the division is done before the addition, yielding an intermediate result of 6 + 2 + 3. The additions are then performed to yield a final result of 1I. Expressions containing operators with the same precedence are evaluated according to their associativity. This means that evaluation is either from left to right or from right to left as each operator is encountered. For example, in the expression 8 + 5 * 7 % 2 * 4, the multiplication and modulus operator are of higher precedence than the addition operator and are evaluated first. Both of these operators, however, are of equal precedence. Therefore, these operators are evaluated according to their left-to-right associativity, yielding 8+ 5*7%2*4 8+35%2*4 8+1*4 8+4
= 12
Exercises 2.1
1. Determine data types appropriate for the following data: a. the average of four grades b. the number of days in a month c. the length of the Golden Gate Bridge d. the numbers in a state lottery e. the distance from Brooklyn, N.Y. to Newark, N.J. t. the names in a mailing list 2. Convert the following numbers into standard decimal form: 6.34e5 126. 656.23 1.95162e2 3426.95 8.395e1 4893.2 2.95e-3 .321 4.623e-4 .0123 .006789 3. Write the following decimal numbers using exponential notation: 4. Listed below are correct algebraic expressions and incorrect C expressions corresponding to them. Find the errors and write corrected C expressions.
Algebra a.
(2)(3)
C expression
(2) (3)
(4)(5)
(4)
(5)
b. 6 + 18
2 c.
6 + 18 / 2
2.1
41
4.6(3.0 (12.1
- 3.8)
5. Determine the value of the following integer expressions: a. 3 + 4 * 6 f. 20 - 2 / (6 + 3) b. 3 * 4 / 6 + 6 g. (20 - 2) / 6 + 3 c. 2 * 3 / 12 * 8 / 4 h. (20 - 2) / (6 + 3) d. 10 * (1 + 7 * 3) i. 50 % 20 e. 20 - 2 / 6 + 3 j. (10 + 3) % 4 6. Determine the value of the following floating point expressions: a. 3.0 + 4.0 * 6.0 b. 3.0 * 4.0 / 6.0 + 6.0 c. 2.0 * 3.0 / 12.0 * 8.0 / 4.0 d. 10.0 * (1.0 + 7.0 * 3.0) e. 20.0 - 2.0 / 6.0 + 3.0 f. 20.0 - 2.0 / (6.0 + 3.0) g. (20.0 - 2.0) / 6.0 + 3.0 h. (20.0 - 2.0) / (6.0 + 3.0) 7. Evaluate the following mixed-mode expressions and list the data type of the result. In evaluating the expressions be aware of the data types of all intermediate calculations. a. 10.0 + 15 / 2 + 4.3 b. 10.0 + 15.0 / 2 + 4.3 c. 3.0 * 4 / 6 + 6 d. 3 * 4.0 / 6 + 6 e. 20.0 - 2 / 6 + 3 f. 10 + 17 * 3 + 4 g. 10 + 17 / 3. + 4 h. 3.0 * 4 % 6 + 6 i. 10 + 17 % 3 + 4. 8. Assume that amount has the integer value I, m has the integer value 50, n has the integer value 10, and p has the integer value 5. Evaluate the following expressions: a. n/p+3 b. m / p + n - 10 * amount c. m - 3 * n + 4 * amount d. amount / 5 e. 18/ P f. -p*n g. -m /20 h. (m + n) / (p + amount) i. m + n / p + amount 9. Repeat Exercise 8 assuming that amount has the real value 1.0, m has the real value 50.0, n has the real value 10.0, and p has the real value 5.0. 10. Using the system reference manuals for your computer, determine the character code used by your computer. 11. Determine the output of the following program:
#include <stdio.h> rnain() /* a program illustrating
{
integer
truncation
*/
printf("answerl is the integer %d", 9/4); printf("\nanswer2 is the integer %d", 17/3);
42
Chapter Two
illustrating
the % operator
*/
printf(nThe printf(nThe
remainder remainder
13. Write a C program that displays the results of the expressions 3.0" 5.0, 7.1" 8.3 - 2.2, and 3.2 / (6.1 5). Calculate the value of these expressions manually to verify that the displayed values are correct. 14. Write a C program that displays the results of the expressions 15 / 4, 15 % 4, and 5 3 - (6" 4). Calculate the value of these expressions manually to verify that the display produced by your program is correct. 15a. Show how the name KINGSLEY would be stored inside a computer that uses the ASCII code. That is, draw a figure similar to Figure 2-1 for the letters KINGSLEY. b. Show how the name KINGSLEY would be stored inside a computer that uses the EBCDIC code. 16a. Repeat Exercise 15a using the letters of your own last name. b. Repeat Exercise 15b using the letters of your own last name.
numbers, C allows characters and integers to be added or subtracted. This can be done because C always converts a character to an equivalent integer value whenever a character is used in an arithmetic expression. Thus, characters and integers can be freely mixed in such expressions. For example, if your computer uses the ASCII code, the expression 'a' + 1 equals 'h', and 'z' - 1 equals 'y'. Similarly, 'A' + 1 is 'B', and 'z' - 1 is 'Y'. With this
2.2
43
as background, determine the character results of the following expressions (assume that all characters are stored using the ASCII code). a. 'm'-5 b. 'm' +5 c. 'G' +6 d. 'G'-6 e. 'h' - 'a' f. 'g' - 'a' + 1
g. 'G' - 'A'
21a. The table in Appendix F lists the integer values corresponding to each letter stored using the ASCII code. Using this table, notice that the uppercase letters consist of contiguous codes starting with an integer value of 65 for A and ending with 90 for the letter Z. Similarly, the lowercase letters begin with the integer value of 97 for the letter a and end with 122 for the letter z. With this as background, determine the character value of the expressions' A' + 32 and 'z' + 32. b. Using Appendix F, determine the integer value of the expression 'a' - 'A'. c. Using the results of Exercises 21a and 21b, determine the character value of the following expression, where uppercase letter can be any uppercase letter from A to Z: uppercase letter + 'a' - 'A'
2.2
All integers, numbers, floating point numbers, and values used in a computer program are stored and retrieved from the computer's memory unit. Conceptually, individual memory locations in the memory unit are arranged like the rooms in a large hotel. Like hotel rooms, each memory location has a unique address ("room number"). Before high-level languages such as C existed, memory locations were referenced by their addresses. For example, to store the integer values 45 and 12 in the memory locations 1652 and 2548 (see Figure 2-2), respectively, required instructions equivalent to
put a 45 in location 1652 put a 12 in location 2548
FIGURE 2-2
1652
2548
Memory Addresses
44
Chapter Two
To add the two numbers just stored and save the result in another memory location, for example at location 3000, required a statement comparable to add the contents of location 1652 to the contents of location 2548 and store the result location 3000 Clearly this method of storage and retrieval is a cumbersome process. In high-level languages like C, symbolic names are used in place of actual memory addresses. Symbolic names used in this manner are called variables. A variable is simply a name given by the programmer to a memory storage location. The term variable is used because the value stored in the variable can change, or vary. For each name that the programmer uses, the computer keeps track of the actual memory address corresponding to that name. In our hotel room analogy, this is equivalent to putting a name on the door of a room and referring to the room by this name, such as the Blue Room, rather than using the actual room number. In C the selection of variable names is left to the programmer, as long as the variable name is chosen according to the same rules used for selecting function names given in the previous chapter. Thus, a variable name can consist of no more than 31 letters, digits, or underscore, the first of which must be a letter or underscore and cannot be a keyword (see Table 1-2). As with function names, variable names should be mnemonics that give some indication of the variable's use. For example, a good name for a variable used to store a value that is the total of some other values would be sum or total. Similarly the variable name width is a good choice if the value stored in the variable represents a width. Variable names that give no indication of the value stored, such as r2d2, linda, bill, and getum should not be selected. Now assume the first memory location illustrated in Figure 2-2, that has address 1652, is given the name numl. Also assume that memory location 2548 is given the variable name num2, and memory location 3000 is given the variable name total, as illustrated in Figure 2-3. Using these variable names, the operations of storing 45 in location 1652, storing 12 in location 2548, and adding the contents of these two locations is accomplished by the C statements num1 num2 total
= =
num2;
Each of these three statements is called an assignment statement because it tells the computer to assign (store) a value into a variable. Assignment statements always have an equal (=) sign and one variable name immediately to the left of this sign. The value on the right of the equal sign is determined first and this value is assigned to the variable on the left of the equal sign. The blank spaces in the assignment statements are inserted for readability. We will have much more to say about assignment statements in the next section, but for now we can use them to store values in variables. A variable name is useful because it frees the programmer from concern over where data are physically stored inside the computer. We simply use the variable name and let the computer worry about where in memory the datum is actually stored. Before storing a value into a variable, however, we must clearly define
2.2
45
FIGURE 2-3
~ numl
+ num2
~
+ total
~
(
1652
[
2548
[
3000
t
VariableAddresses
the type of data that is to be stored in it. This requires telling the compiler, in advance, the names of the variables that will be used for integers, the names that will be used for floating point numbers, and the names that will be used to store the other C data types. Declaration Statements Naming a variable and specifying the data type that can be stored in it are accomplished using declaration statements. A declaration statement has the general form:
variable
name;
Variables used to hold floating point values are declared using the keyword float, while variables that will be used to hold double precision values are declared using the keyword double. For example, the statement float
3
firstnum;
In addition to the keyword int used to specify an integer, the keyword long specifies a long integer, whichtypicallydoubles the size of the allowableinteger value that can be assigned to the declared variable. Also,the keywords unsigned int are used to specify an integer that can only store nonnegative numbers. Long integers and unsigned integers are displayed using the %ldand %uconversion control sequences, respectively, in the printf ( ) function.
46
Chapter Two
declares firstnum as a variable that can be used to store a floating point number. Similarly, the statement
double secnum;
declares that the variable secum will be used to store a double precision number. Declaration statements within a function appear immediately after the opening brace of a function, and like all C statements must end with a semicolon. A main ( ) function containing declaration statements has the general form:
main
{
() statements;
declaration
other statements;
.Program 2-2 illustrates the declaration and use of four floating point variables. The printf () function is then used to display the contents of one of these variables.
,101,
Program 2-2
#include main()
{
<stdio.h>
/* /* /* /*
grade1 as a float variable */ grade2 as a float variable */ total as a float variable */ average as a float variable */
grade1 = 85.5; grade2 = 97.0; total = grade1 + grade2; average = total/2.0; /* divide the total by 2.0 */ printf("The average grade is %f\n",average);
The placement of the declaration statements in Program 2-2 is straightforward, although we will shortly see that individual declaration statements for the same data type are typically combined into a single declaration statement. When Program 2-2 is run, the following output is displayed:
The average grade is 91.250000
2.2
47
Two comments with respect to the printf () function call made in Program 2-2 should be mentioned here. If a variable name is one of the arguments passed to a function, as it is to printf () in Program 2-2, the function only receives a copy of the value stored in the variable. It does not receive the variable's name. When the program sees a variable name in the function parentheses, it first goes to the variable and retrieves the value stored. It is this value that is passed to the function. Thus, when a variable is included in the printf () argument list, print f () receives the value stored in the variable and then displays this value. Internally, printf () has no knowledge of where the value it receives came from or the variable name under which the value was stored. . Although this procedure for passing data into a function may seem surprising, it is really a safety procedure for ensuring that a called function does not have access to the original variable. This guarantees that the called function cannot inadvertently change data in a variable declared outside itself. We will have more to say about this in Chapter 7 when we examine and begin writing our own functions. The second comment concerns the use of the % f conversion control sequence in Program 2-2. Although this conversion control sequence works for both floating point and double precision numbers, the conversion control sequence % 1f may also be used for displaying the values of double precision variables. The letter 1 indicates that the number is a long floating point number, which is what a double precision number really is. Omitting the 1 conversion character has no effect on the printf () function when double precision values are displayed. As we shall see, however, it is essential in entering double precision values when the input function scanf (), introduced in the next chapter, is used. Just as integers, floating point, and double precision variables must be declared before they can be used, a variable used to store a character must also be declared. Character variables are declared using the reserved word char. For example, the declaration char declares ch to be a character variable. Multiple Declarations Variables having the same data type can always be grouped together and declared using a single declaration statement. The common form of such a declaration is: Chi
48
Chapter Two
can be replaced by the single declaration statement float gradel, grade2, total, average;
..
can be replaced with the single declaration statement int num1, num2;
Notice that declaring multiple variables in a single declaration requires that the data type of the variables be given only once, that all the variables be separated by commas, and that only one semicolon be used to terminate the declaration. The space after each comma is inserted for readability and is not required. Declaration statements can also be used to store an initial value into each declared variable. For example, the declaration statement int num1
=
15;
both declares the variable num1 as an integer variable and sets the value of 15 into the variable. The first time a value is stored in a variable the variable is said to be initialized. Thus, in this example it is correct to say that the variable num1 has been initialized to 15. Similarly, the declaration statement float grade1
=
87.0,
grade2
93.5,
total;
declares three floating point variables and initializes two of them. Constants, expressions using only constants (such as 87.0 + 12.2), and expressions using constants and previously initialized variables can all be used as initializers within a declaration statement. For example, Program 2-2 with initialization of the variables within their declaration statements would appear as: #include main()
{
<stdio.h>
float
grade1
85.5,
grade2
97.0,
total,
average;
total = grade1 + grade2; average = tota1/2.0; /* divide the total printf("The average grade is %f\n",average);
by
2.0
*/
Notice the blank space after the declaration statement. Placing a blank line after variable declarations is a common programming practice that improves both a program's appearance and its readability. We will adopt this practice for all of our programs.
2.2
49
Specifying Storage Allocation Declaration statements perform both software and hardware tasks. From a software perspective, declaration statements always provide a convenient, up-front list of all variables and their data types. In this software role, variable declarations also eliminate an otherwise common and troublesome error caused by the misspelling of a variable's name within a program. For example, assume that a variable named distnce is declared and initialized using the statement
int distance
26;
In languages that do not require variable declarations, the program would treat distnce as a new variable and either assign an initial value of zero to the variable or use whatever value happened to be in the variable's storage ar.ea. In either case a value would be calculated and assigned to mpg, and finding the error or even knowing that an error occurred could be extremely troublesome. Such errors are impossible in C, because the compiler will flag distnce as an undeclared variable. The compiler cannot, of course, detect when one declared variable is typed in place of another declared variable. In addition to their software role, declaration statements can also perform a distinct hardware task. Since each data type has its own storage requirements, the computer can only allocate sufficient storage for a variable after it knows the variable's data type. Because variable declarations provide this information, they can be used to force the computer to reserve sufficient physical memory storage for each variable. Declaration statements used for this hardware purpose are also called definition statements, because they define or tell the computer how much memory is needed for data storage. All the declaration statements we have encountered so far have also been definition statements. Later, we will see cases of declaration statements that do not cause any new storage to be allocated and are used simply to declare or alert the program to the data types of previously created and existing variables. Figure 2-4 illustrates the series of operations set in motion by declaration statements that also perform a definition role. The figure shows that definition statements (or, if you prefer, declaration statements that also cause memory to be allocated) "tag" the first byte of each set of reserved bytes with a name. This name is, of course, the variable's name and is used by the computer to correctly locate the starting point of each variable's reserved memory area. Within a program, after a variable has been declared, it is typically used by a programmer to refer to the contents of the variable (that is, the variable's value). Where in memory this value is stored is generally of little concern to the programmer. The computer, however, must be concerned with where each value is stored and with correctly locating each variable. In this task the computer uses the variable name to locate the first byte of storage previously allocated to the variable. Knowing the variable's data type then allows the computer to store or retrieve the correct number of bytes.
50
Chapter Two
int total;
"Tag" the first byte of reserved storage with the name total
G
Tells the computer to
an integer number
FIGURE 2-4a
float
1 1
firstnum;
/'
Tells the computer to
Reserve enough room for a floating point number One or more storage locations
"Tag" the first byte of reserved storage with the name firstnum
FIGURE 2-4b
firstnurn
double
secnum;
Two or more storage locations "Tag" the first byte of reserved storage with the name secnum
FIGURE 2-4c
secnurn
char
ch;
Gl------------'~
Tells the computer to "Tag" the first byte of reserved storage with the name ch
FIGURE 2-4d
2.2
51
Exercises 2.2
1. State whether the following variable names are valid or not. If they are irtvalid, state the reason why. prod3 cl234 abcd _c3 12345 newbal while $total new bal alb2c3d4 9ab6 sum.of average gradel fin~ad 2. State whether the following variable names are valid or not. If they are invalid, state the reason why. Also indicate which of the valid variable names should not be used because they convey no information about the variable. salestax a243 r2d2 first_num cc_al harry sue c3pO average sum maximum okay a awesome goforit 3sum for tot.al c$five netpay 3a. Write a declaration statement an integer. b. Write a declaration statement a floating point number. c. Write a declaration statement a double precision number. d. Write a declaration statement store a character. to declare that the variable count will be used to store to declare that the variable grade will be used to store to declare that the variable yield will be used to store to declare that the variable initial will be used to
4. Write declaration statements for the following variables. a. num1,nurn2,and num3used to store integer numbers b. gradel, grade2, grade3, and grade4 used to store floating point numbers c. tempa, tempb, and tempc used to store double precision numbers d. ch, leU, let2, let3, and let4 used to store character types 5. Write declaration statements for the following variables. a. firstnum and secnum used to store integers b. price, yield, and coupon used to store floating point numbers c. maturity used to store a double precision number 6. Rewrite each of these declaration statements as three individual declarations. a. int month, day = 30, year;
b.
double hours, rate, otime = 15.62; c. float price, amount, taxes; d. char in_key, ch, choice = 'f';
7a. Determine what each statement causes to happen in the following program. #include main()
{
<stdio.h>
num1 = 25; nurn2 = 30; total = num1 + nurn2; printf ("The total of %d and %d is
b. What is the output that will be printed when the program listed in Exercise 7a is run?
52
Chapter Two
8. Write a C program that stores the sum of the integer numbers 12 and 33 in a variable named sum.Have your program display the value stored in sum. 9. Write a C program that stores the value 16 in the integer variable length and the value 18 in the integer variable width. Have your program calculate the value assigned to the variable perimeter, using the assignment statement
perimeter
2* (length
+ width);
and print out the value stored in the variable perimeter. Make sure to declare all the variables as integers at the beginning of the main () function.
10. Write a C program that stores the integer value 16 in the variable numI and the integer value 18 in the variable num2.(Make sure to declare the variables as integers.) Have your program calculate the total of these numbers and their average. The total should be stored in an integer variable named total and the average in an integer variable named average. (Use the statement average = total/2. 0; to calculate the average.) Use the printf () function to display the total and average.
11. Repeat Exercise 10, but store the number 15 in numI instead of 16. With a pencil, write down the average of numI and num2.What do you think your program will store in the integer variable that you used for the average of these two numbers? How can you ensure that the correct answer will be printed for the average? 12. Write a program that stores the number 105.62 in the variable firstnum, 89.352 in the variable secnum, and 98.67 in the variable thirdnum. (Make sure to declare the variables first as either float or double.) Have your program calculate the total of the three numbers and their average. The total should be stored in the variable total and the average in the variable average. (Use the statement average = total /3.0; to calculate the average.) Use the printf ( ) function to display the total and average. 13. Every variable has at least two items associated with it. What are these two items? Note for Exercises 14 through 16: Assume that a character requires one byte of stor-
age, an integer two bytes, a floating point number four bytes, a double precision number eight bytes, and that variables are assigned storage in the order they are declared.
14a. Using Figure 2-5 and assuming that the variable name rate is assigned to the byte having memory address 159, determine the addresses corresponding to each variable declared in the following statements. Also fill in the appropriate bytes with the initialization data included in the declaration statements (use letters for the characters, not the computer codes that would actually be stored).
float rate; char chI = 'w', double taxes; int num, count
ch2
'0',
ch3
'w',
ch4
I! I;
0;
b. Repeat Exercise 14a, but substitute the actual byte patterns that a computer using the ASCII code would use to store the characters in the variables chI, ch2, ch3, and eM. (Hint: Use Table 2-2.) 15a. Using Figure 2-5 and assuming that the variable named cnI is assigned to the byte at memory address 159, determine the addresses corresponding to each variable declared in the following statements. Also fill in the appropriate bytes with the initialization data included in the declaration statements (use letters for the characters and not the computer codes that would actually be stored).
2.3
Assignment Statements
53
Address:
159
160
161
162
163
164
165
166
111111_1
Address: 167 168
I I
169
170 171 172 173
174
11111111_1
Address: 175 176 177 178
179
180
181
182
I
Address: 183
111111_1_1
184 185 186 187 188 189 190
III
FIGURE 2-5 char char char cnl cn6 incl
III
cn2 cn7 cn3
'h', key
I
I I_I
I
I
=
=
u',
enS
In
1 ;
= 'c',
'f';
'\\',
sch
= '\' "
inc
'0';
b. Repeat Exercise 15a, but substitute the actual byte patterns that a computer using the ASCII code would use to store the characters in each of the declared variables. (Hint: Use Table 2-2.) 16. Using Figure 2-5 and assuming that the variable name miles is assigned to the byte at memory address 159, determine the addresses corresponding to each variable declared in the following statements: float miles; int count, nurn; double dist, temp;
variable = operand;
54
Chapter Two
The simplest operand in C is a single constant. In each of the following assignment statements, the expression to the right of the equal sign is a constant:
length = 25; width = 17.5;
In each of these assignment statements the value of the constant to the right of the equal sign is assigned to the variable on the left side of the equal sign. It is extremely important to note that the equal sign in C does not have the same meaning as an equal sign in algebra. The equal sign in an assignment statement tells the computer first to determine the value of the operand to the right of the equal sign and then to store (or assign) that value in the variable to the left of the equal sign. In this regard, the C statement 1ength = 25; is read "length is assigned the value 25." The blank spaces in the assignment statement are inserted for readability only. Recall from the previous section that an initial value can be assigned to a variable when it is declared. If an initialization is not done from within the declaration statement, the variable is initialized the first time a value is assigned using an assignment statement. Subsequent assignment statements can, of course, be used to change the value assigned to a variable. For example, assume the following statements are executed one after another and that no value has been assigned to slope previously:
slope slope 3.7; 6.28 ;
The first assignment statement assigns the value of 3 . 7 to the variable named slope. Since this is the first time a value is assigned to this variable it is also correct to say that "slope is initialized to 3.7." The next assignment statement causes the computer to assign a value of 6.28 to slope. The 3.7 that was in slope is simply erased and replaced with the new value of 6.2, because a variable can store only one value at a time. It is sometimes useful to think of the variable to the left of the equal sign as a temporary parking spot in a huge parking lot. Just as an individual parking spot can be used only by one car at a time, each variable can store only one value at a time. The "parking" of a new value in a variable automatically causes the computer to remove any value previously parked there. In addition to being a constant, the operand to the right of the equal sign in an assignment statement can be a variable or any valid C expression. An expression is any combination of constants and variables that can be evaluated to yield a result. Thus, the expression in an assignment statement can be used to perform calculations using the arithmetic operators introduced in Section 2.1. Examples of assignment statements using expressions containing these operators are:
sum diff
3 + 7; = 15 - 6;
2.3
Assignment Statements
55
taxes = 0.06 * amount totwet = weight * factor; average = sum /items; slope = (y2 - y1) / (x2 - xl); As always in an assignment statement, the computer first calculates the value of the expression to the right of the equal sign and then stores this value in the variable to the left of the equal sign. For example, in the assignment statement totwet = weight * factor the expression weight * factor is first evaluated to yield a result. This result, which is a number, is then stored in the variable totwet. In writing assignment statements, you must be aware of two important considerations. Since the expression to the right of the equal sign is evaluated first, all variables used in the expression must be initialized if the result is to make sense. For example, the assignment statement totwet = weight * factor only causes a valid number to be stored in totwet if the programmer first takes care to put valid numbers in weight and factor. Thus, the sequence of statements weight factor totwet
155.0; 1. 06;
weight
* factor;
ensures that we know the values being used to obtain the result that will be stored in the variable to the left of the equal sign. Figure 2-6 illustrates the values stored in the variables weight, factor, and totwet. The second consideration to keep in mind is that since the value of an expression is stored in the variable to the left of the equal sign, there must be only one variable listed in this position. For example, the assignment statement amount
+
1769
1462
10 - 24;
is invalid. The right-hand expression evaluates to the integer 1448, which can only be stored in.a variable. Since amount + 1769 is not the valid name of a memory location (it is not a valid variable name), the computer does not know where to store the value 1448. Program 2-3 illustrates the use of assignment statements to calculate the volume of a cylinder. As illustrated in Figure 2-7, the volume of a cylinder is determined by the formula volume = 'IT f2h, where r is the radius of the cylinder, h is the height, and 'IT is the constant 3.1416 (accurate to four decimal places).
FIGURE 2-6
weight
factor
totwet
155.0
1.06
164.30
56
Chapter Two
height =
r L
r= 2.5
----FIGURE 2-7
,101,
Program 2-3
#include main( )
{
<stdio.h>
volume of a cylinder,
*/ */
float
radius,
height,
volume;
radius 2.5; height 16.0; volume 3.1416 * radius * radius * height; printf("/nthe volume of the cylinder is %f", volume);
When Program 2-3 is compiled and executed, the output is: the volume of the cylinder is 314.160000
Notice the order in which statements are executed in Program 2-3. The program begins with the keyword main and continues sequentially, statement by statement, until the closing brace. All computer programs execute in this manner. The computer works on one statement at a time, executing that statement with no knowledge of what the next statement will be. This explains why all variables used in an expression must have values assigned to them before the expression is evaluated. When the computer executes the statement volume
3.1416
* radius
* radius
* height;
in Program 2-3, it uses whatever value is stored in the variables radius and height at the time the assignment statement is executed.4 If no values have been
4
Since C does not have an exponentiation operator. the square of the radius is obtained by the term radius. radius. In Section 3.1 we introduce C's power function pow ( ), which allows us to raise a number to a power.
2.3
Assignment Statements
57
specifically assigned to these variables before they are used in the assignment statement, the computer uses whatever values happen to occupy these variables when they are referenced (on some systems all variables are automatically initialized to zero). The computer does not "look ahead" to see that you might assign values to these variables later in the program. It is important to realize that in C, the equal sign, =, used in assignment statements is itself an operator, which differs from the way most other high-level languages process this symbol. In C, the = symbol is called the assignment operator. Since the equal sign is an operator in C, multiple assignments are possible in the same statement. For example, in the statement a = b = c = 25; all the assignment operators have the same precedence. Since the assignment operator has a right-to-Ieft associativity, the final evaluation proceeds in the sequence c
b a 25;
c;
b;
This has the effect of assigning the number 25 to each of the variables individually, which can be represented as
a
= =
b
(b
(c
25));
Assignment Variations Although only one variable is allowed immediately to the left of an equal sign in an assignment expression, the variable on the left of the equal sign can also be used on the right of the equal sign. For example, the assignment expression sum = sum + lOis valid. Clearly, as an algebra equation sum could never be equal to itself plus 10. But in C, the expression sum = sum + 10 is not an equation-it is an expression that is evaluated in two major steps. The first step is to calculate the value of sum + 10. The second step is to store the computed value in sum. See if you can determine the output of Program 2-4. The assignment statement sum = 25; tells the computer to store the number 25 in sum, as shown in Figure 2-8. The first call to printf () causes the value stored in sum to be displayed by
FIGURE 2-8
sum
25
58
Chapter Two
}Ol,
Program 2-4
#include main(
{
<stdio.h>
int
sum;
sum = 25; printf("\nThe number sum = sum + 10; printf (" \nThe number
stored
in
sum is in
now stored
sum is
the message The number stored in sum is 25. The second assignment statement in Program 2-4, sum = sum + 10; causes the computer to retrieve the 25 stored in sum and add 10 to this number, yielding the number 35. The number 3 5 is then stored in the variable on the left side of the equal sign, which is the variable sum. The 25 that was in sum is simply overwritten with the new value of 35, as shown in Figure 2-9. Assignment expressions like sum = sum + 25, which use the same variable on both sides of the assignment operator, can be written using the following assignment operators:
+=
*=
/=
%=
For example, the expression sum sum + 10 can be written as sum += 10. Similarly, the expression price *= rate is equivalent to the expression price
= price
* rate.
In using these new assignment operators it is important to note that the variable to the left of the assignment operator is applied to the complete expression on the right. For example, the expression price *= rate + 1 is equivalent to the expression price = price * (rate + 1), not price = price * rate + 1. Accumulating Assignment expressions like sum += 10 or its equivalent, sum = sum + 10, are very common in programming. These expressions are required in accumulating subtotals when data is entered one number at a time. For example, if we want to add the numbers 96, 70, 85, and 60 in calculator fashion, the .following statementscould be used:
Statement
sum sum sum sum sum 0; sum sum sum sum
+ + + +
Value in
96; 70; 85; 60; 0 96 166 251 311
sum
2.3
Assignment Statements
59
-1=-,-----' )<;=~-.
sum
New Value
~35
Is Stored
FIGURE 2-9
sum
+ 10;
sum
The first statement initialized sum to O.This removes any number ("garbage" value) stored in sum that would invalidate the final total. As each number is added, the value stored in sum is increased accordingly. After completion of the last statement, sum contains the total of all the added numbers. Program 2-5 illustrates the effect of these statements by displaying sum's contents after each addition is made.
,lOI,
Program 2-5
#include main( )
{
<stdio.h>
int
sum; 0; \nThe value of sum is sum + 96; (" \n sum is now %d.", sum + 70; (" \n sum is now %d.", sum + 85; ("\n sum is now %d.", sum + 60; The final sum is (" \n initially sum); sum); sum); %d.", sum); set to %d.", sum);
sum = printf(" sum = printf sum = printf sum = printf sum = printf
311.
Although Program 2-5 is not a practical program (it is easier to add the numbers by hand), it does illustrate the subtotaling effect of repeated use of statements having the form
variable = variable
+ new_value;
60
Chapter Two
We will find many uses for this type of statement when we become more familiar with the repetition statements introduced in Chapter 5. Counting An assignment statement that is very similar to the accumulating statement is the counting statement. Counting statements have the form variable
=
variable
+ fixed_number;
+ 1;
In each of these examples the same variable is used on both sides of the equal sign. After the statement is executed the value of the respective variable is increased by a fixed amount. In the first three examples the variables i, n, and count have all been increased by one. In the next two examples the respective variables have been increased by two, and in the final example the variable kk has been increased by three. For the special case in which a variable is either increased or decreased by one, C provides two unary operators. Using the increment operator, ++, the expression variable = variable + 1 can be replaced by the expression ++variable. Examples of the increment operator are:
Expression
i
n
Alternative
++i
i + 1
n + 1
++n + 1 ++count
count
count
.}Ol,
Program 2-6
#include main( )
{
<stdio.h>
int count
count;
=
0;
continued
2.3
Assignment Statements
61
value
of count
is
%d.",
count);
now %d.",
The output displayed by Program 2-6 is: The initial count count count count value is now is now is now is now of count
l. 2. 3. 4.
is
O.
In addition to the increment operator, C also provides a decrement operator, --. As you might expect, the expression --variable is equivalent to the expression variable = variable - 1. Examples of the decrement operator are:
Expression i
n
Alternative --i
--n
i - 1
n -
count
count
- 1
--count
When ++ appears before a variable it is called a prefix increment operator. Besides appearing before (pre) a variable, the increment operator can also be applied after a variable; for example, in the expression n ++. When the increment appears after a variable it is called a postfix increment. Both of these expressions, ++nand n ++, correspond to the longer expression n = n + 1. The distinction between a prefix and postfix increment operator occurs when the variable being incremented is used in an assignment expression. For example, the expression k = ++n does two things in one expression. Initially the value of n is incremented by one and then the new value of n is assigned to the variable k. Thus, the statement k = ++n; is equivalent to the two statements
n - n + 1;
k
n;
/* /*
to k
*/ ' */
The assignment expression k = n++, which uses a postfix increment operator, reverses this procedure. A postfix increment operates after the assignment is
62
Chapter Two
completed. Thus, the statement k = n + + i first assigns the current value of n to k and then increments the value of n by one. This is equivalent to the two statements
k n
ni
n + 1i
Just as there are prefix and postfix increment operators, C also provides prefix and postfix decrement operators. For example, both of the expressions - -n and n - - reduce the value of n by one. These expressions are equivalent to the longer expression n = n - 1. As with the increment operator, however, the prefix and postfix decrement operators produce different results when used in assignment expressions. For example, the expression k = - -n first decrements the value of n by one before assigning the value of n to k. But the expression k = n -- first assigns the current value of n to k and then reduces the value of n by one. The increment and decrement operators can often be used advantageously to reduce program storage requirements significantly and increase execution speed. For example, consider the following three statements:
count = count + 1i count += 1i ++counti
All perform the same function; however, when these instructions are compiled for execution on an IBM personal computer the storage requirements for the executable instructions are 9, 4, and 3 bytes, respectively. Using the assignment operator, instead of the increment operator results in using three times the storage space for the instruction, with an accompanying decrease in execution speed.
=/
Exercises 2.3
1. Write an assignment statement to calculate the circumference of a circle having a radius of 3.3 inches. The equation for determining the circumference, c, of a circle is c 'IT r, where r is the radius and 'IT equals 3.1416. 2. Write an assignment statement to calculate the area of a circle. The equation for determining the area, a, of a circle is a = 'IT fl, where r is the radius and 'IT = 3.1416. 3. Write an assignment statement to convert temperature in degrees Fahrenheit to degrees Celsius. The equation for this conversion is Celsius = 5/9 (Fahrenheit - 32). 4. Write an assignment statement to calculate the round trip distance, d, in feet of a trip that is s miles long, one way. 5. Write an assignment statement to calculate the elapsed time in minutes, that it takes to make a trip. The equation for computing elapsed time is elapsed time = total distance / average speed. Assume that the distance is in miles and the average speed is in miles/hour. 6. Write an assignment statement to calculate the nth term in an arithmetic sequence. The formula for calculating the value, v, of the nth term is v = a + (n-l)d , where a = the first number in the sequence and d = the difference between any two numbers in the sequence. 7. Write an assignment statement to calculate the linear expansion in a steel beam as a
2.3
Assignment Statements
63
function of temperature increase. The formula for linear expansion, I, is 1= 10D +a(Tr-To)], where 10 is the length of the beam at temperature To' a is the coefficient of linear expansion, and Tr is the final temperature of the beam. 8. Coulomb's law states that the force F acting between two electrically charged spheres is given by the formula F = k ql q2/ r, where ql is the charge on the first sphere, q2 is the charge on the second sphere, r is the distance between the centers of the two spheres, and k is a proportionality constant. Write an assignment statement to calculate the force F. 9. Write an assignment statement to determine the maximum bending moment, M, of a beam. The formula for maximum bending moment is M = X W (L - X) / L, where X is the distance from the end of the beam that a weight, W, is placed, and L is the length of the beam. 10. Determine the output of the following program: #include <stdio.h> main ( ) /* a program illustrating integer truncation */
{
first integer displayed is %d", num1); second integer displayed is %d", num2);
11. Determine the output produced by the following program: #include <stdio.h> main ( )
{
float average
= 26.27;
printf("\nthe average is %f", average) ; average = 682.3; printf("\nthe average is %f" , average) ; average = 1.968; printf("\nthe average is %f" , average) ;
12. Determine the output produced by the following program: #include <stdio.h> main ( )
{
float sum; sum = 0.0; printf ("\nthe sum is %f", sum); sum = sum + 26.27; printf("\nthe sum is %f", sum); sum = sum + 1.968; printf ("\nthe final sum is %f", sum);
13a. Determine what each statement causes to happen in the following program.
64
Chapter Two
#include <stdio.h> main ( ) int num1, num2, num3, total; num1 = 25; num2 = 30; total = num1 + num2; printf("\n %d + %d = %d", num1, num2, total);
Note for Exercises 14 through 16: Identify the errors in the sections of code listed in each exercise: 14.
15.
#include <stdio.h> main ( ) int length, width, area; .area = length * width; length = 20; width = 15; printf ("The area is %d",area) ;
16.
= 20; width
15, area;
int sum;
2.3
Assignment Statements
65
sum 0; sum sum + 96; sum sum + 70; sum sum + 85; sum sum + 60; printf (" \nThe value of sum is printf (" \n sum is now %d. ", printf("\n sum is now %d.", printf("\n sum is now %d.", printf (" \n The final sum is
to
%d.",
sum);
Determihe the output that this program produces. 18. Enter, compile, and execute Program 2-3 on your computer system. 19. Enter, compile, and execute Program 2-4 on your computer system. 20. Enter, compile, and execute Program 2-5 on your computer system. 21. Using Program 2-3, determine the volume of cylinders having the following radii and heights:
22. Modify Program 2-3 to calculate the weight in pounds of the steel cylinder whose volume was found by the program. The formula for determining the weight is weight .28 1T h, where r is the radius in inches and h is the height in inches of the cylinder.
23. The area of an ellipse (see Figure 2-10) is given by the formula area = 1T" a" b. Using this formula, write a C program to calculate the area of an ellipse having a minor axis of 2.5 inches and a major axis of 6.4 inches.
FIGURE 2-10
66
Chapter Two
24a. The combined resistance of three resistors connected in parallel, as shown in Figure 2-11, is given by the equation Combined resistance
=
1 1
Write a C program to calculate and display the combined resistance when the three resistors R1 = 1000, Rz = 1000, and R3 = 1000 are connected in parallel. Your program should produce the display: The combined resistance, in ohIDs, is _
where the underlined spaces are to be replaced by the value of the combined resistance computed by your program. b. How do you know that the value calculated by your program is correct? c. Once you have verified the output produced by your program, modify it to determine the combined resistance when the resistors R1 = 1500, Rz = 1200, and R3 = 2000 are connected in parallel. 25a. Write a C program to calculate and display the value of the slope of the line connecting the two points whos~ coordinates are (3,7) and (8,12). Use the fact that the slope between two points having coordinates (x1,y1) and (x2,y2) is (y2 - y1) / (x2 - xl). b. How do you know that the result produced by your program is correct? c. Once you have verified the output produced by your program, modify it to determine the slope of the line connecting the points (2,10) and (12,6). 26a. Write a C program to calculate and display the coordinates of the midpoint of the line connecting the two points given in Exercise 25a. Use the fact that the coordinates of the midpoint between two points having coordinates (x1,y1) and (x2,y2) are Xl + X2) /2, (Y1 + Y2) /2). Your program should produce the following display: The x midpoint coordinate is The y midpoint coordinate is where the underlined spaces are to be replaced with the values calculated by your program. b. How do you know that the midpoint values calculated by your program are correct? c. Once you have verified the output produced by your program, modify it to determine the midpoint coordinateS'<ofthe line connecting the points (2,10) and (12,6).
FIGURE 2-11
2.4
Formatted Output
67
FIGURE 2-12
An Electrical Circuit
28a. For the electrical circuit shown in Figure 2-12, the currents Iv I2, and, I3 can be determined using the formulas
Using these formulas write a C program to compute the currents when R1 = 10 ohms, R2 = 4 ohms, R3 = 6 ohms, E1 = 12 volts, and E2 = 9 volts. The display produced by your program should be
Current Current Current i1 is 12 is i3 is
where the underlined spaces are to be replaced by the values determined in your program. b. How do you know that the currents calculated by your program are correct? c. Once you have verified the output produced by your program, modify it to determine the three currents for the following values: R1 = 1500, R2 = 1200, R3 = 2000, E1 = 15, and E2 = 12.
2.4
Formatted Output
Besides displaying correct results, it is extremely important for a program to present its results attractively. Most programs are judged, in fact, on their perceived ease of data entry and the style and presentation of their output. For example, displaying a monetary result as 1. 897000 is not in keeping with accepted report conventions. The display should be either $1. 90 or $1. 89, depending on whether rounding or truncation is used.
68
Chapter Two
The format of values displayed by pr in t f ( ) can be controlled by field width specifiers included as part of each conversion control sequence. For example, the statement
printf("The sum of%3d and%4d is%5d.", 6, 15, 21);
The numbers 3, 4, and 5 in the control string are the field width specifiers. The 3 causes the first number to be printed in a total field width of three spaces, in this case two blank spaces followed by the number 6. The field width specifier for the second conversion control sequence, %4d, causes two blank spaces and the number 15 to be printed for atotal field width of four spaces. The last field width specifier causes the 21 to be printed in a field of five spaces, which includes three blanks and the number 21. As illustrated, each integer is right-justified within the specified field. Field width specifiers are useful in printing columns of numbers so that the numbers in each column align correctly. For example, Program 2-7 illustrates how a column of integers would align in the absence of field width specifiers.
)01,
Program 2-7
#include main()
{
<stdio.h>
Since no field widths are given, the printf ( ) function allocates enough space for each number as it is received. To force the numbers to align on the units digit requires a field width wide enough for the largest displayed number. For Program 2-7, a width of three suffices. The use of this field width is illustrated in Program 2-8.
2.4
Formatted Output
69
}Ol,
Program 2-8
#include
main() {
<stdio.h>
18 124 148
Formatted floating point numbers require the use of two field width specifiers. The first specifier determines the total width of the display, including the decimal point; the second determines how many digits are printed to the right of the decimal point. For example, the'statement
printf(" 1%10.3fl ",25.67);
The bar character, I, is used to clearly mark the beginning and end of the display field. The field width specifier 10.3 tells printf ( ) to display the number in a total field of 10, which includes one decimal point and three digits to the right of the decimal point. Since the number contains only two digits to the right of the decimal point, the decimal part of the number is padded with a trailing zero. For all numbers (integers, floating point, and double precision), printf ( ) ignores the specified field width if the total field width is too small and allocates enough space for the integer part of the number to be printed. The fractional part of both floating point and double precision numbers is always displayed with the number of specified digits. If the fractional part contains fewer digits than specified, the number is padded with trailing zeros; if the fractional part contains more digits than called for in the specifier, the number is rounded to the indicated number of decimal places. Table 2-7 illustrates the effect of various field width specifiers.
70
Chapter Two
TABLE 2-7
Comments
Number fits in field Number fits in field Field width ignored Floating point in an integer field Field of 5 with 2 decimal digits Number fits in field Field width ignored but fractional specifier used Integer in a floating point field
1%5.2fl
2.366
1 2.371
1%5.2fl 1%5.2fl
42.3 142.364
142.301 1142.361
1%5.2fl
142
Machine dependent
Format Modifiers In addition to the conversion control sequences (%d,%f, etc.) and the field width specifiers that may be used with them, C also provides a set of format modifiers that provide additional format control, such as left and right field justification. Format modifiers, if used, must always be placed immediately after the % symbol. The more commonly used format modifiers are discussed here.
Left-Justification
Numbers displayed using the pr in t f ( ) function are normally displayed rightjustified with leading spaces inserted to fill the selected field width. To force the output to left-justify the display, a minus sign (-) format modifier can be used. For example, the statement printf("I%-lOdl causes the display
159
",59);
Again, we have used the bar symbol, I, to clearly identify the beginning and end of the designated display. Notice that the displayed number, 59, is printed at the beginning of the field (left-justification within the field) rather than at the end of the field, as would be obtained in the absence of the format modifier. Also notice that the format modifier within the printf ( ) function is placed immediately after the % symbol.
2.4
Formatted Output
71
lOll
#include main()
{
Program 2-9
<stdio.h>
/* a program
to illustrate
output
conversions
*/
1.0) value of 15 is %d.", 15); 8) value of 15 is %0.", 15); (base 16) value of 15 is %x.",
15);
The output produced by Program 2-9 is: The decimal (base 10) value of 15 is 15. The octal (base 8) value of 15 is 17. The hexadecimal (base 16) value of 15 is
5
F.
This topic may be omitted on first reading without loss of subject continuity.
72
Chapter Two
The display of integer values in one of the three possible number systems (decimal, octal, and hexadecimal) does not affect how the number is actually stored inside a computer. All numbers are stored using the computer's own internal codes. The conversion control sequences convert the. internal code for output display purposes. Besides displaying integers in octal or hexadecimal form, integer constants can also be written in a program in these forms. To designate an octal integer constant, the number must have a leading zero. The number 023, for example, is an octal number in C. Hexadecimal numbers are denoted using a leading Ox.The use of octal and hexadecimal integer constants is illustrated in Program 2-10.
)0),
Program 2-10
#include main()
{
<stdio.h>
printf("The printf("The
decimal decimal
value value
When Program 2-10 is run, the following output is obtained: The decimal The decimal value value o~ 025 is 21. of Ox37 is 55.
The relationship between the input, storage, and display of integers is illustrated in Figure 2-13. To force both octal and hexadecimal numbers to be printed with a leading 0 and Ox,respectively, the # format modifier must be used. For example, the statement printf("The produces the display The octal value of decimal 21 is 025 octal value of decimal 21 is %#0",21);
Without the inclusion of the # format modifier within the conversion control sequence %0,the displayed octal value would be 25, with no leading O.Similarly, the statement printf("The hexadecimal value of decimal 55 is %#x",55);
Without the inclusion of the # format modifier within the conversion sequence %x,the displayed hexadecimal value would be 37, with no leading Ox.
2.4
Formatted Output
73
1 1
:
1 1 1 1 1
I I I
1 1 1 1 1 1
1 1 1 1 1 1 1 1 1
I
1 1
/'
INTERNAL NUMBER CODE
1 1 1 1
I I I I I I I I
1 1 1 1
jr
1
Octal Display
I I
Decimal Display
.,
I I I I
I.
I
Hexadecimal Display
'-
Y
Input is either Octal, Decimal, or Hexadecimal
J \.
~ ~----J Y
Storage is always in Binary
'-
-~
y~-----
FIGURE 2-13
Exercises 2.4
1. Determine and write out the display produced by the following statements: a. printf (" I%dI" , 5) ; b. printf("1%4dl",5);
~ printf("1%4dl",56829); d. printf (" I%5.2f 1",5.26); e. printf (" I%5.2f 1",5.267);
74
Chapter Two
2. Write out the display produced by the following statements: a. printf ("The number is 16. 2f\n" ,26.27) ;
printf ("The number is 16. 2f\n" ,682.3) ; printf ("The number is 16.2f\n" ,1.968); b. printf (" $16. 2f\n" ,26.27) ; printf(" 16.2f\n",682.3); printf(" 16.2f\n",1.968); printf("--------\n") ; printf("$16.2f\n", 26.27 + 682.3 + 1.968); c. printf("$%5.2f\n",26.27); printf(" 15.2f\n",682.3); printf(" 15.2f\n",1.968); printf("--------\n"); printf("$15.2f\n", 26.27 + 682.3 + 1.968); d. printf ("15 .2f\n", 34.164); printf("15.2f\n",10.0b3); printf("-----\n"); printf("15.2f\n", 34.164 + 10.003);
3. Determine the errors in each of the following statements: a. printf("ld," 15) b. printf("lf", 33);
c. printf("%5d", 526.768); d. printf("a b c", 26, 15, 18); e. printf("13.6f", 47); f. printf ("13.6", 526.768); ~ printf(526.768, 33,"lf Id");
The sales tax is $ 1.80 The total bill is $37.80 #include <stdio.h> main()
{
b. Run the program written for Exercise 4a to verify the output display. 5. Write a C program that displays the results of the expressions (3. 0 * 5. 0), (7. 1 * 8 . 3 - 2.2), and (3.2 / (6 . 1 * 5)) on three separate lines. Each value displayed should be limited to two decimal positions to the right of the decimal point. Calculate the value of these expressions manually to verify that the displayed values are correct. 6. The combined resistance of three resistors connected in parallel, as shown in Figure 2-14, is given by the equation Combined resistance
1
If;
R;
1
R3
2.4
Formatted Output
75
FIGURE 2-14
Using this formula, write a C program to calculate and display the combined resistance when the three resistors R1 = 1000, Rz = 1000, and R3 = 1000 are connected in parallel. The output should produce the display: The combined resistance is xxxx. xx ohms, where XXXX.xx denotes that the calculated value should be placed in a field width of seven columns with two positions to the right of the decimal point. 7. Write a C program to calculate and display the value of the slope of the line connecting the two points whose coordinates are (3,7) and (8,12). Use the fact that the slope between two points having coordinates (x1,y1) and (x2,y2) is (y2 - y1) I (x2 - xl). The display produced by your program should be: The value of the slope is xxx. xx, where xxx. xx denotes that the calculated value should be placed in a field wide enough for three places to the left of the decimal point and two places to the right of it. 8. Write a C program to calculate and display the coordinates of the midpoint of the line connecting the two points given in Exercise 7. Use the fact that the coordinates of the midpoint between two points having coordinates (x1,y1) and (x2,y2) are X1+X2)/2, (Y1 + Y2) /2). The display produced by your program should be: The x coordinate The y coordinate of the midpoint of the midpoint is xxx.xx is xxx.xx
where xxx. xx denotes that the calculated value should be placed in a field wide enough for three places to the left of the decimal point and two places to the right of it. 9. Write a C program to calculate and display the maximum bending moment, M, of a beam that is supported on both ends (see Figure 2-15). The formula for maximum bending moment is M = X W (L - X) I L, where X is the distance from the end of the beam that a weight, W, is placed and L is the length of the beam. Let W = 500 rhs, x = 10 ft. and L = 25 ft. The display produced by your program should be: The maximum bending moment is XXXX.xxxx, where xxx. xxxx denotes that the calculated value should be placed in a field wide enough for four places to the right and left of the decimal point. .10. For the electrical circuit shown in Figure 2-16, the currents, Iv Iz, and 13 can be determined using the formulas
EzR3 + E1(R1 + R3) II = ---------~ (R1 + R3)(Rz + R3) - (R3)z
76
Chapter Two
FIGURE 2-15
12 =
----------
(R1
Using these formulas write a C program to compute the three currents when R1 = 1000 ohms, R2 = 400 ohms, R3 = 600 ohms, E1 = 12 volts, and E2 = 9 volts. The display produced by your program should be: Current il is xx.xxxxx Current i2 is xx.xxxxx Current i3 is xx.XXXXX where xx. xxxx denotes that the calculated value should be placed in a field wide enough for two places to the left of the decimal point and four places to the right of it.
FIGURE 2-16
2.5
Recall from Section 1.1 that writing a C program is essentially the last step in the programming process. The first step in the process is determining what is required and selecting the algorithm to be coded into C. In this section we present a five-step program development procedure called top-down development for converting programming problems into working C programs. To make this development procedure more meaningful, we first apply it to a simple program-
2.5
77
ming problem. As we will see, designing a program using a top-down approach results in a modular program design. The five steps in the top-down development procedure are: 1. Determine the desired output items that the program must produce. 2. Determine the input items. 3. Design the program as follows: a. Select an algorithm for transforming the input items into the desired outputs. b. Check the chosen algorithm by hand, using specific input values. c. Determine variable names for the selected algorithm 4. Code the algorithm into C. 5. Test the program using selected test data. Steps 1 and 2 in the development procedure are referred to as the program Analysis Phase, Step 3 is called the Design Phase, Step 4 the Coding Phase, and Step 5 the Testing Phase. In the analysis phase of program development (Steps 1 and 2) we are concerned with extracting the complete input and output information supplied by the problem. Together these two items are referred to as the problem's input/output, or I/O for short. Only after a problem's I/O has been determined is it possible to select an algorithm for transforming the inputs into the desired outputs. For example, consider the following simple programming problem: The electrical resistance of a metal wire, in ohms, is given by the formula r = (ml) / a where m is the resistivity of the metal; 1 is the length of the wire in feet; and a is the cross-sectional area of the wire in circular mils. Using this information, write a C program to calculate the resistance of a wire that is 125 feet long, has a cross-sectional area of 500 circular mils, and is copper. The resistivity of copper is lOA. Step 1: Determine the Desired Output The first step in developing a program for this problem statement is to determine the required outputs (Step 1 of the development procedure). Frequently, the statement of the problem will use such words as calculate, print, determine, find, or compare, which can be used to determine the desired outputs. For our sample problem statement, the key phrase is "to calculate the resistance of a wire." This clearly identifies an output item; Since there are no other such phrases in the problem, only one output item is required. Step 2: Determine the Input Items After we have clearly identified the desired output, Step 2 of the development process requires that we identify all input items. It is essential at this stage to distinguish between input items and input values. An input item is the name of an input quantity, while an input value is a specific number or quantity that the input item can be. For example, in our sample problem statement the input items are the resistivity, m, the length of the wire, 1, and the cross-sectional area of the wire, a. Although these input items have specific numerical values, these input item values are generally not important in Step 2.
78
Chapter Two
The reason that input values are not needed at this point is that the initial selection of an algorithm is typically independent of specific input values; the algorithm depends on knowing what the output and input items are and if there are any special limits. Let us see why this is so as we determine a suitable algorithm for our sample problem statement. Step 3a: Determine an Algorithm From the problem statement it is clear that the algorithm for transforming the input items to the desired output is given by the formula r = (ml) / a. Notice that this formula can be used regardless of the specific values assigned to m, I, and a. Although we cannot produce an actual numerical value for the output item, resistance, unless we have actual numerical values for the input items, the correct relationship between inputs and outputs is expressed by the formula. Recall that this is precisely what an algorithm provides: a description of how the inputs are to be transformed into outputs that works for all inputs. Thus, the complete algorithm, in pseudocode, for solving this problem is:
Assign values to m, I, and a Calculate the resistance using the formula r=(ml)/a Display the result
Step 3b: Do a Hand Calculation After an algorithm has been selected, the next step in the design procedure, Step 3b, is to check the algorithm manually using specific data. Performing a manual calculation, either by hand or using a calculator, helps to ensure that you really do understand the problem. An added feature of doing a manual calculation is that the results can be used later to verify the operation of your program in the testing phase. Then, when the final program is used with other data, you will have established a degree of confidence that a correct result is being calculated. Doing a manual calculation requires that we have specific input values that can be applied to the algorithm to produce the desired output. For this problem three input values are given: a resistivity of 10.4, a cross-sectional area of 500 circular mils, and a length of 125 feet. Substituting these values into the formula, we obtain a resistance of 2.60 ohms for the copper wire. Step 3c: Select Variable Names The last step in the design phase (Step 3c) is to choose the names of variables to hold the input, output, and any intermediate calculated items determined in the analysis phase (Steps 1 and 2). Let us use the variables named restvy, area, and length for the input items resistivity, area, and length, respectively; and a variable named resist for the calculated output, the resistance of the wire. All of these names are arbitrary and any valid symbolic names can be used in their place. Step 4: Write the Program Since we have selected variable names for required is for our program to declare these ables appropriately; compute the resistance resistance value. Program 2-11 performs these the chosen algorithm, all that is variables; initialize the input varivariable; and print the calculated steps.
2.5
79
)ql,
#include main( )
{
Program 2-11
<stdio.h>
float restvy,
area,
length,
resist;
restvy = 10.4; area = 500; length = 125; resist = (restvy * length) / area; printf("\n The resistance of the wire
(in ohms)
is %f", resist);
Now that we have a working program that produces a result, the final step in the development process, testing the program, can begin. Step 5: Test the Output The purpose of testing is to verify that a program works correctly and actually fulfills its requirements. Once testing has been completed the program can be used to calculate outputs for differing input data without the need for retesting. This is, of course, one of the real values in writing a program: the same program can be used over and over with new input data . . In theory, testing would reveal all existing program errors (in computer terminology, a program error is called a bug). In practice, this would require checking all possible combinations of statement execution. Because of the time and effort required, this is usually an impossible goal except for extremely simple programs such as Program 2-11. (We illustrate why this is generally an impossible goal in Chapter 4, which describes C's IF statements.) The inability to completely test most programs has led to various testing methodologies. The simplest of these methods is to verify the program's operation for carefully selected sets of input data. One set of input data that should always be used is the data that was selected for the hand calculation made previously in Step 3b of the development procedure. If testing reveals an error (bug), the process of debugging, which includes locating, correcting, and verifying the correction, can be initiated. It is important to realize that although this type of verification testing may reveal the presence of an error, it does not necessarily indicate the absence of one. Thus, the fact that a test does not reveal an error does not indicate that another bug is not lurking somewhere else in the program. Modularity and Top-Down Design The design of Program 2-11 was relatively simple. For more complex problems the design of a program's structure can be considerably more involved. In its
80
Chapter Two
more elaborate form, designing a program is similar to receiving the pieces of a puzzle (the inputs) and deciding how to arrange them to form a completed structure (the desired output). Unlike a jigsaw puzzle, however, the pieces of a program design puzzle can be arranged in many different ways depending on the algorithm chosen for transforming the inputs into the desired outputs. In this regard, the program designer is similar to an architect who must draw up the plans for a house. The general procedure for designing programs (Step 3 in our development procedure) is called top-down design. The purpose of top-down design is to design an algorithm that results in a modular program structure. To achieve this goal the design starts from the highest level requirement and proceeds to the parts that must be constructed to achieve this requirement. To make this more meaningful, consider that an inventory reporting program is required to keep track of the number of parts in inventory. The required output for this program is a description of all parts carried in inventory and the number of units of each item in stock; the given inputs are the initial inventory quantity of each part, the number of items sold, the number of items returned, and the number of items purchased. For these I/O specifications, a designer could initially organize the requirements for the program into the three sections illustrated in Figure 2-17. This is called a first-level structure diagram because it represents the first overall structure of the program selected by the designer. In top-down design the lower boxes in the structure diagram are refined until the tasks indicated in the boxes are small enough to be programmed as individual program units. For example, both the data entry and report subsections shown in Figure 2-17 would be further refined into suitable segments. The data entry section certainly must include provisions for entering the data. Since it is the system designer's responsibility to plan for contingencies and human error, provisions must also be made for handling incorrect data after an entry has been made and for deleting a previously entered value altogether. Similar subdivisions for the report section can also be made. Figure 2-18 illustrates a second-level structure diagram for an inventory tracking system that includes these further refinements. The process of refinement continues until the last level of tasks can be coded using individual program units. Notice that the design produces a tree-like structure where the levels branch out as we move from the top of the structure to the bottom. When the design is complete it specifies both how many program units are needed and the calling sequence of each unit (that is, lower level units are
FIGURE 2-17
I
Data Entry Section
I
Calculation Section
I
Report Section
2.5
81
I
Calculation Section
I
Report Section
I
I I
Change Data
I
Delete Data
I
Screen Reports
I Printer Reports
Enter Data
FIGURE 2-18
called from higher level ones). The individual algorithms specified for each box on the final structure diagram, which are coded using separate functions, are frequently described using either flowcharts or pseudocode.
Exercises 2.5
Note: In each of these exercises a programming problem is given. Read the problem statement first and then answer the questions pertaining to the problem. Do not write the program. 1. Consider the following programming problem: A C program is required that calculates the amount, in dollars, contained in a piggybank. The bank contains half-dollars, quarters, dimes, nickels, and pennies. a. For this programming problem how many outputs are required? b. How many inputs does this problem have? c. Determine an algorithm for converting the input items into output items. d. Test the algorithm written for part c using the following sample data: half-dollars = 0, quarters = 17, dimes = 24, nickels = 16, pennies = 12. 2. Consider the following programming problem: A C program is required to calculate the value of distance, in miles, given the relationship distance
=
a. For this programming problem how many outputs are required? b. How many inputs does this problem have? c. Determine an algorithm for converting the input items into output items. d. Test the algorithm written for part c using the following sample data: rate is 55 miles per hour and elapsed time is 2.5 hours. e. How must the algorithm you determined in part c be modified if the elapsed time is given in minutes instead of hours? 3. Consider the following programming problem: A C program is required to determine the value of Ergies, given the relationships
82
Chapter Two
Ergies = Fergies . ~Lergies Lergies = 2P a. For this programming problem how many outputs are required? b. How many inputs does this problem have? c. Determine an algorithm for converting the input items into output items. d. Test the algorithm written for part c using the following sample data: Fergies = 14.65, and P = 8. 4. Consider the following programming problem: A C program is required to display the following name and address: Mr. J. Knipper 63 Seminole Way Dumont, N.J. 07030 a. For this program problem how many lines of output are required? b. How many inputs does this problem have? c. Determine an algorithm for converting the input items into output items. 5. Consider the following program problem: A C program is required to determine how far a car has traveled after 10 seconds, assuming the car is initially traveling at 60 miles per hour and the driver applies the brakes to uniformly decelerate at a rate of 12 miles/ sec2 Use the fact that distance = s - (1/2)dt2, where s is the initial speed of the car, d is the deceleration, and t is the elapsed time. a. For this programming problem how many outputs are required? b. How many inputs does this problem have? c. Determine an algorithm for converting the input items into output items. d. Test the algorithm written for part c using the data given in the problem. 6. Consider the following programming problem: In 1627, Manhattan Island was sold to the Dutch settlers for approximately $24. If the proceeds of that sale had been deposited in a Dutch bank paying 5 percent interest, compounded annually, what would the principal balance be at the end of 1990? A display is required as follows: Balance as of December 31, 1990 is: where the underlined blank spaces are to be filled in by the amount calculated by your program. a. For this programming problem how many outputs are required? b. How many inputs does this problem have? c. Determine an algorithm for converting the input items into output items. d. Test the algorithm written for part c using the data given in the problem statement. 7. Consider the following programming problem: A C program is required that calculates and displays the weekly gross pay and net pay of two individuals. The first individual is paid an hourly rate of $8.43 and the second individual is paid an hourly rate of $5.67. Both individuals have 20 percent of their gross pay withheld for income tax purposes and both pay 2 percent of their gross pay, before taxes, for medical benefits. a. For this programming problem how many outputs are required? b. How many inputs does this problem have? c. Determine an algorithm for converting the input items into output items. d. Test the algorithm written for part c using the following sample data: The first person works 40 hours during the week and the second person works 35 hours. 8. Consider the following programming problem: The formula for the standard normal deviate, z, used in statistical applications is:
Z=
X-Il
(j
2.6
Applications
83
where I.l refers to a mean value and (J to a standard deviation. Using this formula, A C program is required that calculates and displays the value of the standard normal deviate when X = 85.3, I.l = 80, and (J = 4. a. For this programming problem how many outputs are required? b. How many inputs does this problem have? c. Determine an algorithm for converting the input items into output items. d. Test the algorithm written for part c using the data given in the problem. 9. Consider the following programming problem: The equation of the normal (bellshaped) curve used in statistical applications is: y = --e
1
-[(~)(x-u)/"12
cr-fi;;
Using this equation, A C program is required to calculate the value of y. a. For this programming problem how many outputs are required? b. How many inputs does this problem have? c. Determine an algorithm for converting the input items into output items. d. Test the algorithm written for part c using assuming I.l = 90, (J = 4, x = 80, e 2.71828 and 1t = 3.1416.
2.6 Applications
In this section we apply the top-down development procedure presented in the previous section to two specific applications. Although each application is different, the top-down development procedure can be applied to any programming problem to produce a completed program. Application 1: Pendulum Clocks Pendulums used in clocks keep relatively accurate time for the following reason: When the length of a pendulum is relatively large compared to the maximum arc of its swing the time to complete one swing is independent of both the pendulum's weight and the maximum displacement of the swing. When this condition is satisfied the relationship between the time to complete one swing and the length of the pendulum is given by the formula length
=
g [time/(27t)f
where 7t,accurate to four decimal places, is equal to 3.1416 and g is the gravitational constant equal to 32.2 ft/ sec. When the time of a complete swing is given in seconds, the length of the pendulum is in feet. Using the given formula, write a C program to calculate and display the length of a pendulum needed to produce a swing that will be completed in one second. The length should be displayed in inches.
Program Development
Using our five-step development procedure we have: Step 1: Determine the Required Outputs For this problem a single output is required by the program: the length ofthe pendulum. The problem also specifies that the actual value be displayed in units of inches.
84
Chapter Two
Step 2: Determine All Input Items The input items required for this problem are the time to complete one swing, the gravitational constant, g, and pi. Step 3: Design the Program a. The algorithm for transforming the three input items into the desired output item is given by the formula length = g [time/(21t)f, Since this formula calculates the length in feet, we will have to multiply the result by 12 to convert the answer into inches. b. A hand calculation, using the data that g = 32.2, time = 1, and 1t = 3.1416, yields a length of 9.78 inches for the pendulum. c. We select the variable names time for the time and length for the length. As g and pi are constants that do not change, we will not assign them to variables; instead, their values will be directly incorporated in the assignment statement used to determine the pendulum's length. Step 4: Write the Program Program 2-12 provides the necessary code.
~Oll Program
#include main( )
{
2-12
<stdio.h>
float
time,
length;
1(2.0*3.1416);
Program 2-12 begins with a #inc 1ude preprocessor command followed by a main ( ) function. This function starts with the reserved word main and ends with the closing brace, }. Additionally, Program 2-12 contains one declaration statement, two assignment statements, and one output statement. The assignment statement time = 1. 0; is used to initialize the time variable. The assignment statement length
1(2.0*3.1416)
* time/(2.0*3.1416);
calculates a value for the variable length. Notice that the 12.0 is used to convert the calculated value from feet into inches. Also notice the placement of parentheses in the expression time 1 (2 . 0*3 . 1416 ). The parentheses ensures that the value of 1t is multiplied by 2 . 0 before the division is performed. If these parentheses were not included, the value of time would first be divided by 2 . 0, and then the quantity time 12 . 0 would be multiplied by 1t.Finally, this same quantity is multiplied by itself to obtain the necessary squared value (in the next chapter we will see how to use C's power function to obtain the same result). When Program 2-12 is compiled and executed the following output is produced: The length is
9.78 inches.
2.6
Applications
85
Step 5: Test the Program The last step in the development procedure is to test the output of the program. As the displayed value agrees with the previous hand calculation, we have established a degree of confidence in the program. This permits us to use the program for different values of time. Note that if the parentheses were not correctly placed in the assignment statement that calculated a value for length, the displayed value would not agree with our previous hand calculation. This would have alerted us to the fact that there was an error in the program.
Application 2: Telephone Switching Networks A directly connected telephone network is one in which all telephones in the network are directly connected and do not require a central switching station to establish calls between two telephones. For example, financial institutions on Wall Street use such a network to maintain direct and continuously open phone lines between institutions. The number of direct lines, I, needed to maintain a directly connected network for n telephones is given by the formula 1= n(n-l)/2 For example, directly connecting 4 telephones without the use of a central switching station requires 6 individual lines (see Figure 2-19). Adding a fifth telephone to the network illustrated in Figure 2-19 would require an additional 4 lines for a total of 10 lines. Using the given formula write a program that determines the number of direct lines required for 100 subscribers, and the additional lines required if 10 new subscribers were added to the network.
Program Development
Using our five-step development procedure we have: Step 1: Determine the Required Outputs For this program two outputs are required: the number of direct lines for 100 telephones and the additional number of lines needed when 10 new telephones are added into the existing network. Step 2: Determine All Input Items The input items required for this problem are the number of telephones, denoted as n in the formula.
FIGURE 2-19
Telephone #4
Line 4
Line 2
Telephone #1
Telephone #2
86
Chapter Two
1 = n(n-l)/2. Although there is no formula given for additional lines, we can use the given formula to determine the total number of lines needed for 110 telephones. Subtracting the number of lines for 100 telephones from the number of lines needed for 110 telephones yields the number of additional lines required. Thus, the complete algorithm for our program, in pseudocode, is:
Calculate the number of direct lines for 100 telephones Calculate the number of direct lines for 110 telephones Calculate the additional lines needed, which is the difference between the second and first calculations Display the number of lines for 100 telephones Display the additional lines needed
= 4950 lines
for 100 telephones and that 1 = 5995 direct lines are needed for 110 telephones. Thus, an additional 1045 lines would be needed to directly connect the 10 additional telephones into the existing network. c. We select the variable name nurninfor the initial number of 100 telephones, the variable name nurnfinfor the final number of 110 telephones, lines1 for the initial number of lines, and lines2 for the final number of lines. Step 4: Write the Program Program 2~13 provides the necessary code.
$dl,
{
Program 2-13
#include <stdio.h> main ( ) int nurnin, nurnfin, lines1, lines2; nurnin = 100; nurnfin 110; lines1 = nurnin * (nurnin - 1)/2; lines2 = nurnfin * (nurnfin - 1)/2; printf("\nThe number of initial lines is %d", linesl); printf("\nThere are %d additional lines needed.", lines2 - lines1);
As before, the C program begins with the reserved word main and ends with the closing brace, }. Since the number of lines between telephones must be an integer (a fractional line is not possible), the variables lines1 and lines2 are specified as integer variables. The first two assignment statements initialize the
2.6
Applications
87
variables numin and numf in. The next assignment statement calculates the number of lines needed for 100 telephones and the last assignment statement calculates the number of lines for 110 telephones. The first call to printf ( ) is used to display a message and the result of the first calculation. The second call to printf ( ) is used to display the difference between the two calculations. The following output is produced when Program 2-13 is compiled and executed:
The number of initial lines is 4950 There are 1045 additional lines needed.
to
complete one swing. Your program should produce the following display: The time to complete one swing (in seconds) is: _
where the underlined blank spaces are replaced by the actual value calculated by your program. b. Compile and execute the program written for Exercise 2a on a computer. Make sure to do a hand calculation so that you can verify the results produced by your program. c. After you have verified the results of the program written in Exercise 2a, modify the program to calculate the time it takes a four-foot pendulum to complete one swing. 3a. Modify Program 2-13 to calculate and display the total number of lines needed to directly connect 1000 individual phones to each other. b. Compile and execute the program written for Exercise 3a on a computer. 4a. Modify Program 2-13 so that the variable numfin is initialized to 10, which is the additional number of subscribers to be connected to the existing network. Make any other changes in ~he program so that the program produces the same display as Program 2-13. b. Compile and execute the program written for Exercise 4a on a computer. Check that the display produced by your program matches the display shown in the text. Sa. Write, compile, and execute a C program to convert temperature in degrees Fahrenheit to degrees Celsius. The equation for this conversion is Celsius = 5.0/9.0 (Fahrenheit - 32.0). Have your program convert and display the Celsius temperature corresponding to 98.6 degrees Fahrenheit. Your program should produce the display: For a fahrenheit temperature of The equivalent celsius temperature degrees is degrees
where appropriate values are inserted by your program in place of the underlined blank spaces. b. Check the values computed by your program by hand. After you have verified that your program is working correctly, modify it to convert 86.5 degrees Fahrenheit into its equivalent Celsius value. 6a. Write, compile, and execute a C program to calculate the dollar amount contained in
88
Chapter Two
a piggy bank. The bank currently contains 12 half-dollars, 20 quarters, 32 dimes, 45 nickels, and 27 pennies. Your program should produce the following display: The value of money, in dollars. is xx.xx
where xx . xx is replaced by the actual value calculated by your program and represents a total field width of 5 places with 2 places to the right of the decimal point. b. Check the values computed by your program by hand. After you have verified that your program is working correctly, modify it to determine the dollar value of a bank containing no half-dollars, 17 quarters, 19 dimes, 10 nickels, and 42 pennies. 7a. Write, compile, and execute a C program to calculate the elapsed time it took to make a 183.67 mile trip. The equation for computing elapsed time is elapsed time = total distance / average speed. Assume that the average speed during the trip was 58 miles per hour. Your program should produce the display The time for the trip was xx.xx hours
where xx. xx is replaced by the actual value calculated by your program and represents a total field width of 5 places with 2 places to the right of the decimal point. b. Check the values computed by your program by hand. After you have verified that your program is working correctly, modify it to determine the elapsed time it takes to make a 372 mile trip at an average speed of 67 miles/hour.
8a. Write, compile, and execute a C program to calculate the sum of the numbers from 1 to 100. The formula for calculating this sum is sum = (n/2) (2*a + (n-1)d), where n =
number of terms to be added, a = the first number, and d number. Your program should produce the display The sum of the numbers
is xxxx
where the xxxx represents a field width of four digits and should be replaced by the sum computed by your program. b. Check the values computed by your program by hand. After you have verified that your program is working correctly, modify it to determine the sum of the integers from 100 to 1000. Note: Exercises 9 through 13 require raising a number to a power. This can be accomplished using C's power function pow ( ). For example, the statement pow (2 0, 5 . 0) ; raises the number 2 . 0 to the 5th power, and the statement pow (numl, num2) ; raises the variable numl to the num2 power. To use the power function the declaration statement double pow ( ); should be included with the variable declaration statements used in your program. The power function is explained in more detail in Section 3.1. 9a. Newton's law of cooling states that when an object with an initial temperature Tis placed in a surrounding substance of temperature A, it will reach a temperature TFIN in t minutes according to the formula
TFIN = (T - A)
e-kt + A
In this formula e is the irrational number 2.71828 rounded to five decimal places, commonly known as Euler's number, and k is a thermal coefficient, which depends on the material being cooled. Using this formula write, compile, and execute a C program that determines the temperature reached by an object after 20 minutes when it is placed
2.6
Applications
89
in a glass of water whose temperature is 60 degrees. Assume that the object initially has a temperature of 150 degrees and has a thermal constant of 0.0367. Your program should produce the display The final temperature is xxx.xx where xxx. xx represents a total field width of six digits, which includes a leading sign and two digits to the right of the decimal point. The value placed in this field should be calculated by your program. b. Check the value computed by your program by hand. After you have verified that your program is working correctly, modify the object to determine the temperature reached after 10 minutes when the object is placed in a glass of water whose temperature is 50 degrees. lOa. Given an initial deposit of money, denoted as A, in a bank that pays interest annually, the amount of money at a time N years later is given by the formula Amount
=
A * (1 + nN
where I is the interest rate as a decimal number (e.g., 9.5 percent is .095). Using this formula write, compile, and execute a C program that determines the amount of money that will be available in 4 years if $10,000 is deposited in a bank that pays 10 percent interest annually. Your program produce the display The value after xx years is yyyy.yy where the xs represent a field width of two and the ys represent a field width of seven, with two digits to the right of the decimal point. The xs are to be replaced by the number of years and the ys by the value of money calculated by your program. (See the note prior to Exercise 9 for information on C's power function.) b. Check the value computed by your program by hand. After you have verified that your program is working correctly, modify it to determine the amount of money available if $24 dollars is invested at 4 percent for 300 years. Increase the field sizes of your output statement to accommodate both the number of years and the final amount. lla. The present value of a dollar amount is the amount of money that must be deposited in a bank account today to yield a specified dollar amount in the future. For example, if a bank is currently paying 8 percent interest annually, you would have to deposit $6,947.90 in the bank today to have $15,000 in 10 years. Thus, the present value of the $15,000 is $6,947.90. Using this information, write, compile, and execute a C program that calculates how much must be deposited in a bank today to provide exactly $8,000 in 9 years at an annual interest rate of 8 percent. Use the formula Present value = Future amount / (1.0 + annual interest rate)Years (See the note prior to Exercise 9 for information on C's power function.) b. Check the value computed by your program by hand. After you have verified that your program is working correctly, modify it to determine the amount of money that must be invested in a bank today to yield $15,000 in 18 years at an annual rate of 6 percent. l2a. The set of linear equations
anXI a21XI
+ a12X2 = CI + a22x2 = C2
90
Chapter Two
X2=----allan
allc2
cla21 al~21
X2
Using these equations, write, compile, and execute a C program to solve for the values that satisfy the following equations:
3xI 5xI
XI
and
+ 4X2 = 40 + 2X2 = 34
b. Check the values computed by your program by hand. After you have verified that your program is working correctly, modify it to solve the following set of equations: 3 XI + 12.5 X2 4.2 XI - 6.3 X2
= 22.5 =
30
2.7
1. Forgetting to declare all the variables used in a program. This error is detected by the compiler and an error message is generated for all undeclared variables. 2. Storing an incorrect data type in a declared variable. This error is detected by the compiler and the assigned value is converted to the data type of the variable it is assigned to. However, assigning a floating point value to an integer variable will result in the loss of the fractional part. 3. Using a variable in an expression before a value has been assigned to the variable. Here, whatever value happens to be in the variable will be used when the expression is evaluated, and the result will be meaningless. 4. Dividing integer values incorrectly. This error is usually disguised within a larger expression and can be very troublesome to detect. For example, the expression
3.425 + 2/3 + 7.9
2.8
Chapter Summary
91
5. Mixing data types in the same expression without clearly understanding the effect produced. Since C allows expressions with "mixed" data types, it is important to be clear about the order of evaluation and the data type of all intermediate calculations. As a general rule it is better never to mix data types in an expression unless a specific effect is desired. 6. Not including the correct number and type of conversion control sequences in print f ( ) function calls for the data types of the remaining arguments. 7. Not closing the control string in printf ( ) with a double quote symbol" followed by a comma when additional arguments are passed to printf ( ). 8. Forgetting to separate all arguments passed to print f ( ) with commas.
<stdio.h>
declaration
statements;
other statements;
92
Chapter Two
7. An expression is any combination of constants and/ or variables that can be evaluated to yield a value. 8. Expressions are evaluated according to the precedence and associativity of the operators used in the expression. 9. The assignment symbol, =, is an operator. Expressions using this operator assign a value to a variable; additionally, the expression itself takes on a value. Since assignment is an operation in C, multiple uses of the assignment operator are possible in the same expression. 10. The increment operator, ++, adds one to a variable, while the decrement operator, - -, subtracts one from a variable. Both of these operators can be used as prefixes or postfixes. In prefix operation the variable is incremented (or decremented) before its value is used. In postfix operation the variable is incremented (or decremented) after its value is used. 11. Although assignment and output statements can be placed in any order within a program after the declaration statements, it only makes sense to use a printf ( ) function call to display the contents of a variable after a value has been assigned to it.
2.9
Two major items associated with every variable are the value stored in it and the variable's address. In C, the value stored in a variable is formally referred to as the variable's rvalue, and the address of the variable is called the variable's lvalue. These two terms make more sense when a typical illustration of a variable is considered, as illustrated in Figure 2-20. As shown in this figure, the address of the variable is typically written on the left of the figure and the variable's contents (the value in the box) on the right. Programmers are usually concerned only with the value assigned to a variable (its contents, or rvalue) and give little attention to where the value is stored (its address, or lvalue). For example, consider Program 2-14.
r---
A.
---\
Variable Address
\._-V
--_/
rvalue
2.9
93
~Ol\ Program
#include main( )
{
2-14
<stdio.h>
int
num;
value
stored
in
num is
%d.", num) ;
All that Program 2-14 does is print out the value 22, which was assigned to the integer variable num. We can go further, however, and ask "Where is the number 22 actually stored?" Although the answer is "In num," this is only half of the answer. The variable name num is simply a convenient symbol for real, physical locations in memory, as illustrated in Figure 2-21. To determine the address of num (its lvalue), we can use C's address operator, &, which means "the address of," directly in front of the variable name (no space between & and the variable). For example, &num means the address of num, &total means the address of total, and &price means the address of price. Program 2-15 uses the address operator to display the address of the variable
num.
,QI,
Program 2-15
#include main( )
{
<stdio.h>
int
num;
%d
The address
of
num
%p.",
num,
&num);
22
The address
of
num
FFFO
Figure 2-22 illustrates the additional address information provided by the output of Program 2-15. Clearly, the address output by Program 2-15 depends on the computer used to run the program. Every time Program 2-15 is executed, however, it displays a
94
Chapter Two
"
22
xxxx
V
--~)
FIGURE 2-21
Somewherein Memory
the hexadecimal address of the first byte used to store the variable num. Note also that the address is printed using the conversion control sequence %p. The %p conversion control sequence simply provides us with a convenient way of displaying an address. The display has no impact on how addresses are used internal to the program; it merely provides us with a useful representation that is helpful in understanding what addresses are.6 As we shall see, using addresses as opposed to only displaying them provides the C programmer with an extremely powerful programming tool. Addresses provide the ability to penetrate directly into the inner workings of the computer and access the computer's basic storage structure. This ability, which is more
FIGURE 2-22
_--A(
"
FFEO
(Decimal 65520)
22
t
Ivalue Address of First) ( Memory Location Used by Dum rvalue (Contents of Dum)
6 In non-ANSI C, the %p conversion control sequence may not be available. For these compilers use the conversion sequence %u, which forces the address to be treated as an unsigned integer data type, and what is displayed is printf ( ) 's representation of the address in this format. An address, however, is not an unsigned integer data type-it is a unique data type that mayor may not require the same amount of storage as an unsigned integer. The use of the %uconversion control sequence simply provides us with a convenient way of displaying an address when the %p is unavailable.
2.9
95
important to systems programmers than applications programmers, gives C programmers capabilities and programming power that are just not available in most other computer languages. Determining Storage Size In addition to allowing us to see a variable's address, C also provides an operator for determining the amount of storage allocated for each data type. This operator, called the sizeof ( ) operator, returns the number of bytes of the variable or data type included in the parentheses. Examples of the sizeof ( ) operator are: sizeof(numl) sizeof(int) sizeof (float)
If the item in parentheses is a variable, as in the example sizeof (numl), sizeof ( ) returns the number of bytes of storage that the computer reserved for the variable. If the item following the word sizeof is a data type, such as int or float, sizeof ( ) returns the number of bytes of storage that the computer uses for the given data type. Using either approach, we can use sizeof to determine the amount of storage used by different data types. Consider Program 2-16.
}Ol,
#include main( )
{
Program 2-16
<stdio.h>
int
printf("\nBytes
Program 2-16 declares that the variflble numl is used to store an integer. The s i z eo f ( ) operator is then used to tell us how much room the computer actually set aside for the variable numl. The sizeof( ) operator itself is used as an argument to the printf ( ) function. When Program 2-16 is run on an IBM personal computer the following output is obtained: Bytes of storage used by an integer: 2
In using the sizeof ( ) operator, one caution should be noted. The number of bytes given by the sizeof ( ) operator is in terms of the storage reserved for one character. In ANSI C the specified storage allocation is one byte per character, so the value returned by si teof ( ) is a true hyte count.
Chapter Three
3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 Mathematical Library Functions The scanf ( ) Function scanf ( ) with Buffered Input Symbolic Constants Applications Common Programming Errors Chapter Summary Enrichment Study: Program Life Cycle
96
3.1
97
In the previous two chapters we explored how results are displayed using C's printf ( ) function and how numerical data are stored and processed using variables and assignment statements. In this chapter we complete our introduction to C by presenting additional processing and input capabilities.
3.1
As we have seen, assignment statements can be used to perform arithmetic computations. For example, the assignment statement volts
=
current
* resist;
multiplies the value in current times the value in resist and then assigns the resulting value to volts. Although addition, subtraction, multiplication, and division are easily accomplished using C's arithmetic operators, no such operators exist for raising a number to a power, finding the square root of a number, or determining trigonometric values. To facilitate the calculation of powers, square roots, trigonometric, logarithmic, and other mathematical calculations frequently required in scientific and engineering programs, C provides standard preprogrammed functions that can be included in a program. Like the printf ( ) function with which you are already familiar, the available mathematical functions are stored in a system library that contains the collection of standard and tested functions available on your system. Before using one of C's mathematical functions, you must know: The name of the desired mathematical function What the mathematical function does The type of data required by the mathematical function The data type of the result returned by the mathematical function To illustrate the use of C's mathematical functions, consider the mathematical function named sqrt, which calculates the square root of a number. The square root of a number is computed using the expression sqrt(number) where the function's name, in this case sqrt, is followed by parentheses containing the number for which the square root is desired. The purpose of the parentheses following the function name is to provide a funnel through which data can be passed to the function (see Figure 3-1). The items that are passed to the function through the parentheses are called arguments of the function and constitUte its input data. For example, the following expressions are used to compute the square root ofthe arguments 4 . 0, 17 . 0, 2 5 . 0, 1043 . 29, and 6 . 4516:
98
Chapter Three
sqrt function
FIGURE 3-1
The argument to the function named sqrt must be a double precision value. The sqrt function computes the square root of its argument and the returned result is itself a double precision value. The values returned by the previous expressions are:
In addition to the sqrt function, the more commonly used mathematical functions provided in C are listed in Table 3-1. Although some of the mathematical functions listed require more than one argument, all mathmatical functions, return a single value. Table 3-2 lists the value returned by selected functions using example arguments. Note that the argument types for the examples agree with those given in Table 3-1 for the specified function.
FIGURE 3-2
(data passed
\._----~
to function);
,...----_/ V
--_/
3.1
Mathematical
Library Functions
99
When a mathematical function is used it is called into action by giving the name of the function and passing any data to it within the parentheses following the function's name (see Figure 3-2). The arguments that are passed to a function need not be single constants. Expressions can also be arguments provided that the expression can be computed
TABLE
3-1 Commonly
abs(i) fabs(d) pow(dl,d2) exp(d) sqrt(d) sin (d) cos (d) tan(d) tanh (d) log (d) logl0(d)
TABLE
Example
100
Chapter Three
to yield a value of the required data type. For example, the following arguments are valid for the given functions: sqrt(4.0 + 5.3 * 4.0) sqrt(16.0 * 2.0 - 6.7) sqrt(x * y - z/3.2) abs(2.3 * 4.6) sin (theta - phi) cos(2.0 * omega)
The expressions in parentheses are first evaluated to yield a specific value. Thus, values would have to be assigned to the variables theta, phi, x, y, z, and omega before their use in the above expressions. After the value of the argument is calculated, it is passed to the function. Functions may be included as part of larger expressions. For example: 4 * sqrt(4.5 * 10.0 - 9.0) 4 * sqrt(36.000000) 4 * 6.000000 24.000000 and 3.0 * log(30 * .514) 3..0 * log(15.42) 3.0 * 2.735665 2.0 2.0 2.0 2.0
22.000000
8.206995
Step
Result
Program 3-1 illustrates the use of the sqrt function to determine the time it takes a ball to hit the ground after it has been dropped from an 800 foot tower. The mathematical formula used to calculate the time in seconds that it takes to fall a given distance in feet is: time
=
sqrt(2 * distance I g)
3.1
101
11,
Program 3-1
#include main( )
{
<stdio.h>
double double
height = 800.0; time = sqrt(2.0 * height / 32.2); printf("\nlt will take %4.2lf seconds", time); printf("\nto fall %7.3lf feet.", height);
Notice that Program 3-1 contains a declaration statement for the sqrt ( ) function. Although the actual code for the sqrt ( ) function is contained in the standard system library, the proper declaration for the function must be provided by the programmer in any program using the function. Alternatively, all compilers have a standard header file named rna th .h that contains appropriate declaration statements for the supplied mathematical functions. To include the information in this file in your program, allowing you to use all the mathematical functions without explicitly typing declaration statements for each function used, the following preprocessor statement must be included with your program:
#include <math.h>
no semicolon
Thus, using this statement Program 3-1 can be rewritten as Program 3-2 (the order of the two #include commands can be reversed).
11,
Program 3-2
#include #include main( )
{
<math.h> <stdio.h>
double
time, height;
height = 800.0; time = sqrt(2.0 * height / 32.2); printf("\nlt will take %4.2lf seconds", time); printf("\nto fall %7.3lf feet.", height);
102
Chapter Three
Notice in Program 3-2 that the declaration statement double sqrt ( ) ; used in Program 3-1 has been eliminated. The output of both Programs 3-1 and 3-2 is: It will to fall take
800.00 7.05 seconds
feet.
As used in both programs, the value returned by the sqrt ( ) function is assigned to the variable time. In addition to assigning a function's returned value to a variable, the returned value may be included within a larger expression, or even used as an argument to another function. For example, the expression sqrt (sin (abs (theta) ) )
is valid. Since parentheses are present the computation proceeds from the inner to the outer pairs of parentheses. Thus, the absolute value of theta is computed first and used as an argument to the sin function. The value returned by the sin function is then used as an argument to the sqrt function. Note that the arguments of all trigonometric functions (sin, cos, etc.) must be in radians. Thus, to obtain the sine of an angle that is given in degrees the angle must first be converted to radian measure. This is easily accomplished by multiplying the angle by the term (3.1416/180.). For example, to obtain the sine of 30 degrees, the expression sin ( 3 a * 3. 1416/18 a . ) may be used. Type Conversions We have already seen the conversion of an operand's data type within mixed arithmetic expressions (see Section 2.1). For example, if val is a double precision variable and num is an integer variable, num's value is converted to double precision in the expression val + num. The general rules for converting operands in mixed arithmetic expressions were presented in the previous chapter. A more complete set of conversion rules for arithmetic operators is listed in Table 3-3.
TABLE 3-3
Rule 1.
All character and short integer operands are converted to integer values and all floating point operands are converted to double precision values in arithmetic expressions. If one operand is a double precision value, then the other operand is converted to a double precision value and the result of the expression is a double precision value. If one operand is a long integer value, then the other operand is converted to a long integer value and the resulting value of the expression is a long integervalue. If one operand is an unsigned integer value, then the other operand is converted to an unsigned integer value and the resulting value of the expression is an unsigned value. If both operands are of type int, no conversions occur and the resulting value of the expression is an integer value.
Rule 2.
Rule 3.
Rule 4.
Rule 5.
3.1
103
Data type conversions also take place across assignment operators. Here the value of the expression on the right side of the equal sign is converted to the data type of the variable to the left of the equal sign. For example, consider the evaluation of the expression
a
b * d
where a and b are integer variables and d is a floating point variable. Referring to Rule 1 in Table 3-3, the value of d used in the expression is converted to a double precision number for purposes of computation (it is important to note that the value stored in d remains a floating point number). Since one of the operands is a double precision variable, Rule 2 provides that b's value is converted to a double precision number for the computation (again, the value stored in b remains an integer) and the resulting value of the expression b * d is a double precision number. Finally, since the left side of the assignment operator is an integer variable, the double precision value of the expression (b * d) is truncated to an integer value and stored in the variable a . In addition to data type conversions that are made automatically to operands in mixed arithmetic expressions, C also provides for user-specified type conversions. The operator used to force the conversion of a value to another type is the cast operator. This is a unary operator having the form (data_type), where data type is the desired data type of the operand following the cast. For example, the expression (int) (a * b)
ensures that the value of the expression a * b is converted to an integer value. The parentheses around the expression (a * b) are required because the cast operator has a higher precedence than the multiplication operator. As a last example, consider the expression (int) a * b, where both a and b are double precision variables. Here, only a's value is cast into an integer before multiplication by b. The cast into an integer value causes the fractional part of a's value to be truncated. Since b is a double precision operand, the value of the operand (int) a is converted back to a double precision number (Rule 2 in Table 3-3). The forced conversion back to a double precision number, however, does not restore the fractional part of ~. As before, the value stored in a is not affected and remains a double precision number; it is just the value of a used to evaluate the expression that is truncated.
Exercises 3.1
1. Write function calls to determine: a. The square root of 6.37. b. The square root of x - y. c. The sine of 30 degrees. d. The sine of 60 degrees. e. The absolute value of a2 - b2 f. The value of e raised to the 3rd power.
104
Chapter Three
2. For a
a
b.
c.
d. e.
f.
g.
h. i.
j.
k.
(int) a (int) b (int) c (int) a + b (int) a + b + c (int) (a + b) + c (int) (a + b + c) (float) (int) a + b (float) (int) (a + b) abs(a) + abs(B) sqrt (abs (a - B))
3. Write C statements for the following: a. b=sinx-cosx b. b = sin2x - cos2x c. area = (c * b * sin a)/2 d.c=~ e. p = .)1 m - n I
f.
sum
= ---
a(r" - 1)
r-l
4. Write, compile, and execute a C program that calculates and returns the 4th root of the number 81.0, which is 3. When you have verified that your program works correctly, use it to determine the fourth root of 1,728.896400. Your program should make use of the sqrt ( ) function. 5. Write, compile, and execute a C program that calculates the distance between two points whose coordinates are (7,12) and (3,9). Use the fact that the distance between two points having coordinates (xl,y1) and (x2,y2) is distance = sqrt([xl - x2f + [yl - y2f). When you have verified that your program works correctly by manually calculating the distance between the two points, use your program to determine the distance between the points (-12,-15) and (22,5). 6. If a 20-foot ladder is placed on the side of a building at an 85 degree angle, as illustrated in Figure 3-3, the height at which the ladder touches the building can be calculated as height = 20 * sin 85. Calculate this height by hand and then write, compile, and execute a C program that determines and displays the value of the height. When you have verified that your program works correctly, use it to determine the height of a 25foot ladder placed at an angle of 85 degrees.
FIGURE 3-3
E8
3.1
105
7. The maximum height reached by a ball thrown with an initial velocity v in feet/sec at an angle of IIis given by the formula height = (.5 * v2 * sin2 ll) I 32.2. Using this formula, write, compile, and execute a C program that determines and displays the maximum height reached when the ball is thrown at 5 miles/hour at an angle of 60 degrees. (Hint: Make sure to convert the initial velocity into the correct units.) Calculate the maximum height manually and verify the result produced by your program. After you have verified that your program works correctly, use it to determine the height reached by a ball thrown at 7 miles/hour at an angle of 45 degrees. 8. For small values of x, the value of sin (x) can be approximated by the first three terms of a power series as: x-
-+
x3 6
x5
120
As with the sin function, the value of x must be in radians. Using this power series, write, compile, and execute a C program that approximates the sine of 180/3.1416 degrees, which equals one radian. Additionally, have your program use the sin function to calculate the sine and display both calculated values and the absolute difference of the two results. Manually verify the approximation produced by your program. After you have verified your program is working correctly, use it to approximate the value of the sine of 62.2 degrees. 9. The polar coordinates of a point consist of the distance, r, from a specified origin and an angle, ll, with respect to the X axis. The x and y coordinates of the point are related to its polar coordinates by the formulas
x=rcosll
y=rsinll Using these formulas, write a C program that calculates the x and y coordinates of the point whose polar coordinates are r = 10 and II = 30 degrees. Verify the results produced by your program by calculating the results manually. After you have verified your program is working correctly, use it to convert the polar coordinates r = 12.5 and II = 67.8 into rectangular coordinates. 10. A model of worldwide population in billions of people after 1990 is given by the equation Population = 5.5 e .02 [Year 1990J
Using this formula, write, compile, and execute a C program to estimate worldwide population in the year 1995. Verify the result displayed by your program by calculating the answer manually. After you have verified your program is working correctly, use it to estimate world population in the year 2012. 11. A model to estimate the number of grams of a certain radioactive isotope left after N years is given by the formula Remaining material = (Original material) e
-.00012 N
Using this formula, write, compile, and execute a C program to determine the amount of radioactive material remaining after 1000 years, assuming an initial amount of 100 grams. Verify the display produced by your program using a hand calculation. After you have verified your program is working correctly, use it to determine the amount of radioactive material remaining after 275 years, assuming an initial amount of 250 grams. 12. The number of years that it takes for a certain isotope of uranium to decay to one-half of an original amount is given by the formula
106
Chapter Three
Half-life
= In(2)/k
where k equals .00012. Using this formula, write, compile, and execute a C program that calculates and displays the half-life of this uranium isotope. Verify the result produced by your program using a hand calculation. After you have verified your program is working correctly, use it to determine the half-life of a uranium isotope having k = .00026. 13. The amplification of electronic circuits is measured in units of decibels, which is calculated as 10 log (PO I PI) where PO is the power of the output signal and PI is the power of the input signal. Using this formula, write, compile, and execute a C program that calculates and displays the decibel amplification in which the output power is 50 times the input power. Verify the result displayed by your program using a hand calculation. After you have verified your program is working correctly, use it to determine the amplification of a circuit whose output power is 4.639 times its input power. 14. The loudness of a sound is measured in units of decibels, which is calculated as 10 log (SLI RL) where SL is intensity of the sound being measured and RL is a reference sound intensity level. Using this formula write a C program that calculates and displays the decibel loudness of a busy street having a sound intensity of 10,000,000 RL. Verify the result produced by your program using a hand calculation. After you have verified your program is working correctly, use it to determine the sound level in decibels of the following sounds: a. A whisper of sound at intensity 200 RL b. A rock band playing at a sound intensity of 1,000,000,000,000 RL c. An airplane taking off at a sound intensity of 100,000,000,000,000 RL 15. The dollar change remaining after an amount paid is used to pay a restaurant check of amount check can be calculated using the following C statements:
/* /*
determine the amount of pennies in the change change = (paid - check) * 100; determine the number of dollars in the change dollars = (int) (change/lOO);
*/ */
a. Using the previous statements as a starting point, write a C program that calculates the number of dollar bills, quarters, dimes, nickels, and pennies in the change when $10 is used to pay a bill of $6.07. b. Without compiling or executing your program check the effect, by hand, of each statement in the program and determine what is stored in each variable as each statement is encountered. c. When you have verified that your algorithm works correctly, compile and execute your program. Verify that the result produced by your program is correct. After you have verified your program is working correctly, use it to determine the change when a check of $12.36 is paid using a twenty-dollar bill.
16a. For display purposes the %f conversion control sequence allows the programmer to
round all outputs to the desired number of decimal places. This can, however, yield seemingly incorrect results when used in financial program that require all monetary values to be displayed to the nearest penny. For example, the display produced by the statements
3.2
The scanf
( ) Function
107
float a, b; a = 1.674 b = 1.322 printf("\n%4.2f",a); printf("\n%4.2f",b); printf ( "\n-") ; c = a + b; printf("\n%4.2f",c); is: 1.67 1.32 3.00 Clearly, the sum of the displayed numbers should be 2.99 and not 3.00. The problem is that although the values in a and b have been displayed with two decimal digits, they were added internal to the program as three-digit numbers. The solution is to round the values in a and b before they are added by the statement c = a + b;. Using the (int) cast, devise a method to round the values in the variables a and b to the nearest hundredth (penny value) before they are added. b. Include the method you have devised for Exercise 16a into a working program that produces the following display: 1.67 1.32 2.99
( ) Function
Data for programs that are only going to be executed once may be included directly in the program. For example, if we wanted to multiply the numbers 300.0 and .05, we could use Program 3-3.
fOI\
Program 3-3
#include main( )
{
<stdio.h>
float numl,num2,product; numl = 300.0; num2 = .05; product = numl * num2; printf("%f times %f is if", numl,
num2, product);
108
Chapter Three
Program 3-3 can be shortened, as illustrated in Program 3-4. Both programs, however, suffer from the same basic problem in that they must be rewritten in order to multiply different numbers. Neither program lets the user enter different numbers to be operated on.
11,
Program 3-4
#include main( )
{
<stdio.h>
printf("%f
times
%f is
%f",
300.0,
.05,
300.0*.05);
Except for the practice provided to the programmer of writing, entering, and running the program, programs that do the same calculation only once, on the same set of numbers, are clearly not very useful. After all, it is simpler to use a calculator to multiply two numbers than to enter and run either Program 3-3 or Program 3-4. This section presents the scanf ( ) function, which is used to enter data into a program while it is executing. Just as the printf ( ) function displays a copy of the value stored inside a variable, the scanf ( ) function allows the user to enter a value at the terminal. The value is then stored directly in a variable. Like the printf ( ) function, the scanf ( ) function requires a control string as the first argument inside the function name parentheses. The control string tells the function the type of data being input and uses the same conversion control sequences as the printf ( ) function. Unlike the control string used in a printf ( ) function, however, the control string passed to scanf ( ) typically consits of conversion control sequences only. Also, unlike printf ( ) where a listof variable names can follow the control string, scanf ( ) requires that a list
FIGURE 3-4
main(
{
...--
L~$m:::l;#1r~~--" ~~~~~~() ~;
Keyboard
000
Screen
Illm~mlllllll1/
3.2
109
of variable addresses follow the control string. For the variables we have been using (integer, floating point, double, and character) the variable addresses are obtained by writing an ampersand symbol, &/immediately before the variable' s name. For example, whereas numI, num2, num3 is a list of three variable names, &numI, &num2, &num3is a list of three variable addresses. The ampersand symbol/ &/is C's address operator, which means "the address of." Thus, if numI is a variable name, &numI means "the address of numl/" and the statement scanf ( "%d" / &numl); is a call to the scanf ( ) function.1 The conversion control sequence %d in this statement is identical to the conversion control sequence used in printf ( ) in that it tells the scanf ( ) function that it will be dealing with an integer number, and the address operator &in front of the variable numI, as already noted, is required for scanf ( ) . When a statement such as scanf ( "%d"/ &numI) ; is encountered, the computer stops program execution and continuously scans the keyboard for data (scanf is short for scan function and formatted scan). When a data item is typed, the scanf ( ) function stores the item using the address it was given. The program then continues execution with the next statement after the call to scanf ( ) . To see this, consider Program 3-5.
)01\
Program 3-5
#include main( )
{
<stdio.h>
float
numl,
num2, product;
printf("Please type in a number: "); scanf (" %f" / &numl) ; printf("Please type in another number: "); scanf ( "%f" , &num2); product = numI * num2; printf("%f times %f is %f"/numI, num2,product);
The first call to printf ( ) in Program 3-5 prints a message that tells the person at the terminal what should be typed. When a message is used in this manner it is called a prompt. In this case the prompt tells the user to type a number. The computer then executes the next statement, which is a call to scanf ( ) . The scanf ( ) function puts the computer into a temporary pause (or wait) state for as long as it takes the user to type a value. Then the user signals the scanf ( ) function by pressing the return key after the value has been typed. The entered
1 The interested reader may review Section 2.9, which contains more detailed introductory. material on the address operator. We will encounter the address operator again and be much more specific about its purpose in Chapters 6 and 10.
110
Chapt6r Three
value is stored in the variable whose address was passed to scanf ( ), and the computer is taken out of its paused state. Program execution proceeds with the next statement, which in Program 3-5 is another call to printf ( ) . This call causes the next message to be displayed. The second call to scanf ( ) again puts the computer into a temporary wait state while the user types a second value. This second number is stored in the variable num2 . The following sample run was made using Program 3-5.
Please type in a number: 300. Please type in another number: .05 300.000000 times .050000 is 15.000000
In Program 3-5, each call to scanf ( ) is used to store one value in a variable. The scanf ( ) function, however, can be used to enter and store as many values as there are conversion control sequences in the control string. For example, the statement
scanf (" %f %f", &num1, &num2)
i
results in two values being read from the terminal and assigned to the variables num1 and num2 . If the data entered at the terminal was
0.052 245.79
the variables num1 and num2 would contain the values 0.052 and 245.79, respectively. The space in the control string between the two conversion control sequences, "%f % f " , is strictly for readability. The control string" % f% f" would work equally well. When actually entering numbers such as 0.052 and 245.79, however, you should leave at least one space between the numbers, regardless of which control string, "%f % f" or "% f % f ", is used. The space between the entered numbers, called a delimiter, clearly indicates where one number ends and the next begins. Inserting more than one space between numbers has the same effect on scanf ( ) as inserting a single space. The only time that a space can affect the value being entered is when scanf ( ) is expecting a character data type. For example, the statement scanf ( "%c%c%c" , &ch1, &ch2, &ch3 ) i causes scanf ( ) to store the next three characters typed in the variables ch1, ch2, and ch3, respectively. If you type x y 2, the x is stored in ch1, a blank is stored in ch2, and y is stored in ch3. If, however, the statement scanf ( "%c %c %c", &ch1, &ch2 , &ch3 ) i was used, scanf ( ) looks for three characters, each separated by exactly one space. Any number of scanf ( ) function calls may be made in a program, and any number of values may be input using a single scanf ( ) function. Just be sure that a conversion control sequence is used for each value to be entered and that the address operator is used in front of the variable name where the value is to be stored. Program 3-6 illustrates using the scanf ( ) function to input three numbers from the keyboard. The program then calculates and displays the average of the numbers entered.
3.2
The scanf
( ) Function
111
)01,
Program 3-6
#include main(
{
<stdio.h>
int numl, num2, num3; float average; printf("Enter three integer numbers: "); scanf("%d %d %d", &numl, &num2, &num3); average = (numl + num2 + num3)/3.0; printf("The average of the numbers is %f",
average);
The following sample run was made using Program 3-6: Enter three The average integer of the numbers: 22 56 73 numbers is 50.333333
Note that the data typed at the keyboard for this sample run consists of this input:
22 56 73
In response to this line of input, Program 3-6 stores the value 22 in the variable numl, the value 56 in the variable num2, and the value 73 in the variable num3 (see Figure 3-5). Since the average of three integer numbers can be a floating point number, the variable average, which is used to store the average, is declared as a floating point variable. Note also that the parentheses are needed in the assignment statement average = (numl + num2 + num3) /3 .0; . Without these pllrentheses, the only value that would be divided by three would be the integer in num3 (since division has a higher precedence than addition). The conversion control sequences used in a scanf( ) control string are the same as those used in printf ( ) calls, with one caution. In printing a double precision number using printf ( ), the conversion control sequence for a floating point variable, % f, can be used. This is not true when using scanf ( ) . If a double precision number is to be entered, the conversion control sequence % 1f must be used. The scanf ( ) function, again like the printf ( ) function, does not test the data type of the values being entered. It is up to the user to ensure that all variables are declared correctly and that any numbers entered are of the correct type. However, scanf ( ) is "clever" enough to make a few data type conversions. For example, if an integer is entered in place of a floating point or double precision number, the scanf ( ) function automatically adds a decimal point at the end of the integer before storing the number. Similarly, if a floating point or double pre-
112
numl
~
num2
22
56 num3
.1
scanf("%d %d %d", &numl, &num2, &num3);
73
22 FIGURE 3-5
56
73
cision number is entered when an integer is expected, the scanf ( ) function only uses the integer part of the number. For example, assume the following numbers are typed in response to the function call scanf ("%f %d %f",
&numl,&num2,&num3); : 56 22.879 33.923
scanf ( ) converts the 56 to 56.0 and stores this value in the variable numl. The function continues scanning the input, expecting an integer value. As far as scanf ( ) is concerned, the decimal point after the 22 in the number 22.879 indicates the end of an integer and the start of a decimal number. Thus, the number 22 is stored in num2. Continuing to scan the typed input, scanf ( ) takes the . 879 as the next floating point number and stores this value in num3. As far as scanf ( ) is concerned, 33.923 is extra input and is ignored. If, though, you do not initially type enough data, the scanf ( ) function will continue to make the computer pause until sufficient data has been entered.
Exercises 3.2
1. For the following declaration statements, write a scanf ( ) function call that will cause the computer to pause while the appropriate data is typed by the user. a. int firstnum;
3.2
The scanf
( ) Function
113
b. float grade; c. double secnurn; d. char keyval; e. int month years; float average; f. char Chi int nurn1,nurn2; double grade1,grade2; _ g. float interest, principal, double price,yield; h. char ch, letter1, letter2; int nurn1,nurn2,nurn3; i. float temp1,temp2,temp3; double volts1,volts2;
capital;
2. For the following scanf( ) function calls, write appropriate declaration statements for the variables. a. scanf ("%d", &day); b. scanf ("%c", &fir_char) ; c. scanf ("%f", &grade) ; d. scanf (" %If'' ,&price) ; e. scanf ( "%d %d %c",&nurn1, &nurn2 , &ch1) ; f. scanf ("%f %f %d",&firstnurn, &secnurn,&count); g. scanf("%c %c %d %If'',&ch1,&ch2,&flag,&average); 3. Given the following declaration statements, int nurn1, nurn2; float firstnurn, secnurn; double price, yield; determine and correct the errors in the following scanf ( ) function calls. ' a. scanf ("%d",nurn1) ; b. scanf ("%f %f %f", &nurn1, firstnurn, &price) ; c. scanf ("%c %If %f", &nurn1, &secnurn,&price) ; d. scanf ( "%d %d %If'', nurn1,nurn2,yield) ; e. scanf (&nurn1, &nurn2) ; f. scanf (&nurn1,"%d") ; 4a. Write a C program that first displays the following prompt: Enter the temperature in degrees Celsius:
Have your program accept a value entered from the keyboard and convert the temperature entered to degrees Fahrenheit, using the equation Fahrenheit = (9.0/5.0) * Celsius + 32.0. Your program should then display the temperature in degrees Celsius, using 'an appropriate output message. b. Compile and execute the program written for Exercise 4a. Verify your program by calculating, first manually and then using your program, the Fahrenheit equivalent of the following test data: Test data set 1: a degrees Celsius. Test data set 2: 50 degrees Celsius Test data set 3: 100degrees Celsius
114
Chapter Three
When you are sure your program is working correctly, use it to complete the following table:
Celsius
Fahrenheit
45 50 55
60 65
70
5. Write, compile, and execute a C program that displays the following prompt: Enter the radius of a circle:
After accepting a value for the radius, your program should calculate and display the area of the circle. (Note: area = 3.1416 * radius2.) For testing purposes, verify your program using a test input radius of 3 inches. After manually checking that the result produced by your program is correct, use your program to complete the following table:
2.5
3.0 3.5
6a. Write, compile, and execute a C program that displays the following prompts:
Enter Enter
After each prompt is displayed, your program should use a scanf ( ) statement to accept data from the keyboard for the displayed prompt. After the gallons of gas used has been entered, your program should calculate and display miles per gallon (MPG) obtained. This value should be included in an appropriate message and calculated using the equation miles per gallon = miles / gallons used. Verify your program using the following test data: Test data set 1: Miles = 276, Gas = 10 gallons. Test data set 2: Miles = 20~, Gas = 15.5 gallons. When you have completed your verification, use your program to complete the following table:
3.2
The scanf
Function
115
MPG
b. For the program written for Exercise 6a, determine how many verification runs are required to ensure the program is working correctly and give a reason supporting your answer.
7a. Write, compile, and execute a C program that displays the following prompts: Enter Enter Enter Enter a a a a number: second number: third number: fourth number:
After each prompt is displayed, your program should use a scanf ( ) statement to accept a number from the keyboard for the displayed prompt. After the fourth number has been entered, your program should calculate and display the average of the numbers. The average should be included in an appropriate message. Check the average displayed by your program using the following test data: Testdata set 1: 100, 100, 100, 100 Test data set 2: 100,0,100,
When you have completed your verification, use your program to complete the following table:
Average
b. Repeat Exercise 7a, making sure that you use the same variable name, number, for each number input. Also use the variable sum for the sum of the numbers. (Hint: To do this, you may use the statement sum = sum + number after each number is accepted. Review the material on accumulating presented in Section 2.3.) 8a. Write, compile, and execute a C program that computes and displays the value of the
second-order polynomial a:?- + bx + c for any user input values of the coefficients a, b, c, and the variable x. Have your program first display a message informing the user what the program will do, and then display suitable prompts to alert the user to enter the desired data. (Hint: Use a prompt such as Enter the coefficient of the x
squared term:.)
b. Check the result produced by your program using the following test data: Test Test Test Test Test data data data data data set set set set set 1: a 2: a 3: a 4: a 5: a
116
Chapter Three
When you have completed your verification, use your program to complete the following table:
Polynomial Value
9. The number of bacteria, B, in a certain culture that is subject to refrigeration can be approximated by the equation B = 300,000 e-.032t, where e is the irrational number 2.71828 rounded to five decimal places, known as Euler's number, and t is the time in hours that the culture has been refrigerated. Using this equation, write, compile, and execute a single C program that prompts the user for a value of time, calculates the number of bacteria in ' the culture, and displays the result. For testing purposes, check your program using a test input of 10 hours. When you have verified the operation of your program, use it to determine the number of bacteria in the culture after 12, 18, 24, 36, 48, and 72 hours. 10. Write, compile, and execute a C program that calculates and displays the square root of a user-entered real number. Verify your program by calculating the square roots of the following data: 25,16,0, and 2. When you complete your verification, use your program to determine the square root of 32.25, 42, 48, 55, 63, and 79. 11. Write, compile, and execute a C program that calculates and displays the fourth root of a user-entered number. Recall from elementary algebra that the fourth root of a number can be found by raising the number to the 1/4 power. (Hint: Do not use integer divisioncan you see why?) Verify your program by calculating the fourth root of the following data: 81, 16, 1, and O.When you have completed your verification, use your program to determine the fourth root of 42, 121, 256, 587, 1240, and 16256. 12. For the series circuit shown in Figure 3-{), the voltage drop, V2, across resistor R2 and the power, P2, delivered to this resistor are given by the equations V2 = I R2 and P2 = I
FIGURE ~
-------------------------------------
3.2
The scanf
( ) Function
117
V2, where I = E j(Rl + R2). Using these equations, write, compile, and execute a C program that prompts the user for values of E, Rl, and R2, calculates the voltage drop and power delivered to R2,'and displays the results. Check your program using this test data: E = 10 volts, Rl = 100 ohms, and R2 = 200 ohms. When you have completed your verification, use your program to complete the following table:
13. Write, compile, and execute a C program that computes the combined resistance of three parallel resistors. The values of each resistor should be accepted using a scanf ( ) statement (use the formula for combined resistance given in Exercise 24 of Section 2.3). Verify the operation of your program by using the following test data: Test data set 1: Rl = 1000, R2 = 1000, and R3 = 1000. Test data set 2: Rl = 1000, R2 = 1500, and R3 = 500. When you have completed your verification, use your program to complete the following table:
E (volts) 10 10 10 20 20 20
14. Using scanf ( ) statements, write, compile, and execute a C program that accepts the x and y coordinates of two points. Have your program determine and display the midpoints of the two points (use the formula given in Exercise 26 of Section 2.3). Verify your program using the following test data: . Test data set 1: Point 1 = (0,0) and Point 2 = (16,0) Test data set 2: Point 1 = (0,0) and Point 2 = (0,16) Test data set 3: Point 1 = (0,0) and Point 2 = (-16,0) Test data set 4: Point 1 = (0,0) and Point 2 = (0,-16) Test data set 5: Point 1 = (-5,-5) and Point 2 = (5,5) When you have completed your verification, use your program to complete the following table.
118
Chapter Three
Point 1
(4,6)
Midpoint
15. Write, compile, and execute a C program that calculates and displays the amount of money, A, available in N years when an initial deposit of X dollars is deposited in a bank account paying an annual interest rate of R percent. Use the relationship A = X(1.0 + R/100r The program should prompt the user to enter appropriate values and use scanf ( ) statements to accept the data. In constructing your prompts use statements such as Enter the amount of the initial deposit. Verify the operation of your program by calculating, by hand and using your program, the amount of money available for the following test cases: Test data set 1: $1000 invested for 10 years at 0% interest Test data set 2: $1000 invested for 10 years at 6% interest When you have completed your verification, use your program to determine the amount of money available for the following cases: a. $1000 invested for 10 years at 8% interest. b. $1000 invested for 10 years at 10% interest. c. $1000 invested for 10 years at 12% interest. d. $5000 invested for 15 years at 8% interest. e. $5000 invested for 15 years at 10% interest. f. $5000 invested for 15 years at 12% interest. g. $24 invested for 300 years at 4% interest. 16. Program 3-5 prompts the user to input two numbers, where the first value entered is stored in numl and the second value is stored in nwn2. Using this program as a starting point, write a program that swaps the values stored in the two variables. (Hint: For an appropriate algorithm review the solution to Exercise 3 in Section 1.1) 17. Write a C program that prompts the user to type in an integer number. Have your program accept the number as an integer and immediately display the integer. Run your program three times. The first time you run the program enter a valid integer number, the second time enter a floating point number, and the third time enter a character constant. Using the output display, see what number your program actually accepted from the data you entered. 18. Repeat Exercise 17 but have your program declare the variable used to store the number as a floating point variable. Run the program four times. The first time enter an integer, the second time enter a decimal number with less than six decimal places, the third time enter a number with more than six decimal places, and the fourth time enter a character constant. Using the output display, keep track of what number your program actually accepted from the data you typed in. What happened, if anything, and why? 19. Write a C program that uses the declaration statement double num; . Then use the function call scanf ("%f", &num); to input a value into num. (Notice that we have used the wrong conversion sequence for the variable num.) Run your program and enter a decimal number. Using a printf ( ) function call, have your program display the number stored in numl. Determine what problem you can run into when an incorrect conversion sequence is used in scanf ( ).
20a. Why do you think that most successful applications programs contain extensive data
input validity checks? (Hint: Review Exercises 17, 18, and 19.)
119
What do you think is the differencebetween a data type check and a data reasonableness check? c. Assume that a program requests that the velocity and acceleration of a car be entered by the user. What are some checks that could be made on the data entered?
b.
101\
Program 3-7
#include main( )
{
<stdio.h>
char
fkeYi ")
i
printf ("Type in a character: scanf("%c", &fkey) i printf ("The key just accepted
is
%d", fkey)
. When Program 3-7 is run, the character entered in response to the prompt a character: is stored in the character variable fkey and the decimal code for the character is displayed by the last printf ( ) function call. The following sample run illustrates this:
Type in Type in a character: m The key just accepted is
109
At this point, everything seems to be working just fine, although you might be wondering why we displayed the decimal value of m rather than the character itself. The reason for this will soon become apparent. When you type m, you usually press two keys, the m key and the ENTER key. On most computer systems these two characters are stored in a temporary holding area called a buffer immediately after you press the keys, as illustrated in Figure 3-7.
2 This section contains supplementary material on the scanf ( ) function and can be omitted on first reading without loss of subject continuity.
120
Chapter Three
L8,&*L*v~
Keyboard FIGURE
I
~
(Temporary Storage)
3-7
The first key pressed, m in this case, is taken from the buffer and stored in
fkey. This, however, still leaves the code for the ENTER key in the buffer. Any subsequent call to scanf ( ) for a character input automatically picks up the
code for the ENTER key as the next character. For example, consider Program
3-8.
(1O,
Program 3-8
#include main( )
{
<stdio.h>
char fkey, skey; printf("Type in a character: "); scanf ("%c", &fkey); printf ("The key just accepted is %d", fkey); printf("\nType in another character: ")i scanf ("%c", &skey) i printf ("The key just accepted is %d", skey);
is 10
Let us review what has happened. When you enter m in response to the first prompt, you also press the ENTER key. From a character standpoint this represents the entry of two distinct characters. The first character is m, which is stored as 109. The second character also gets stored in the buffer with the numerical code for the ENTER key. The second call to scanf ( ) picks up this code immediately, without waiting for any additional key to be pressed. The last call to
121
printf ( ) displays the code for this key. The reason for displaying the numeri-
cal code rather than the character itself is because the ENTER key has no printable character associated with it that can be displayed. Remember that every key has a numerical code, including the ENTER, SPACE, ESCAPE, and CONTROL keys. These keys generally have no effect when entering numbers, because scanf ( ) ignores them as leading or trailing input with numerical data. Nor do these keys affect the entry of a single character requested as the first user data to be input, as is the case in Program 3-7. Only when a character is requested after the user has already input some other data, as in Program 3-8, does the usually invisible ENTER key become noticeable. There is a quick solution to avoid having the ENTER key accepted as a legitimate character input. All we have to do is accept the ENTER key, store it as a character variable, and then just not use it. Program 3-9 illustrates this technique. The ENTER key is accepted along with the first character typed. This clears the computer's buffer and prepares the way for the character input.
~Ol\ Program
#include main( )
{
3-9
<stdio.h>
char .fkey, skey; printf("Type in a character: "); scanf("%c%c", &fkey, &skey); /* the enter code goes to skey */ printf ("The key just accepted is %d", fkey); printf("\nType in another character: "); scanf("%c", &skey); /* accept another code */ printf("The key just accepted is %d", skey);
In reviewing Program 3-9, observe that the first scanf ( ) function call accepts two back-to-back characters. Now when the user types m and presses the ENTER key, the m is assigned to fkey and the code for the ENTER key is automatically assigned to skey. The next call to scanf ( ) stores the code for the next key pressed in the variable skey. also. This automatically erases the code for the ENTER key that was previously stored there. From the user's standpoint, the ENTER key has no effect except to signal the end of each character input. The following is a sample run for Program 3-9:
Type in The key Type in The key a character: m just accepted is 109 another character: b just accepted is 98
The solution to the "phantom" ENTER key used in Program 3-9 is not the only
122
Chapter Three
solution possible (there is never just one way of doing something in C).3 All solutions, however, center on the fact that the ENTER key is a legitimate character input and must be treated as such when using a buffered system.
= 2 * 3.1416
* radius;
are also called literals because they are literally included directly in the statement. Additional examples of literals are contained in the following C assignment statements. See if you can identify them.
perimeter
y
salestax
The literals are the numbers 2, 5 and 7 .2, and O. 05 in the first, second, and third statements, respectively. Quite frequently, literal data used within a program have a more general meaning that is recognized outside the context of the program. Examples of these types of constants include the number 3.1416,which is the value of 'IT accurate to four decimal places; 32.2 ft/sec2, which is the gravitational constant (see, for example, Program 3-1); and the number 2.71828, which is Euler's number accurate to five decimal places. The meaning of certain other constants appearing in a program are defined strictly within the context of the application being programmed. For example, in a program to determine bank interest charges, the value of the interest rate takes on a special meaning. Similarly, in determining the weight of various sized objects, the density of the material being used takes on a special significance. Numbers such as these are referred to by programmers as magic numbers. By themselves the numbers are quite ordinary, but in the context of a particular application they have a special ("magical") meaning. Frequently, the same magic number appears repeatedly within the same program. This recurrence of the same constant throughout a program is a potential source of error should the constant have to be changed. For example, if either the interest rate changes or a new material is employed with a different density, the programmer would have the cumbersome task of changing the value of the magic number everywhere it appears in the program. Multiple changes, however, are subject to error: If just one value is overlooked and not changed, the result obtained when the program is run will be incorrect and the source of the error troublesome to find. To avoid the problems of having a magic number spread throughout a program and to permit clear identification of more universal constants, such as 'IT, C allows the programmer to give these constants their own symbolic name. Then,
Another solution, for example, is to replace the last scanf ( ) call in Program 3-9 with the statement scanf (" In%c", &skey);.
3
3.4
Symbolic Constants
123
instead of using the constant throughout the program, the symbolic name is used instead. If the number ever has to be changed, the change need only be made once at the point where the symbolic name is equated to the actual constant value. Equating numbers to symbolic names is accomplished using a preprocessor #define statement. Two such statements are:
#define #define PI 3.1416 NUM_ELS 10
no semicolon no semicolon
These two statements are called either #def ine or equivalence statements. Since they are preprocessor statements they must not be terminated with a semicolon. The first #define statement equates the value 3 .1416 to the symbolic name PI, while the second #define statement equates the number 10 to the symbolic name NUM_ELS. Other terms for symbolic names are named constants or symbolic constants. Although we have typed the symbolic constants in uppercase letters, lowercase letters could have been used. It is common in C, however, to use uppercase letters for symbolic constants. Then, whenever a programmer sees uppercase letters in a program, he or she knows the name is a symbolic constant defined in a #def ine statement, not a variable name declared in a declaration statement. Once a constant has been named, the name can be used in any C statement in place of the number itself. For example, the assignment statement
circum
= 2
* PI * radius;
makes use of the symbolic constant PI. This statement must, of course, appear after PI has been named in a #define statement. Usually #define statements are placed at the top of file before any functions, including main ( ),are typed4 Program 3-10 illustrates the use of a #define statement to calculate the weight of a steel cylinder. The density of the steel is 0.284Ib/in3 Notice in Program 3-10 that each symbolic constant, PI and DENSITY, requires its own #define statement. The following run was made using Program 3-10 to determine the weight of a cylinder having a radius of 3 inches and a height of 12 inches.
Enter the radius Enter the height The cylinder of the cylinder: of the cylinder: 3 12
weights
96.359154
pounds.
The advantage of using the symbolic constant PI in Program 3-10 is that it clearly identifies the value 3 . 1416 in terms recognizable to most people. The advantage of using the symbolic constant DENSITY is that it permits a programmer to change the value of the density for another material without having to search through the program to see where the density is used. If, of course, many different materials are to be considered, the density should be changed from a symbolic constant to a variable. A natural question arises, then, as to the difference between symbolic constants and variables.
4
#define and #include statements may be freely intermixed in any order. Thus, in Program 3-10, the #include statement could have been placed above the #define statement.
124
Chapter Three
}ql,
Program 3-10
#define PI 3.1416 #define DENSITY 0.284 #include <stdio.h> #include <math.h> main( ) /* this program determines the weight of a steel cylinder */ /* by multiplying the volume of the cylinder times its density
{
*/
float radius,
height,
weight;
printf("Enter the radius of the cylinder: "); scanf("%f", &radius); printf("Enter the height of the cylinder: "); scanf("%f", &height); weight = PI * DENSITY * pow(radius,2.0) * height; printf("\nThe cylinder weighs %f pounds.", weight);
The value of a variable can be altered anywhere within a program. By its nature a symbolic constant is a constant value that must not be altered after it is defined. Naming a constant rather than assigning the value to a variable ensures that the value in the constant cannot be subsequently altered. Whenever a symbolic constant appears in an instruction it has the same effect as the constant it represents. Thus, DENSITY in Program 3-10is simply another way of representing the number 0.284. Since DENSITY and the number 0.284 are equivalent, the value of DENSITY may not be subsequently changed within the program. An assignment statement such as
DENSITY
0.156;
is meaningless and will result in an error message, because DENSITY is not a variable. Since DENS ITY is only a stand-in for the value 0 .284, this statement is equivalent to writing the invalid statement 0 .284 = O. 156 . Notice also that #define statements do not end with a semicolon. The reason for this is that #de fine statements are not processed by the regular C compiler used to translate C statements into machine language. The # sign is a signal to the C preprocessor. This preprocessor screens all program statements when a C program is compiled. When the preprocessor encounters a # sign, it recognizes an instruction to itself. The word define tells the preprocessor to equate the symbolic constant in the statement with the information or data following it. In the case of a statement like #define PI 3.1416, the word PI is equated to the value 3 .1416. The preprocessor then replaces each subsequent occurrence of the word PI in the C program with the value 3 .1416. Notice that if a semicolon followed the literal value 3.1416, the preprocessor would equate the word PI with 3 .1416;. Then, when it replaced PI in the assignment statement
3.4
Symbolic Constants
125
weight
= PI * DENSITY
* pow(radius,2.0)
* height;
* DENSITY
* pow(radius,2.0)
* height;
which is the valid statement weight = 3.1416; followed by the invalid statement* DENSITY * pow(radius,2.0) * height;. In addition to using a #define statement to name constants, as in Program 3-10, this statement can also be used to equate any expression or text to a symbolic name. For example, the statement
#define CONVERT 3.1416/180.0
equates the expression 3 .1416 /18 0 .0 to the symbolic name CONVERT. As the expression 3. 1416 /18 0 .0 is required for converting degrees to radians, the symbolic name selected for this conversion factor can be conveniently used whenever such a conversion is required. For example, in the assignment statement
height
= distance
* sin (CONVERT*angle) ;
the symbolic constant CONVERT is used to convert the value in angle to radian measure. A previously defined symbolic constant can also be used in a subsequent #define. For example, the following sequence of preprocessor statements is valid:
#define #define PI 3.1416 CONVERT PI/180.0
Since the constant 3 .1416 has been equated to the symbolic name PI, it can be used legitimately in any subsequent definition. Program 3-11 uses the symbolic
1011
Program 3-11
#define PI 3.1416 #define CONVERT PI/180.0 #include stdio.h #include math.h main( )
{
float angle; printf("Enter the angle (in degrees): "); scanf("%f", &angle); printf("\nThe sine of the angle is If", sin(CONVERT*angle));
126
Chapter Three
constant CONVERT to convert a user-entered angle in degrees into its equivalent radian measure for use by the sin ( ) function. Following is a sample run using Program 3-11:
Enter the angle (in degrees): 30 The sine of the angle is 0~500000
Realizing that #define statements simply relate two items allows us to use them to create individualized programming languages. For example, the #define statements
#define #define BEGIN END }
equate the first brace { to the word BEG IN and the closing brace } to the word END. Once these symbols are equated the words BEG IN and END can be used in place of the respective braces. This is illustrated in Program 3-12.
}Oll
#define #define #define #define #include #include main( ) BEGIN float
Program 3-12
BEGIN { END } PI 3.1416 CONVERT PI/180.0 <stdio.h> <math.h>
angle;
printf("Enter the angle (in degrees): "); scanf("%f", &angle); printf("\nThe sine of the angle is if", sin(CONVERT*angle)); END
When Program 3-12 is compiled, the preprocessor faithfully replaces all occurrences of the words BEG IN and END with their equivalent symbols. Although using #define statements to create a new set of symbols equivalent to . the standard C symbol set usually is not a good idea, Program 3-12 should give you an idea of the richness and diversity that C provides. Generally, the constructions that can be created in C are limited only by the imagination and good sense of the programmer.
Exercises 3;4
1. Modify Program 3-1 to use the symbolic constant GRAV in place of the value 32.2 used in the program. Compile and execute your program to verify that it produces the same result shown in the text.
3.5
Applications
127
2. Rewrite the following program using a #define statement for the constant 3 .1416: #include <stdio.h> #include <math.h> main ( )
{
float radius, circum, area; printf("\nEnter a radius: "); scanf ("%f", &radius); circum = 2.0 * 3.1416 * radius; area = 3.1416 * pow(radius,2.0); printf ("The circumference of the circle is %f", circum); printf("\nThe area of the circle is %f", area);
3. Rewrite the following program so that the variable prime is changed to a symbolic constant: #include <stdio.h> #inc1ude <math.h> main ( )
{
float prime, amount, interest; prime = 0.08; 1* prime interest rate *1 printf("\nEnter the amount: "); scant ("%f". &amount); interest = prime * amount; printf ("\nThe interest earned is %f dollars". interest);
4. Rewrite the following program to use the symbolic constant FACTOR in place of the expression (5. 0/9 .0) used in the program: #include <stdio.h> #include <math.h> main ( )
{
float fahren, celsius; printf("\nEnter a temperature in degrees Fahrenheit: "); scanf ("%f", &fahren); celsius = (5.0/9.0) * (fahren - 32.0); printf("\nThe equivalent Celsius temperature is %f", celsius);
3.5 Applications
In this section we present two applications to further illustrate both the use of scanf ( ) function calls to accept user input data and the use of library functions for performing calculations.
---------------
--
128
Chapter Three
Application 1: Acid Rain The use of coal as the major source of steam power began with the Industrial Revolution. Currently coal is one of the principal sources of generating electrical power in many industrialized countries. Since the middle of the 19th century it has been known that the oxygen used in the burning process combines with the carbon and sulfur in the coal to produce both carbon dioxide and sulfur dioxide. When these gases are released into the atmosphere the sulfur dioxide combines with the water and oxygen in the air to form sulfuric acid, which itself is transformed into separate hydronium ions and sulfates (see Figure 3-8). The hydronium ions in the atmosphere fall to earth either as components of rain or as a dry deposition and change the acidity level of lakes and forests. The acid level of rain and lakes is measured on a pH scale using the formula pH
=-
where the concentration of hydronium ions is measured in units of moles/liter. A pH value of 7 indicates a neutral value (neither acid nor alkaline), while levels below 7 indicate the presence of an acid and levels above 7 indicate the presence of an alkaline substance. For example, sulfuric acid has a pH value of approximately I, lye has a pH value of approximately 13, and water typically has a pH value of 7. Marine life usually cannot survive in water with a pH level below 4. Using the formula for pH, we will write a C program that calculates the pH level of a substance based on a user-input value for the concentration of hydronium ions. Program Development We will use the top-down development procedure described in Chapter 2. Step 1: Determine the Desired Outputs Although the statement of the problem provides technical information on the composition of acid rain, from a programming viewpoint this is a rather simple problem. Here there is only one required output-a pH level. Step 2: Determine the Input Items For this problem there is only one input item, the concentration level of hydronium ions.
FIGURE 3-8
Sulfuric Acid
Acid Rain
Em Em
3.5
Applications
129
Step 3a: Determine an Algorithm The processing required to transform the input to the required output is a rather straightforward use of the pH formula that is provided. The flowchart representation of the complete algorithm for entering the input data, processing the data to produce the desired output, and displaying the output is illustrated in Figure 3-9. The pseudocode representation of the algorithm depicted in Figure 3-9 is:
Display a prompt to enter an ion concentration level Read a value for the concentration level Calculate a pH level using the given formula Display the calculated value
Step 3b: Do a Hand Calculation To ensure that we understand the formula used in the algorithm we will do a hand calculation. The result of this calculation can then be used to verify the result produced by the program. Assuming a hydroni-
FIGURE 3-9
Calculate pH Value
End
130
Chapter Three
urn concentration of .0001 (any value would do), the pH level is calculated as - LoglO 10-4 Either by knowing that the logarithm of 10 raised to a power is the power itself, or by using a log table, the value of this expression is -( -4) = 4. Step 3c: Select Variable Names The last step required before the selected algorithm is described in C is to select variable named for the input and output, and any intermediate variables required by the algorithm. For this problem we will select the variable name hydron for the input variable and the name phlevel for the calculated pH level (any valid variable name could have been selected). Step 4: Write the Program Progra~ 3-13 describes the selected algorithm in C using the variable names chosen inStep 3c.
)01,
Program 3-13
#include #include main( )
{
<stdio.h> <math.h>
float
hydron,
phlevel; ");
printf("Enter the hydronium ion concentration: scanf (."%f", &hydron); phlevel = -loglO(hydron); printf("\nThe pH level is %f", phlevel);
Program 3-13 begins with an #include preprocessor statement, followed by the function main ( ) . Within main ( ), a declaration statement declares two floating point variables, hydron and phlevel. The program then displays a prompt requesting input data from the user. After the prompt is displayed a scanf ( ) function call is used to store the entered data in the variable hydron. Finally, a value for phlevel is calculated, using the logarithmic library function, and displayed. As always, the program is terminated with a closing brace. Step 5: Test the Program A test run using Program 3-13produced the following: Enter the hydronium ion concentration The pH level is 4.000000 level:
0.0001
As the program performs a single calculation, and the result of this test run agrees with our previous hand calculation, the program has been completely tested. It can now be used to calculate the pH level of other hydronium concentrations with confidence that the results being produced are accurate. Application 2: Sine Approximation The sine of an angle x, in radians, can be approximated using the polynomial x3 x5 x--+-6 120
3.5
Applications
131
Using this polynomial as a base, write a program that approximates the sine of a user-entered angle using the first, second, and third terms, respectively, of the approximating polynomial. For each approximation display the value calculated by C's intrinsic sine function, the approximate value, and the absolute difference between the two. Make sure to verify your program using a hand calculation. Once the verification is complete, use the program to approximate the sine of 62.5 degrees.
Program Development
We will use the top-down development procedure described in Chapter 2. Step 1: Determine the Desired Outputs This program requires a total of nine outputs, which we arrive at as follows. The statement of the problem specifies that three approximations are to be made, using one, two, and three terms of the approximating polynomial, respectively. For each approximation to the sine three output values are required: the value of the sine produced by the intrinsic SIN ( ) function, the approximated value, and the absolute difference between the two values. Figure 3-10 illustrates, in symbolic form, the structure of the required output display. The output indicated in Figure 3-10 can be used to get a "feel" for what the program must look like. Because each line in the display can only be produced by executing a printf ( ) function call, clearly three such statements must be executed. Additionally, since each output line contains three computed values, each pr in t f ( ) function call will have three items in its expression list. Step 2: Determine the Input Items The only input to the program consists of the angle whose sine is to be approximated. This will, of course, require a single prompt and a scanf ( ) function call to input the necessary value. Step 3a: Determine an Algorithm Before any output item can be calculated, it will be necessary to have the program prompt the user for an input angle and then have the program accept a user-entered value. As this angle will be in degrees and both the SIN ( ) function and the approximating polynomial require radian measure, the input angle will have to be converted into radians. The actual output display consists of two title lines followed by three lines of calculated data. The title lines can be produced using two printf ( ) function calls. Now let's see how the actual data being displayed is produced. The first item on the first data output line illustrated in Figure 3-10, the sine of the angle, can be obtained using the SIN ( ) function. The second item on this line, the approximation to the sine, can be obtained by using the first term in the polynomial that was given in the program specification. Finally, the third item on the line can be calculated using the f abs ( ) function on the difference between the first two items. When all of these items are calculated a single printf ( ) statement can be used to display the three results on the same line. The second output line illustrated in Figure 3-10 displays the same type of items as the first line, except that the approximation to the sine requires using two terms of the approximating polynomial. Notice also that the first item on the second line, the sine of the angle, is the same as the first item on the first line. This means that this item does not have to be recalculated and the value calculated for the first line can simply be displayed a second time. Once the data for the second line has been calculated a single printf ( ) statement can be used to display the required values. Finally, only the second and third items on the last output line shown in
132
Chapter Three
Sine intrinsic function value intrinsic function value intrinsic function value
Approximation
1st approximate value 2nd approximate value 3rd approximate value
Figure 3-10 need to be recalculated, since the first item on this line is the same as previously calculated for the first line. The second item on the third line is calculated using all three terms of the polynomial and the last item on this line is obtained as the absolute difference between the first two items. Once the data for this last line has been calculated a single printf ( ) statement can be used to display the complete line. Thus, for this problem, the complete algorithm described in pseudocode is:
Display a prompt for the input angle Read an angle in degrees Convert the angle to radian measure Display the heading lines Calculate the sine of the angle Calculate the first approximation Calculate the first difference Print the first output line Calculate the second approximation Calculate the second difference Display the second output line Calculate the third approximation Calculate the third difference Display the third output line.
Step 3b: Do a Hand Calculation To ensure that we understand the processing used in the algorithm we will do a hand calculation. The result of this calculation can then be used to verify the result produced by the program that we write. For test purposes any angle will do and we will arbitrarily select a value of 30 degrees. Converting this angle to radian measure requires multiplying it by the factor 3.1416/180.0, which corresponds to 0.523600 radians. For this test value the following approximations to the sine are obtained: Using the first term of the polynomial the approximation is:
0.523600
Using the first two terms of the polynomial the approximation is:
0.523600 - (0.523600)3 / 6
= .499675
3.5
Applications
133
Using all three terms of the polynomial the approximation is: 0.499675+ (0.523600)5 / 120 = .500003 Notice that in using all three terms of the polynomial it was not necessary to recalculate the value of the first two terms. Instead, we used the value previously calculated in our second approximation. All of the approximations are extremely close to 0.5, which is the actual sine of 30 degrees. Step 3c: Select Variable Names The last step required before the selected algorithm is described in C is to select variable names for the input and output, and any intermediate variables required by the algorithm. For this problem we will select the variable name ang 1e for the input angle entered by the user and the name radian for its equivalent radian measure. The conversion factor 3.1416/180.0 to convert the input angle from degrees into radians will be given the name CONVERT and constructed as a named constant using the #define statement. Finally, the variable names sine, approx, and difrence will be declared for the sine of the angle, the approximation to the sine, and the difference between these two quantities, respectively. As always any valid variable names could have been selected. Step 4: Write the Program Program 3-14 represents a description of the selected algorithm in the C language. In reviewing Program 3-14 notice that the input angle is immediately converted to radians after it is read. The two title lines are printed prior to any calculations being made. The value of the sine is then computed using the intrinsic sine function and assigned to the variable sine. This assignment permits this value to be used in the three difference calculations and displayed three times without the need for recalculation. Since the approximation to the sine is "built up" using more and more terms of the approximating polynomial, only the new term for each approximation is calculated and added to the previous approximation. Finally, to permit the same variables to be used again, the values in them are immediately printed before the next approximation is made. Following is a sample run produced by Program 3-14:
Enter an angle SINE (in degrees): 30.0 APPROXIMATION DIFFERENCE 0.523600 0.499675 0.500003 0.023599 0.000326 0.000002
Step 5: Test the Program The first two columns of output data produced by the sample run agree with our hand calculation (if you are unfamiliar with exponental notation review Section 2.1). A hand check of the last column verifies that it also correctly contains the difference in values between the first two columns. As the program only performs seven calculations and the result of the test run agrees with our hand calculations, the program has been completely tested. It can now be used with other input angles with confidence in the results produced.
134
Chapter Three
,10 I,
Program 3-14
#define CONVERT 3.1416/180.0 #inc1ude <stdio.h> #include <math.h> /* this program approximates the sine of an angle, in degrees */ /* using one, two, and three terms of an approximating polynomial main ( )
{
*/
float angle, radian, sine, approx, difrence; printf("Enter an angle (in degrees): "); scanf("%f", &ang1e); radian = CONVERT * angle; /* convert to radian measure
/* print two title lines printf ("\n SINE printf("\n------------*/
*/
APPROXIMATION ------------_
DIFFERENCE" );
_ II);
= sin(radian); /* use the library function for the sine calculate the first approximation */ approx = radian; difrence = fabs(sine - approx); printf ("\n%10.6f %13.6f %13.6f", sine, approx, difrence); /* calculate the second approximation */ approx = approx - pow(radian,3.0)/6.0; difrence = fabs(sine - approx); printf("\n%10.6f %13.6f %13.6f", sine, approx, difrence); /* calculate the third approximation */ approx = approx + pow(radian,5.0)/120.0; difrence = fabs(sine - approx); printf("\n%10.6f %13.6f %13.6f", sine, approx, difrence);
sine
/*
*/
Additional
1. Enter, compile, and run Program 3-13 on your computer system. 2. Enter, compile, and run Program 3-14 on your computer system. 3. By mistake a student wrote Program 3-14 as follows: #include <math.h> #define CONVERT 3.1416/180.0 /* this program approximates the sine of an angle, in degrees
*/
3.5
Applications
135
*/
float angle, radian, sine, approx, difrence; DIFFERENCE" ); APPROXIMATION printf ("\n SINE _____________ 'I); printf("\n-----------printf ("Enter an angle (in degrees): "); scanf("%f", &angle); radian = CONVERT * angle; /* convert to radian measure */ sine = sin(radian); /* use the library function for the sine */ approx = radian; /* first approximation */ difrence = fabs(sine - approx); %13.6f", sine, approx, printf("\n%10.6f %13.6f difrence) ; /* second approximation */ approx = approx - pow(radian,3)/6.0; difrence = fabs(sine - approx); %13.6f", sine, approx, printf ("\n%10. 6f %13. 6f difrence) ; approx = approx + pow(radian,5)/120.0; /* third approximation */ difrence = fabs(sine - approx); %13.6f", sine, approx, printf("\n%10.6f %13.6f difrence) ;
}
(J'
to a standard deviation.
X-Il z=--
cr
Using this formula, write a program that calculates and displays the value of the standard normal deviate when X = 85.3, Il = 80, and (J' = 4. b. Rewrite the program written in Exercise 4a to accept the values of x, Il, and (J' as user inputs while the program is executing. . 5 a. The equation of the normal (bell-shaped) curve used in statistical applications is:
_ 1
y---e
-[(>~)(x-u)/(J12
cr-fiii.
Using this equation, and assuming Il = 90 and (J' = 4, write a program that determines and displays the value of y when x = 80. b. Rewrite the program written in Exercise Sa to accept the values of x, Il, and (J' as user inputs while the program is executing. 6 a. Write, compile, and execute a program that calculates and displays the gross pay and net pay of two individuals. The first individual works 40 hours and is paid an hourly rate of $8.43. The second individual works 35 hours and is paid an hourly rate of $5.67. Both individuals have 20 percent of their gross pay withheld for income tax purposes and both pay 2 percent of their gross pay, before taxes, for medical benefits.
136
Chapter Three
b. Redo Exercise 6a assuming that the individuals' hours and rate will be entered when the program is run. 7. The volume of oil stored in a underground 200-foot-deep cylindrical tank is determined by measuring the distance from the top of the tank to the surface of the oil. Knowing this distance and the radius of the tank, the volume of oil in the tank can be determined using the formula volume = 'IT radius2 (200 - distance). Using this information, write, compile, and execute a C program that accepts the radius and distance measurements, calculates the volume of oil in the tank, and displays the two input values and the calculated volume. Verify the results of your program by doing a hand calculation using the following test data: radius equals 10 feet and distance equals 12 feet. 8. The perimeter, surface area, and volume of an in-ground pool are given by the following formulas: Perimeter = 2(/ength + width) Volume = length * width * average depth Underground surface area = 2(length + width) average depth + length * width Using these formulas as a basis, write a C program that accepts the length, width, and average depth measurements, and calculates the perimeter, volume, and underground surface area of the pool. In writing your program make the following two calculations immediately after the input data has been entered: length * width and length + width. The results of these two calculations should then be used, as appropriate, in the assignment statements for determining the perimeter, volume, and underground surface area. Verify the results of your program by doing a hand calculation using the following test data: length equals 25 feet, width equals 15 feet, and average depth equals 5.5 feet. When you have verified that your program is working, use it to complete the following table:
Length 25 25 25 25 30 30 30 30
Width 10 10 10 10 12 12 12 12
Perimeter
Volume
3.6
1. Forgetting to assign initial values to all variables before the variables are used in an expression. Initial values can be assigned when the variables are declared, by explicit assignment statements, or by interactively entering values using the scanf ( ) function. 2. Using a library function without #include <math.h>. including the preprocessor statement
3.7
Chapter Summary
137
3. Using a library function without providing the correct number or arguments having the proper data type. 4. Forgetting to pass addresses to scanf ( ) . Since scanf ( ) treats all arguments following the control string as addresses, it is up to the programmer to ensure that addresses are passed correctly. 5. Including a message within the control string passed to scanf ( ) . Unlike print f ( ), scanf ( )'s control string typically contain only conversion control sequences. 6. Not including the correct conversion control sequences in scanf ( ) function calls for the data values that must be entered. 7. Not closing off the control string passed to scanf ( ) with a double quote symbol followed by a comma, and forgetting to separate all arguments passed to scanf ( ) with commas. 8. Terminating #include and #define statements to the preprocessor with a semicolon. By now you probably end every line in your C programs with a semicolon, almost automatically. But there are cases, for example preprocessor commands, where a semicolon should not end a line. 9. Rushing to code and run a program before fully understanding what is required and the algorithms and procedures that will be used to produce the desired result. A symptom of this haste to get a program entered into the computer is the lack of either an outline of the proposed program or a written program itself (see the Enrichment Section at the end of this chapter). Many problems can be caught just by checking a copy of the program, either handwritten or listed from the computer, before it is compiled. 10. Being unwilling to test a program in depth. After all, since you wrote the program you assume it is correct or you would have changed it before it was compiled. It is extremely difficult to back away and honestly test your own software. As a programmer you must constantly remind yourself that just because you think your program is correct does not make it so. Finding errors in your own program is a sobering experience, but one that will help you become a master programmer.
138
Chapter Three
function does, the name of the function, the number and data types of the arguments expected by the function, and the data type of the returned value. 4. Functions may be included within larger expressions. 5. The scanf ( ) function is a standard library function used for data input. scanf ( ) requires a control string and a list of addresses. The general form of this function call is:
scanf(~control string", &argl, &arg2, ... , &argn);
6.
7.
8.
9.
The control string typically contains only conversion control sequences, such as %d,and must contain the same number of conversion control sequences as argument addresses. When a scanf ( ) function call is encountered the computer temporarily suspends further statement execution until sufficient data has been entered for the number of variables contained in the scanf ( ) function. It is good programming practice to display a message, prior to a scanf ( ) function call, that alerts the user as to the type and number of data items to be entered. Such a message is called a prompt. Each compiled C program is automatically passed through a preprocessor. Lines beginning with # in the first column are recognized as commands to this preprocessor. Preprocessor commands are not terminated with a semicolon. Expressions can be made equivalent to a single identifier using the preprocessor define command. This command has the form
#define identifier expression-or-text
it allows the identifier to be used instead of the expression or text anywhere in the program after the command. Generally, a define command is placed at the top of a C program.
3.8
Just as people and products have a life cycle, so do programs. A program's life cycle is divided into three main stages as illustrated in Figure 3-11. These stages consist of program development, program documentation, and program maintenance. The development stage is where a program is initially developed. It is at this stage that requirements must be understood and the structure of the program is planned using the top-down development procedure presented in Section 2.5. The documentation stage, as its name implies, consists of creating, both within the program and in separate documents, sufficient user and programmer support references and explanations. At the maintenance stage the program is modified or enhanced as new demands and requirements are obtained or program errors are detected. Complete courses and textbooks are devoted to each of these three program stages. Our purpose in listing them is to put the actual writing of a program in perspective with the total effort needed to produce professional engineering and scientific software.
3.8
139
Development
Documentation
Maintenance
Time
FIGURE 3-11
The writing of a program in a computer language is formally called coding (informally, of course, it is called programming). And that, after all, is what we have been doing-writing programs in a language, or code, that can be decoded and used by the computer. As we saw in Section 2.5, the coding of a program is but one component in the program's development stage. The total development effort is composed of four distinct phases, as illustrated in Figure 3-12. Listed below are both the steps in the top-down development procedure corresponding to each development phase and the relative amount of effort that is typically expended on each phase in large engineering and scientific programming projects. As can be seen from this listing, the coding phase is not the major effort in overall program development.
Step
FIGURE 3-12
Time
140
Chapter Three
Many new programmers have trouble because they spend the majority of their time coding the program, without spending sufficient time understanding and designing the program. In this regard, it is worthwhile to remember the programming proverb, "It is impossible to write a successful program for a problem or application that is not fully understood." For this reason the analysis phase is one of the most important, because if the requirements are not fully and completely understood before programming begins, the results are almost always disastrous. Once a program structure is created and the program is written, new or reinterpreted requirements often cause havoc. An analogy with house construction is useful to illustrate this point. Imagine designing and building a house without fully understanding the architect's specifications. After the house is completed, the architect tells you that a bathroom is required on the first floor, where you have built a wall between the kitchen and the dining room. In addition, that particular wall is one of the main support walls for the house and contains numerous pipes and electrical cables. In this case, adding one bathroom requires a major modification to the basic structure of the house. Experienced programmers understand the importance of analyzing and understanding a program's requirements before coding, if for no other reason than that they too have constructed programs that later had to be entirely dismantled and redone. The following exercise should give you a sense of this experience. Figure 3-13 illustrates the outlines of six individual shapes from a classic children's puzzle. Assume that as one or more shapes are given, starting with shapes A and B, an easy-to-describe figure must be constructed. Typically, shapes A and B are initially arranged to obtain a square, as illustrated in Figure 3-14. Next, when shape C is considered, it is usually combined with the existing square to form a rectangle, as illustrated in Figure 3-15. Then when pieces D and E are added, they are usually arranged to form another rectangle, which is placed alongside the existing rectangle to form a square, as shown in Figure 3-16. The process of adding new pieces onto the existing structure is identical to constructing a program and then adding to it as each subsequent requirement is understood. The problem arises when the program is almost finished and a requirement is added that does not fit easily into the established pattern. For example, assume that the last shape (shape F) is now to be added (see Figure 3-17). This last piece does not fit into the existing pattern that has been constructed. In order to include this piece with the others, the pattern must be completely dismantled and restructured. Unfortunately, many programmers structure their programs in the same manner used to construct Figure 3-16. Rather than taking the time to understand the complete set of requirements, new programmers frequently start coding based on their understanding of only a small subset of the total requirements. Then, when a subsequent requirement does not fit the existing program structure, the programmer is forced to dismantle and restructure either parts or all of the program. Now, let's approach the problem of creating a figure from another view. If we started by arranging the first set of pieces as a parallelogram, all the pieces could be included in the final figure, as illustrated in Figure 3-18. It is worthwhile observing that the piece that caused us to dismantle the first
3.8
141
GD~/
~F
FIGURE 3-13
FIGURE 3-14
Dj
~
I
FIGURE 3-15 Typical Second Figure
Dj
~ E
I
FIGURE 3-16 Typical Third Figure
142
Chapter Three
/
FIGURE 3-17 The Last Piece
7
D
D:J c:]
I
c
F E
FIGURE 3-18
figure (Figure 3-16) actually sets the pattern for the final figure illustrated in Figure 3-18. This is often the case with programming requirements. The requirement that seems to be the least clear is frequently the one that determines the main interrelationships of the program. Thus, it is essential to include and understand all the known requirements before coding is begun. In practical terms this means doing the analysis and design before any coding is attempted.
Selection
Chapter Four
4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 Relational Expressions The if-else Statement Nested if Statements The swi tch Statement Applications Common Programming Errors Chapter Summary Enrichment Study: A Closer Look at Errors, Testing, and Debugging
143
144
Chapter Four
Selection
The field of programming, as a distinct discipline, is still relatively new. It should not be surprising, then, that many advances have occurred in the theoretical foundations of this field. One of the most important of these advances was the recognition in the late 1960s that any algorithm, no matter how complex, could be constructed using combinations of three standardized flow of control structures: sequential, selection, and repetition. The term flow of control refers to the order in which a program's statements are executed. Unless directed otherwise, the normal flow of control for all programs is sequential. This means that statements are executed in sequence, one after another, in the order in which they are placed in the program. Selection and repetition structures permit the sequential flow of control to be altered in precisely defined ways. As you might have guessed, the selection structure is used to select which statements are to be performed next and the repetition structure is used to repeat a set of statements. In this chapter we present C's selection statements. As selection requires choosing between alternatives, we begin this chapter with a description of C's selection criteria.
4.1
Relational Expressions
Besides providing arithmetic capabilities (addition, subtraction, multiplication, division, etc.) all computers have logical capabilities that permit comparisons of various quantities. Because many seemingly "intelligent" decision-making situations can be reduced to the level of choosing between two values, a computer's comparison capability can be used to create a remarkable intelligence like facility. The expressions used to compare operands are called relational expressions. A simple relational expression consists of a relational operator connecting two variable and/or constant operands, as shown in Figure 4-1. The relational operators available in C are given in Table 4-1. These relational operators may be used with integer, float, double, or character operands, but must be typed exactly as given in Table 4-1. Thus, while the following examples are all valid:
age > 40
3 < 4 day ! = 5
Relational expressions are sometimes called conditions, and we will use both terms to refer to these expressions. Like all C expressions, relational expressions are evaluated to yield a numerical result.1 In the case of relational expressions,
1 In this regard C differs from other high-level programming languages that yeild a Boolean (true, false) result.
4.1
Relational Expressions
145
Operand \
Relational ope~ator
Operand
'-----V---_/
Expression FIGURE
price
<
10.63
the value of the expression can only be an integer value of 1 or 0, which is interpreted as true and false, respectively. A condition that we would interpret as true evaluates to an integer value of 1, and a false condition results in an integer value of O. For example, because the relationship 3 < 4 is always true, this expression has a value of 1, and because the relationship 2.0 > 3.3 is always false, the value of the expression itself is O.This can be verified using the statements printf("The printf("\nThe value of 3 > 4 is %d", 3 > 4); value of 2.0 > 3.0 is %d, 2.0
>
3.3);
which results in the display The value The value of 3 < 4 is 1 of 2.0 > 3.0 is
The value of a relational expression such as hours > 40 depends on the value stored in the variable hours. In a C program, a relational expression's value is not as important as the interpretation C places on the value when the expression is used as part of a selection statement. In these statements, which are presented in the next section, we will see that a zero value is used by C to represent a false condition and any nonzero
TABLE
Relational operator
< > <= >=
age
<
30
<
6.2
<=
20000
== !=
!=
not equal to
146
Chapter Four
Selection
value is used to represent a true condition. The selection of which statement to execute next is then based on the value obtained. In addition to numerical operands, character data can also be compared using relational operators. For example, in the ASCII code the letter 'A' is stored using a code having a lower numerical value than the letter 'B', the code for a 'B' is lower in value than the code for a 'e', and so on. For character sets coded in this manner, the following conditions are evaluated as listed below.
Expression
'A' 'D' 'E' 'G' 'B' > <= >= !=
Value 0 1 0 0 1
'e' 'z'
'F'
'M'
'e'
Comparing letters is essential in alphabetizing names or using characters to select a particular choice in decision-making situations. Logical Operators In addition to using simple relational expressions as conditions, we can create compound conditions using the logical operators AND, OR, and NOT. These operators are represented by the respective symbols: &&
II
When the AND operator, &&, is used with two expressions, the resulting condition is called a logical expression and is true only if both individual expressions are true by themselves. Thus, the logical expression (voltage>
40) && (milliamp
< 10)
is true (has a value of 1) only if vo 1t age is greater than 40 and mi 11 i amp is less than 10. The parentheses surrounding the individual relational expressions are used for clarity only, because the logical && operator has a lower precedence than the relational operators (==, <, >, etc.). The logical OR operator, I I, must also be applied between two expressions, and the resulting condition is also referred to as a logical expression. When using the OR operator, the condition is true if either one or both of the two individual expressions is true. Thus, the compound condition (voltage>
40) I I
(milliamp
< 10)
is true if either vo 1tage is greater than 40, or mi 11 i amp is less than 10, or both conditions are true. Again, the parentheses around the relational expressions are used only for clarity, since the I I operator has a lower precedence than all relational operators.
4.1
Relational Expressions
147
float
Before these conditions can be evaluated, the values of a, b, i, j, and complete must be known. Assuming
a
= 12.0, b = 2.0,
= 15,
= 30, and
complete
= 0.0
Expression
a > b I I a < b I I complete alb > 5 && i <= 20
j
Value
I
==
The NOT operator, !, is used to change any expression to its opposite state. That is, if the expression has any nonzero value (true), !expression produces a zero value (false). If an expression is false to begin with (has a zero value), ! expression is true and evaluates to 1. For example, assuming the number 26 is stored in the variable vol tage, the relational expression vol tage > 40 has a value of zero (it is false), while the expression! (vol tage > 40) has a value of 1 (it is true). Since the NOT operator is a unary operator, it is used with only one expression. The relational and logical operators have a hierarchy of execution similar to the arithmetic operators. Table 4-2 lists the precedence of these operators in relation to the other operators we have used. The following example illustrates the use of an operator's precedence and associativity to evaluate relational expressions, assuming the following declarations:
char key = ' m' ; int i = 5, j = 7, k double x = 22.5;
12;
148
Chapter Four
Selection
Expression i + 2 == k -1 3 * i
- j
Value 0 1 1 + (3*i)) 0 1 0 1 0
--
(k - 1) < 22
< 22
- j)
i + 2 * j > k
(i + (2 * j) ) > k
k + 3 <=
'a'
-j
--
+ 3 * i
'b'
(k + 3 ) <=
( 'a'
( (-j) --
+ 1
+ 1)
'b'
'n'
2"5<= (x + 1.0)
As with all expressions, parentheses can be used to alter the assigned operator priority and improve the readability of relational expressions. By evaluating the expressions within parentheses first, the following compound condition is evaluated as:
(6
3 == 36 / 2 ) (18 == 18) 1
II II II 1 II 1 I I
1
(13
! (6
- 2 < 5) ! (4 < 5) !1 0
Precedence
highest
* 1 + -
&&
II = += *= 1=
right to left
lowest
4.1
Relational Expressions
149
A Numerical Accuracy Problem A subtle numerical accuracy problem relating to floating point and double precision numbers can occur with C's relational expressions. Due to the way computers store these numbers, tests for equality of floating point and double precision values and variables using the relational operator == should be avoided. The reason for this is that many decimal numbers, such as .1, for example, cannot be represented exactly in binary using a finite number of bits. Thus, testing such numbers for exact equality can fail. When equality of non-integer values is desired it is better to require that the absolute value of the difference between operands be less than some extremely small value'. Thus, for floating point and double precision operands the general expression:
where the value 0.000001 can be altered to any other acceptably small value. Thus, if the difference between the two operands is less than 0.000001 (or any other user selected amount), the two operands are considered essentially equal. For example if x and yare floating point variables, a condition such as
x/y == 0.35
should be programmed as
fabs(x/y - 0.35) < 0.000001
This latter condition ensures that slight inaccuracies in representing non-integer numbers in binary do not affect evaluation of the tested condition. Since all computers have an exact binary representation of zero, comparisons for exact equality to zero don't encounter this numerical accuracy problem.
Exercises 4.1
1. Determine the value of the following expressions. Assume a = 5, b = 2, c = 4, d = 6, and e = 3. f. a * b a. a > b
b. a != b c. d % b == c % b d. a * c != d * b e. d * b == c * e g. a h. c
% b % b
i. b %
* c * a c * a
2. Using parentheses, rewrite the following expressions to correctly indicate their order of
4.
c. b % d. b %
* b * c * c *
c && c II a && a II
c c a a
% b % % %
* b * c * c *
a a b b
150
Chapter Four
Selection
3. Write relational expressions to express the following conditions (use variable names of your own choosing): a. a person's age is equal to 30 b. a person's temperature is greater than 98.6 c. a person's height is less than 6 feet d. the current month is 12 (December) e. the letter input is m f. a person's age is equal to 30 and the person is taller than 6 feet g. the current day is the 15th day of the 1st month h. a person is older than 50 or has been employed at the company for at least 5 years i. a person's identification number is less than 500and the person is older than 55 j. a length is greater than 2 feet and less than 3 feet 4. Determine the value of the following expressions, assuming a = 5, b = 2, C = 4, and
d
=
== 5 b. b * d == C * C c. d % b * c > 5 I I c % b * d < 7
5. a. a
4.2
The if-else statement directs the computer to select a sequence of one or more instructions based on the result of a comparison. For example, the state of New Jersey has a two-level state income tax structure. If a person's taxable income is less than $20,000, the applicable state tax rate is 2 percent. For incomes exceeding $20,000, a different rate is applied. The if-else statement can be used in this situation to determine the actual tax based on whether the gross income is less than or equal to $20,000. The general form of the if-else statement is: if (expression) statementl; else statement2;
The expression is evaluated first. If the value of the expression is nonzero, s tat emen t 1 is executed. If the value is zero the statement after the reserved word else is executed. Thus, one of the two statements (either statementl or statement2, but not both) is always executed depending on the value of the expression. Notice that the tested expression must be put in parentheses and a semicolon is placed after each statement. (Do not put a semicolon after the parentheses or the reserved word else-the semicolons go after the ends of the statement.) The flowchart for the if - e 1s e statement is shown in Figure 4-2. As a specific example of an if -else statement, we construct a C program for determining New Jersey income taxes. As previously described, these taxes are assessed at 2 percent of taxable income for incomes less than or equal to $20,000. For taxable income greater than $20,000, state taxes are 2.5 percent of the income that exceeds $20,000 plus a fixed amount of $400. The expression to be tested is
4.2
151
No
Statement
Statement
(else part)
Next statement
FIGURE 4-2
whether taxable income is less than or equal to $20,000. An appropriate if -else statement for this situation is:
if (taxable <= 20000.0) taxes .02 * taxable; else taxes .025 * (taxable
- 20000.0)
+ 400.0;
Recall that the relational operator < = represents the relation "less than or equal to." If the value of taxable is less than or equal to 20000, the condition is true (has a value of 1) and the statement taxes = .02 * taxable; is executed. If the condition is not true, the value of the expression is zero, and the statement after the reserved word else is executed. Program 4-1 illustrates the use of this statement in a complete program.
}Ol,
Program 4-1
#include main( )
{
<stdio.h>
152
ChapterFour Selection
scanf("%f",
&taxable);
if (taxable <= 20000.0) taxes .02 * taxable; else taxes .025 * (taxable - 20000.0) printf("Taxes are $%7.2f",taxes);
+ 400.0;
A blank line was inserted before and after the if-else statement to highlight it in the complete program. We will continue to do this throughout the text to emphasize the statement being presented. To illustrate selection in action, Program 4-1 was run twice with different input data. The results are:
Please type in the taxable Taxes are $ 200.00 income: 10000.
and
Please type in the taxable Taxes are $ 650.00 income: 30000.
Observe that the taxable income input in the first run of the program was less than $20,000, and the tax was correctly calculated as 2 percent of the number entered. In the second run, the taxable income was more than $20,000, and the else part of the if - e 1s e statement was used to yield a correct tax computation of
.025 * ($30,000. - $20,000.)
+ $400. = $650.
Although any expression can be tested by an if-else statement, relational expressions are predominately used. However, statements such as:
if (num) printf("Bingol"); else printf("You lose!");
are valid. Since num, by itself, is a valid expression, the message Bingo! is displayed if num has any nonzero value and the message You lose I is displayed if num has a value of zero. Compound Statements Although only a single statement is permitted in both the if and else parts of the if-else statement, this statement can be a single compound statement. A compound statement is a sequence of single statements contained between braces, as shown in Figure 4-3. .
4.2
153
last statement;
FIGURE 4-3 A Compound Statement Consists of Individual Statements Enclosed Within Braces
The use of braces to enclose a set of individual statements creates a single block of statements, which may be used anywhere in a C program in place of a single statement. The next example illustrates the use of a compound statement within the general form of an i f ~e 1 s e statement.
if (expression)
{
/* as many statements as necessary */ /* can be put within the braces */ /* each statement must end with a ; */
statementn;
11,
{
Program 4-2
#incllide <stdio.h> main( ) char temp_type; float temp, fahren, celsius; printf("Enter the temperature to be converted: "); scanf ("%f ,&temp) ; printf("Enter an f if the temperature is in Fahrenheit"); printf("\n or a c if the temperature is in Celsius: "); scanf("\n%c",&temp_type); /* see footnote on page 124 */
Il
154
if (temp_type
{
==
'f')
is %6.2f",
celsius);
else fahren = (9.0 / 5.0) * temp + 32.0; printf("\nThe equivalent Fahrenheit temperature
is %6.2f",
fahren);
Program 4-2 checks whether the value in temp_type is f. If the value is f, the compound statement corresponding to the if part of the if -else statement is executed. Any other letter results in execution of the compound statement corresponding to the else part. Following is a sample run of Program 4-2.
Enter the temperature to be converted: 212 Enter an f if the temperature is in Fahrenheit or a c if the temperature is in Celsius: f The equivalent Celsius temperature is 100.00
One-Way Selection A useful modification of the if-else statement involves omitting the else part of the statement altogether. In this case, the if statement takes the shortened and frequently useful form:
if (expression) statement;
The statement following the if (expression) is only executed if the expression has a nonzero value (a true condition). As before, the statement may be a compound statement. The flowchart for this statement is illustrated in Figure 4-4. This modified form of the if statement is called a one-way if statement. Program 4-3 uses this statement to selectively display a message for cars that have been driven more than 3000.0 miles. As an illustration of its one-way selection criteria in action, Program 4-3 was run twice, each time with different input data. Only the input data for the first run causes the message Car 256 is over the 1imi t to be displayed.
Please type in car number and mileage: Car 256 is over the limit. End of program output. 256 3562.8
and
Please type in car number End of program output. and mileage: 23 2562.3
4.2
155
No
Statement
>01,
Program 4-3
#define LIMIT 3000.0 #include <stdio.h> main()
{
int id_num; float miles; printf("Please type in car number scanf ( %d %f ", &id_num, &miles);
II
and mileage:
");
if(miles > LIMIT) printf(" Car %d is over the limit.\n",id_num); printf("End of program output.\n");
Caution Two of the most common problems encountered in initially using C's if-else statement are:
156
Chapter Four
Selection
1. Misunderstanding the full implications of what an expression is. 2. Using the assignment operator, =, in place of the relational operator,
==.
Recall that an expression is any combination of operands and operators that yields a result. This definition is extremely broad and more encompassing than may be initially apparent. For example, all of the following are valid C expressions: age age age
+ ==
5 40
= 30
Assuming that the variables are suitably declared, each of the above expressions yields a result. Program 4-4 uses the printf ( ) function to display the. value of these expressions when age = 18.
JP1,
#include main( )
{
Program 4-4
<stdio.h>
int
age
18; value value value of the of the of the first expression second expression third expression is %d", age is %d", age is %d", age
+
5);
= 30);
==
40 );
The display produced by Program 4-4 is: The value The value The value of the of the of the first expression second expression third expression is 23 is 30 is 0
As the output of Program 4-4 illustrates, each expression, by itself, has a value associated with it. The value of the first expression is the sum of the variable age plus 5, which is 23. The value of the second expression is 30, which is also assigned to the variable age. The value of the third expression is zero, since age is not equal to 40, and a false condition is represented in C with a value of zero. If the values in age had been 40, the relational expression a == 40 would be true and would have a value of 1. Now assume that the relational expression age == 40 was intended to be used in the if statement if (age printf
==
4.2
The if-else
Statement
157
40, resulting in
=
(age printf
Since the mistake results in a valid C expression, and any C expression can be tested by an i f statement, the resulting if statement is valid and will cause the message Happy Birthday! to be printed regardless of what value was previously assigned to age. Can you see why? The condition tested by the if statement does not compare the value in age to the number 40, but assigns the number 40 to age. That is, the expression age = 40 is not a relational expression at all, but an assignment expression. At the completion of the assignment the expression itself has a value of 40. Since C treats any nonzero value as true, the call to printf( ) is made. Another way of looking at this is to realize that the if statement is equivalent to the following two statements: age = 40; /* assign 40 to age */ if (age) /* test the value of age */ printf ("Happy Birthday!"); Since a C compiler has no means of knowing that the expression being tested is not the desired one, you must be especially careful when writing conditions.
Exercises 4.2
1. Write appropriate if statements for each of the following conditions: a. If angle is equal to 90 degrees print the message The angle is a right angle, else print the message The angle is not a right angle. b. If the temperature is above 100 degrees display the message above the boiling point of water, else display the message below the boiling point of water. c. If the number is positive add the number to possum, else add the number to negsum. d. If the voltage is less than .5 volts set the variable flag to zero, else set flag to one. e. If the difference between voltsl and volts2 is less than .001,set the variable approx to zero, else calculate approx as the quantity (voltsl - volts2) / 2.0. f. If the frequency is above 60 hertz, display the message Frequency is too high. g. If the difference between templ and temp2 exceeds 2.3 degrees, calculate error as (templ - temp2) * factor. h. If x is greater than y and z is less than 20, read in a value for p. i. If distance is greater than 20 and less than 35, read in a value for time. 2. Write if statements corresponding to the conditions illustrated by each of the following flowcharts.
158
Chapter Four
Selection
a.
No
sum sum + a
count count + 1
b.
volts pwr
5 10
volts pwr
16 25
4.2
The if-else
Statement
159
c.
No
factor
.7
d.
No
average = sum/count
3. Write a C program that prompts the user to type in a voltage. If the entered voltage is greater than 12,the program should print the message Battery is charging, otherwise the program should print the message Battery is discharging. 4. Write a C program that asks the user to input two numbers. If the first number
160
Chapter Four
Selection
entered is greater than the second number the program should print the message The first number is greater, else it should print the message The first number is smaller. Test your program by entering the numbers 5 and 8 and then using the numbers 11 and 2. What do you think your program will display if the two numbers entered are equal? Test this case.
Sa. If money is left in a particular bank for more than two years, the interest rate given
by the bank is 8.5 percent, else the interest rate is 7 percent. Write a C program that uses the scanf ( ) function to accept the number of years into the variable nyrs and display the appropriate interest rate depending on the input value. b. How many runs should you make for the program written in Exercise Sa to verify that it is operating correctly? What data should you input in each of the program runs?
6a. In a pass/fail course, a student passes if the grade is greater than or equal to 70 and
fails if the grade is lower. Write a C program that accepts a grade and prints the message A passing grade or A failing grade, as appropriate. b. How many runs should you make for the program written in Exercise 6a to verify that it is operating correctly? What data should you input in each of the program runs?
7a. Write a C program to compute and display a person's weekly salary as determined
by the following conditions: If the hours worked are less than or equal to 40, the person receives $8.00 per hour; else the person receives $320.00 plus $12.00 for each hour worked over 40 hours. The program should request the hours worked as input and should display the salary as output. b. How many runs should you make for the program written in Exercise 7a to verify that it is operating correctly? What data should you input in each of the program runs? Sa. Write a program that displays either the message I FEEL GREATTODAY! or I FEEL DOWN TODAY#$*! depending on the input. If the character u is entered in the variable code, the first message should be displayed; else the second message should be displayed. b. How many runs should you make for the program written in Exercise 8a to verify that it is operating correctly? What data should you input in each of the program runs?
9a. A senior engineer is paid $1000 a week and a junior engineer $600 a week. Write a C
program that accepts as input an engineer's status in the character variable status. If status equals's', the senior person's salary should be displayed, else the junior person's salary should be output. b.How many runs should you make for the program written in Exercise 9a to verify that it is operating correctly? What data should you input in each of the program runs? 10. Write a C program that accepts a character as input data and determines if the character is an uppercase letter. An uppercase letter is any character that is greater than or equal to 'A' and less than or equal to 'Z'. If the entered character is an uppercase letter, display the message The character just entered is an uppercase letter. If the entered letter is not uppercase, display the message The character just entered is not an uppercase letter. 11. Repeat Exercise 10 to determine if the character entered is a lowercase letter. A lowercase letter is any character greater than or equal to 'a and less than or equal to 'z
0 0
12. The following program displays the message Hello input. Determine where the error is. #include main ( )
{
there!
<stdio.h>
char
letter;
printf ("Enter a letter: "); scanf ("%c", &letter) ; if (letter = 'm') printf("Hello
there!");
4.3
Nested if Statements
161
for statementl
(hours if
< 9)
else printf
("pop")
The braces around the inner one-way if are essential, because in their absence C associates an else with the closest unpaired if. Thus, without the braces, the above statement is equivalent to
if (hours < 9) (distance> 500) printf("snap") i else printf ("pop") i if
Here the else is paired with the inner if, which destroys the meaning of the original if-else statement. Notice also that the indentation is irrelevant as far as the compiler is concerned. Whether the indentation exists or not, the statement is compiled by associating the last else with the closest unpaired if, unless braces are used to alter the default pairing. The process of nesting if statements can be extended indefinitely, so that the print f ( "snap" ) i statement could itself be replaced by either a complete ife 1s e statement or another one-way if statement. Figure 4-5 illustrates the general form of a nested if-else statement when
162
Chapter Four
Selection
No (else part)
No (else part)
Statement
Statement
Statement
FIGURE 4-5a
if-else
Nested within
if
an if-else statement is nested (a) within the if part of an if-else and (b) within the else part of an if-else statement.
The if-else
statement
Chain
In general, the nesting illustrated in Figure 4-Sa tends to be confusing and is best avoided in practice. However, an extremely useful construction occurs for the nesting illustrated in Figure 4-Sb, which has the form:
(expression_l) statementl; else if (expression_2) statement2; else statement3; if
4.3
Nested if Statements
163
No (else part)
Statement
No (else part)
Statement
Statement
FIGURE 4-5b
if-else
As with all C programs, the indentation we have used is not required. Since white space is ignored in C, the above construction can be rewritten using the following arrangement:
if (expression_l) statementl; else if (expression_2) statement2; else statement3;
This form of a nested if statement is extremely useful in practice. It is formally referred to as an if-else chain. Each condition is evaluated in order, and if any condition is true the corresponding statement is executed and the remainder of the chain is terminated. The statement associated with the final else is only
164
Chapter Four
Selection
executed if none of the previous conditions are satisfied. This serves as a default or catch-allcase that is useful for detecting an impossible or error condition. The chain can be continued indefinitely by repeatedly making the last statement another if-else statement. Thus, the general form of an if-else chain is: (expression_i) statementi; else if (expression_2) statement2; else if (expression_3) statement3; if
else if (expression_n) statement_n; else last_statement; Each condition is evaluated in the order it appears in the statement. For the first condition that is true, the corresponding statement is executed and the remainder of the statements in the chain are not executed. Thus, if expression_i is true, only ~tatementi is executed; otherwise expression_2 is tested. If expression_2 is then true, only statement2 is executed; otherwise expression_3 is tested, and so on. The final else in the chainis optional, and last_statement is only executed if none of the previous expressions were true. As a specificexample, consider the following if-else chain: (marcode == 'M') printf("\nlndividual is married. "); else if (marcode == 'S') printf("\nlndividual is single. "); else if (marcode == 'D') printf("\nlndividual is divorced. "); else if (marcode == 'W') printf("\nlndividual is widowed. ") else printf("An invalid code was entered. "); if Execution through this chain begins with the testing of the expression marcode == 'M'. If the value in marcode is an M the message Individual is married is displayed, no further expressions in the chain are evaluated, and execution resumes with the next statement immediately following the chain. If the value in marcode was not an M, the expression marcode == 'S' is tested, and so on, until a true condition is found. If none of the conditions in the chain are true, the message An invalid code was entered would be displayed. In all cases, execution resumes with whatever statement immediately follows the chain.
4.3
Nested if Statements
165
JQ1I
Program 4-5
#include main( )
{
<stdio.h>
if (marcode == 'M') printf("\nlndividual is married."); else if (marcode == 'S') printf("\nlndividual is single."); else if (marcode == 'D') printf("\nlndividual is divorced."); else if (marcode == 'W') printf("\nlndividual is widowed."); else printf ("\nAn invalid code was entered."); printf ("\nThanks' for participating in the survey");
In reviewing Program 4-5 note that the message Thanks for participating in the survey is always printed. This is the statement immediately after the if-else chain to which execution is transferred once the chain completes its execution. Which message is printed within the if-else chain depends on the value entered into marcode. As with all C statements, each individual statement within the if-else chain can be replaced by a compound statement bounded by the braces { and }. As an example requiring a compound statement, consider an if-else statement that calculates a taxi-ride fare. In most cities taxis charge a set amount for the first mile, followed by an additional charge for each additional mile. Assuming that the initial mileage charge is $1.90 and any additional distance is charged at the rate of $1.25 per mile, the following if-else statement, using compound state- . ments, can be used to determine and display the cost of a trip:
if (distance>
{
1.0)
fare = 1.90 + (distance - 1.0) * 1.25; printf("The distance is greater than one mile"); printf("\n and the total fare is $%5.2f.", fare);
}
else
166
Chapter Four
Selection
printf("The distance was under a mile and the standard"); printf("\n fare af $1.90 is charged. ");
As a final example illustrating the if - e 1s e chain, let us calculate the monthly income of a computer salesperson using the following commission schedule:
Monthly Sales greater than or equal to $50,000 less than $50,000 but greater than or equal to $40,000 less than $40,000 but greater than or equal to $30,000 less than $30,000 but greater than or equal to $20,000 less than $20,000 but greater than or equal to $10,000 less than $10,000
$350plus 14% of sales $325plus 12% of sales $300plus 9% of sales $250plus 5% of sales $200plus 3% of sales
The following if-else chain can be used to determine the correct monthly income, where the variable man_sales is used to store the salesperson's current monthly sales: if (man_sales >= 50000.00) incame = 375.00 + .16 * man_sales; else if (man_sales >= 40000.00) incame = 350.00 + .14 * man_sales; else if (man_sales >= 30000.00) incame = 325.00 + .12 * man_sales; else if (man_sales >= 20000.00) incame = 300.00 + .09 * man_sales; else if (man_sales >= 10000.00) incame 250.00 + .05 * man_sales; else incame 200.000 + .03 * man_sales; Notice that this example makes use of the fact that the chain is stopped once a true condition is found. This is accomplished by checking for the highest monthly sales first. If the salesperson's monthly sales is less than $50,000, the if-else
4.3
Nested if Statements
167
chain continues checking for the next highest sales amount until the correct category is obtained. Program 4-6 uses this if - e 1s e chain to calculate and display the income corresponding to the value of monthly sales input to the scanf ( ) function.
9t
Program 4-6
#include main( )
{
a<stdio.h>
float man_sales,
if (man_sales >= 50000.00) income ~ 375.00 + .16 * man_sales; else if (man_sales >= 40000.00) income = 350.00 + .14 * man_sales; else if (man_sales >= 30000.00) income = 325.00 + .12 * man_sales; else if (man_sales >= 20000.00) income = 300.00 + .09 * man_sales; else if (man_sales >= 10000.00) income 250.00 + .05 * man_sales; else 200.00 + .03 * man_sales; income printf("The income is $%7.2f",income);
Exercises 4.3
1. Modify Program 4-5 to accept both lower and uppercase letters as marriage codes. For example, if a user enters either an m or an M, the program should display the message Individual is married. 2. Write nested if statements corresponding to the conditions illustrated in each of the following flowcharts. .
---------------------------
168
Chapter Four
Selection
a.
No
No
bin = 1
4.3
Nested if Statements
169
b.
sum
No
No
fail fail + 1
3. An angle is considered acute if it is less than 90 degrees, obtuse if it is greater than 90 degrees, and a right angle if it is equal to 90 degrees. Using this information write a C program that accepts an angle in degrees and displays the type of angle corresponding to the degrees entered. 4. The grade level of undergraduate college students is typically determined according to the following schedule: Number of Credits Completed less than 32 32 to 63 64 to 95 96 or more Grade Level Freshman Sophomore Junior Senior
170
Chapter Four
Selection
Using this information, write a C program that accepts the number of credits a student has completed, determines the student's grade level, and displays the grade level. 5. A student's letter grade is calculated according to the following schedule:
Numerical Grade greater than or equal to 90 less than 90 but greater than or equal to 80 less than 80 but greater than or equal to 70 less than 70 but greater than or equal to 60 less than 60
Letter Grade A B C
D
Using this information, write a C program that accepts a student's numerical grade, converts the numerical grade to an equivalent letter grade, and displays the letter grade. 6. The interest rate used on funds deposited in a bank is determined by the amount of time the money is left on deposit. For a particular bank, the following schedule is used:
Time on Deposit greater than or equal to 5 years less than 5 years but greater than or equal to 4 years less than 4 years but greater than or equal to 3 years less than 3 years but greater than or equal to 2 years less than 2 years but greater than or equal to 1 year less than 1 year
Using this information, write a C program that accepts the time that funds are left on deposit and displays the interest rate corresponding to the time entered. 7. Write a C program that accepts a number followed by one space and then a letter. If the letter following the number is f, the program is to treat the number entered as a temperature in degrees Fahrenheit, convert the number to the equivalent degrees Celsius, and print a suitable display message. If the letter following the number is c, the program is to treat the number entered as a temperature in Celsius, convert the number to the equivalent degrees Fahrenheit, and print a suitable display message. If the letter is neither f nor c the program is to print a message that the data entered is incorrect and terminate. Use an if-else chain in your program and make use of the conversion formulas:
4.3
Nested if Statements
171
Celsius = (5.0 / 9.0) " (Fahrenheit - 32.0) Fahrenheit = (9.0 / 5.0) "Celsius + 32.0 8. Using the commission schedule from Program 4-6, the following program calculates monthly income: #include <stdio.h> main ( )
{
float man_sales, income; printf("Enter the value of monthly sales: "); scanf ("%f",man_sales); if (man_sales >= 50000.00) income = 375.00 + .16 * man_sales; if (man_sales >= 40000.00 && man_sales income = 350.00 + .14 * man_sales; if (man_sales >= 30000.00 && man_sales income = 325.00 + .12 * man_sales; if (man_sales >= 20000.00 &&'mon_sales income = 300.00 + .09 * man_sales; if (man_sales >= 10000.00 && man_sales income = 250.00 + .05 * man_sales; if (man_sales < 10000.00) income = 200.00 + .03 * mon_sales; printf("The income is $%7.2f",income);
a. Will this program produce the same output as Program 4-6? b: Which program is better and why? 9. The following program was written to produce the same result as Program 4-6: #include <stdio.h> main ( )
{
float man_sales, income; printf("Enter the value of monthly sales: "); scanf ("%f",man_sales) ; if (man_sales < 10000.00) income = 200.00 + .03 * man_sales; else if (man_sales >= 10000.00) income = 250.00 + .05 * man_sales; else if (man_sales >= 20000.00) income = 300.00 + .09 * man_sales; else if (man_sales >= 30000.00) income = 325.00 + .12 * man_sales; else if (man_sales >= 40000.00) income = 350.00 + .14 * man_sales; else if (man_sales >= 50000.00) income = 375.00 + .16 * man_sales; printf("The income is $%7.2f",income);
172
Chapter Four
Selection
a. Willthisprogramrun? b. Whatdoesthisprogramdo?
c. Forwhatvaluesofmonthlysalesdoesthisprogramcalculate thecorrectincome?
4.4
The if-else chain is used in programming applications where one set of instructions must be selected from many possible alternatives. The swi tch statement provides an alternative to the if-else chain for cases that compare the value of an integer expression to a specific value. The general form of a swi tch statement is: switch (expression)
{
/* start
<---
*/
<---terminated
with a colon
breaki
<---
<---terminated
with a colon
/*
end of switch
*/
The swi tch statement uses four new keywords: swi tch, case, default, and break. Let's see what each of these words does. The keyword switch identifies the start of the swi tch statement. The expression in parentheses following this word is evaluated and the result of the expression compared to various alternative values contained within the compound statement. The expression in the switch statement must evaluate to an integer result ora compilation error results.
4.4
173
Internal to the swi tch statement, the keyword case is used to identify or label individual values that are compared to the value of the swi tch expression. The swi tch expression's value is compared to each of these case values in the order that these values are listed until a match is found. When a match occurs, execution begins with the statement immediately following the match. Thus, as illustrated in Figure 4-6, the value of the expression determines where in the swi tch statement execution actually begins. Any number of case labels may be contained within a swi tch statement, in any order. If the value of the expression does not match any of the case values, however, no statement is executed unless the keyword default is encountered. The word default is optional and operates the same as the last else in an ifelse chain. If the value of the expression does not match any of the case values, program execution begins with the statement following the word defaul t. Once an entry point has been located by the switch statement, all further case evaluations are ignored and execution continues through the end of the compound statement unless a break statement is encountered. This is the reason for the break statement, which identifies the end of a particular case and causes an immediate exit from the swi tch statement. Thus, just as the word case identifies possible starting points in the compound statement, the break statement determines terminating points. If the break statements are omitted, all cases following the matching case value, including the default case, are executed.
FIGURE 4-6 The Expression Determines an Entry Point switch
{
(expression)
/* evaluate
expression
*/ .
... ~ case
value_l:
. break;
Start here if -------< expression equals value_3
. case
val ue_ 3 :
break;
break;
Start here if no previous match . default:
/* end of
switch
statement
*/
174
Chapter Four
Selection
When we write a switch statement, we can use multiple case values to refer to the same set of statements; the default label is optional. For example, consider the following: switch (number)
{
case 1: printf("Have break; case 2: printf ("Have break; case 3: case 4: printf("Have
a Good Morning");
If the value stored in the variable number is 1, the message Have a Good Morning is displayed. Similarly, if the value of number is 2, the second message is displayed. Finally, if the value of number is 3 or 4 or 5, the last message is displayed. Since the statement to be executed for these last three cases is the same, the cases for these values can be "stacked together" as shown in the example. Also, since there is no default, no message is printed if the value of number is not one of the listed case values. Although it is good programming practice to list case values in increasing order, this is not required by the swi tch statement. A swi tch statement may have any number of case values, in order; only the values being tested for need be listed. Program 4-7 (see next page) uses a swi tch statement to select the arithmetic operation (addition, multiplication, or division) to be performed on two numbers depending on the value of the variable opselect. Program 4-7 was run twice. The resulting display clearly identifies the case selected. The results are: Please type in Enter a select 1 for 2 for 3 for The product of and Please type in two numbers: 12 3 Enter a select code: 1 for addition 2 for multiplication 3 for division : 3 The first number divided by the second two numbers: 12 3 code: addition multiplication division : 2 the numbers entered
is
36.000
is
4.000
In reviewing Program 4-7 notice the break statement in the last case. Although this break is not necessary, it is a good practice to terminate the last
4.4
175
}Ol,
#include main ( )
{
Program 4-7
<stdio.h>
int opselect; double fnum, snum; printf("Please type in two numbers: "); scanf("%lf %If'', &fnum, &snum); printf ("Enter a select code: "); printf("\n 1 for addition"); printf (" \n 2 for multiplication"); printf (" \n 3 for division : "); scanf ("%d"" , &opselect); switch (opselect)
{
case 1: printf("The sum of the numbers entered is %6.3lf", fnum+snum); break; case 2: printf("The product of the numbers entered is %6.3lf", fnum*snum); break; case 3: printf("The first number divided by the second is %6.3lf", fnum/snum); break; /* this break is optional */ /* end of switch */ }. / * end of main ( ) */
case in a swi tch statement with a break. This prevents a possible program error later, if an additional case is subsequently added to the swi tch statement. With the addition of a new case, the break between cases becomes necessary; having the break in place ensures you will not forget to include it at the time of the modification. Since character data types are always converted to integers in an expression, a swi tch statement can also be used to "switch" based on the value of a character expression. For example, assuming that choice is a character variable, the following swi tch statement is valid: switch(choice)
{
case 'a': case 'e': case 'i': case '0': case 'u': printf("\nThe character in choice in the vowel"); break default: printf("\nThe character in choice is not a vowel"); /* end of switch statement */
176
Chapter Four
Selection
Exercises 4.4
1. Rewrite the following if-else chain using a switch statement: if (let_grad == 'A') printf("The numerical grade is between 90 else if (let_grad == 'B') printf("The numerical grade is between 80 else if (let_grad == 'C') printf("The numerical grade is between 70 else if (let_grad == 'D'); printf("How are you going to explain this else
printf("Of course I had nothing to do with my grade."); printf("\nThe professor was really off the wall.");
==
1)
in_data ( ); check ( );
}
else if (res_typ
{
2)
capacity ( ) devtype( ) else if (res_typ volume ( ); mass ( ); else if (res_typ area ( ); weight ( );
}
3)
4)
else if (res_typ
{
5)
files. ( ); save ( );
}
6)
3. Each disk drive in a shipment of these devices is stamped with a code from 1 through 4, which indicates a drive of the following type:
4.5
Applications
177
Code
1
Disk Drive Type 360kilobyte drive (5 1/2 inch) 1.2megabyte drive (5 1/2 inch) 722kilobyte drive (3 1/4 inch) 1.4megabyte drive (3 1/4 inch)
2 3 4
Write a C program that accepts the code number as an input and, based on the value entered, displays the correct disk drive type. 4. Rewrite Program 4-5 in Section4-3 using a switch statement. 5. Determine why the if-else chain in Program 4-6 cannot be replaced with a switch statement. 6. Repeat Exercise3 in Section4-3using a switch statement instead of an if-else chain. 7. Rewrite Program 4-7 using a character variable for the select code. (Hint: Review Section 3.3if your program does not operate as you think it should.)
4.5 Applications
Two major uses of C's if statements are to select appropriate processing paths and to filter undesirable data from being processed at all. In this section examples of both uses are presorted. Application 1: Solving Quadratic Equations
A quadratic equation is an equation that has the form ar + bx + c = 0 or that can be algebraically manipulated into this form. In this equation x is the unknown variable and a, b, and c are known constants. Although the constants band c can be any numbers, including zero, the value of the constant a cannot be zero (if a is zero, the equation would become a linear equation in x). Examples of quadratic equations are:
r - 7x + 20 = 0
34r
5r
+ 6x + 2 = 0 + 16 = 0
In the first equation a = 5, b = 6, and c = 2; in the second equation a = I, b = -7, and,c = 20; and in the third equation a = 34, b = 0, and c = 16. The real roots of a quadratic equation can be calculated using the quadratic formula as: root 1 = and root 2
= -------~----
2a
-b - ~b2 - 4ac 2a
178
Chapter Four
Selection
A C program that solves for the two roots of a quadratic equation, without any data validation statements, would use the user-entered values of a, b, and c to directly calculate a value for each of the roots. However, if the user entered a value of 0 for a, the division by 2a would result in an error when the program is run. Another error occurs when the value of the term b2 - 4ac, which is called the discriminant, is negative, because the square root of a negative number cannot be taken. The complete logic for correctly determining the roots of a quadratic equation, including the steps necessary to determine that a valid quadratic equation is being processed, is illustrated in Figure 4-7 (see page 180). The pseudocode corresponding to this figure is:
display a program purpose message display a prompt for the coefficients accept user input values for a, b, and c if a = 0 and b = 0 then display a message saying that the equation is degenerate (has no solution) else if a = zero then calculate the single root equal to -clb display the single root else calculate the discriminant if the discriminant> 0 then solve for both roots using the quadratic formula display the two roots else if the discriminant < 0 then display a message that there are no real roots else calculate the repeated root equal to - bl(2a) display the repeated root endif endif
Notice in both the flowchart and the equivalent pseudocode that we have used nested if statements. The outer if statement is used to validate the entered coefficients and determine that we have a valid quadratic equation. The inner if statement is then used to determine if the equation has two real roots (discriminant > 0), two imaginary roots (discriminant <0),or repeated roots (discriminant == 0). The equivalent C code for this problem is listed in Program 4-8. Following are two sample runs of Program 4-8:
This program calculates the roots of a quadratic equation of the form ax2 + bx + c = 0 Please enter values for a, b, and c: 1 2 -35 and -7.000000
and
4.5
Applications
179
1CdI,
Program 4-8
1* this program solves for the roots of a quadratic equation *1 #include <math.h> #include <stdio.h> main ( )
{
double a, b, c, disc, rootl, root2; printf("This program calculates the roots of a"); printf ("\n quadratic equation of the form"); printf("\n 2"); printf("\n ax + bx + c = 0"); printf("\n\nPlease enter values for a, b, and c: "); scanf (''%If%If %If'',&a, &b, &c); if ( a == 0.0 && b == 0.0) printf("\nThe equation is degenerate and has no roots"); else if (a == 0.0) printf ("\nThe equation has the single root x = %If'', -c/b); else disc = pow(b,2.0) - 4 * a * c; if (disc> 0.0)
{
1* calculate discriminant *1
disc = sqrt(disc); rootl = (-b + disc) I (2 * a); root2 = (-b - disc) I (2 * a); printf("\nThe two real roots are %If and %If'',. rootl, root2);
}
else if (disc < 0.0) printf("\nBoth roots are imaginary"); else printf ("\nBoth roots are equal to %If'', -bl (2 * a));
This program calculates the roots quadratic equation of the form ax2 + bx + c = 0 Please The enter values for a, b, and and has
of a
c: 0 0 16 no roots
equation
is degenerate
The first run solves the quadratic equation + 2x - 35 = 0, which has the real roots x = 5 and x = -7. The input data for the second run results in the equation Or + Ox + 16 = O. As this degenerates into the mathematical impossibility 19 = 0, the program correctly identifies this as a degenerate equation.
180
Chapter Four
Selection
No
Calculate discriminant
FIGURE
4.5
Applications
181
Application 2: Data Validation An important use of C's if statements is to validate data by checking for clearly invalid cases. For example, a date such as 5/33/86 contains an obviously invalid day. Similarly, the division of any number by zero within a program, such as 14/0, should not be allowed. Both of these examples illustrate the need for a technique called defensive programming, in which the program includes code to check for improper data before attempting to process it further. The defensive programming technique of checking user-input data for erroneous or unreasonable data is referred to as input data validation. Consider this case: We are to write a C program to calculate the square root and the reciprocal of a user-entered number. Since the square root of a negative number does not exist as a real number and the reciprocal of zero cannot be taken, our program will contain input data validation statements to screen the user-input data to avoid these two cases. The flowchart describing the processing required for our program is shown in Figure 4-8. The pseudocode corresponding to this flowchart logic is:
display a program purpose message display a prompt for a number accept a user input number if the number is negative then print a message that the square root cannot be taken else calculate and display the square root endif if the number is zero then print a message that the reciprocal cannot be taken else calculate and display the reciprocal endif
182
Chapter Four
Selection
Start
No
No
FIGURE 4-8
4.5
Applications
183
Program 4-9
#include #include main( )
{
<math.h> <stdio.h>
double
usenum;
printf("This program calculates the square root and"); printf("\nreciprocal (l/number) of a number"); printf("\n\nPlease enter a number: "); scanf (''%If'', &usenum); if (usenum < 0.0) printf("\nThe square root of a negative number does not exist"); else printf("\nThe square root of %If is %If'', usenum, sqrt(usenum)); if (usenum == 0.0) printf("\nThe reciprocal of zero does not exits"); else printf("\nThe reciprocal of %If is %If'', usenum, l/usenum);
Program 4-9 is a rather straightforward program containing two separate (nonnested) if statements. The first if statement checks for a negative input number; if the number is negative a message indicating that the square root of a negative number cannot be taken is displayed, else the square root is taken. The second i f statement checks if the entered number is zero; if it is, a message indicating that the reciprocal of zero cannot be taken is displayed, else the reciprocal is taken. Following are two sample runs of Program 4-9:
This program calculates the square reciprocal (l/number) of a number Please enter a number: 5 root and
and
This program calculates the square reciprocal (l/number) of a number Please enter a number: -6 root and
The square root of a negative number does not exist The reciprocal of -6.000000 is -0.166667
184
Chapter Four
Selection
a. Write a program that accepts two real numbers and a select code a from user. If the entered select code is 1, have the program add the two previously entered numbers and display the result; if the select code is 2, the numbers should be multiplied, and if the select code is 3, the first number should be divided by the second number. b. Determine what the program written in Exercise 1a does when the entered numbers are 3 and 0, and the select code is 3. c. Modify the program written in Exercise 1a so that division by 0 is not allowed and that an appropriate message is displayed when such a division is attempted.
2 a. Write a program to display the following two prompts: Enter a month (use a 1 for Jan, etc.): Enter a day of the month: Have your program accept and store a number in the variable month in response to the first prompt, and accept and store a number in the variable DAY in response to the second prompt. If the month entered is not between 1 and 12 inclusive, print a message informing the user that an invalid month has been entered. If the day entered is not between 1 and 31, print a message informing the user that an invalid day has been entered. b. What will your program do if the user types a number with a decimal point for the month? How can you ensure that your if statements check for an integer number? c. In a non-leap year February has 28 days, the months January, March, May, July, August, October, and December have 31 days, and all other months have 30 days. Using this information modify the program written in Exercise 2a to display a message when an invalid day is entered for a user-enentered month. For this program ignore leap years. 3 a. The quadrant that a line drawn from the origin resides in is determined by the angle that the line makes with the positive X axis as follows:
Angle from the Positive X Axis Between Between Between Between 0 and 90 degrees 90 and 180 degrees 180 and 270 degrees 270 and 360 degrees
Quadrant
I II
III IV
Using this information, write a C program that accepts the angle of the line as user input and determines and displays the quadrant appropriate to the input data. (Note: If the angle is exactly 0, 90, 180, or 270 degrees the corresponding line does not reside in any quadrant but lies on an axis.) b. Modify the program written for Exercise 3a so that a message is displayed that identifies an angle of zero degrees as the positive X axis, an angle of 90 degrees as the positive Y axis, an angle of 180 degrees as the negative X axis, and an angle of 270 degrees as the negative Yaxis. 4 a. All years that are evenly divisible by 400 or are evenly divisible by four and not evenly divisible by 100 are leap years. For example, since 1600 is evenly divisible by 400, the year 1600 was a leap year. Similarly, since 1988 is evenly divisible by four but not by 100, the year 1988 was also a leap year. Using this information, write a C program that accepts the year as a user input, determines if the year is a leap year, and displays an appropriate message that tells the user if the entered year is or is not a leap year.
4.6
185
b. Using the code written in Exercise 4a redo Exercise 2f to take leap years into account. 5. Based on an automobile's model year and weight the state of New Jersey determines the car's weight class and registration fee using the following schedule:
Model Year
Weight
Weight Class 1 2 3 4 5 6 7 8
Registration Fee $16.50 25.50 46.50 27.00 30.50 52.50 19.50 52.50
1970 or earlier
less than 2,700 lbs 2,700 to 3,800 lbs more than 3,800 lbs less than 2,700 lbs 2,700 to 3,800 lbs more than 3,800 lbs less than 3,500 lbs 3,500 or more lbs
1971 to 1979
1980 or later
Using this information write a C program that accepts the year and weight of an automobile and determines and displays the weight class and registration fee for the car. 6. Modify Program 4-8 so that the imaginary roots are calculated and displayed when the discriminant is negative. For this case the two roots of the equation are
XI
=-+-~---~l 2a 2a
-b
sqrt(-(b
2 -
4ac)] .
and x2 =
-+-~---~l
-b
sqrt(-(b
4ac)] .
.
2a
2a
where i is the imaginary number symbol for the square root of -1. (Hint: Calculate the real and imaginary parts of each root separately.) 7. In the game of blackjack the cards 2 through 10 are counted at their face values regardless of suit, all face cards (jack, queen, and king) are counted as 10, and an ace is counted as either a 1 or an 11, depending on the total count of all the cards in a player's hand. The ace is counted as 11 only if the resulting total value of all cards in a player's hand does not exceed 21, else it is counted as a 1. Using this information write a C program that accepts three card values as inputs (a 1 corresponding to an ace, a 2 corresponding to a 2, and so on), calculates the total value of the hand appropriately, and displays the value of the three cards with a printed message.
,
There are three programming errors common to C's selection statements. 1. Using the assignment operator, =, in place of the relational operator, ==. This can cause an enormous amount of frustration because any expression can be tested by an if - e 1 s e statement. For example, the statement
186
Chapter Four
Selection
if else
always results in the message Happy Birthday being printed, regardless of the initial value in the variable opselect. The reason for this is that the assignment expression opselect = 2 has a value of 2, which is considered a true value in C. The correct expression to determine the value in opselect is opselect
== 2.
2. The second error presents a typical debugging problem. Here an if - e 1s e statement appears to select an incorrect choice and the programmer mistakenly concentrates on the tested condition as the source of the problem. For example, assume that the following if - e 1s e statement is part of your program: if
{
(key
==
'F')
-32.0)
j
was done")j
else contemp = (9.0/5.0) * intemp + 32.0j printf("Conversion to Fahrenheit was done")j This statement will always display Conversion to Celsius was done when the variable key contains an F. Therefore, if this message is displayed when you believe key does not contain F, investigation of key's value is called for. As a general rule, whenever a selection statement does not act as you think it should, make sure to test your assumptions about the values assigned to the tested variables by displaying their values. If an unanticipated value is displayed, you have at least isolated the source of the problem to the variables themselves, rather than the structure of the if - e 1s e statement. Then you will have to determine where and how the incorrect value was obtained. 3. The third error occurs when nested if statements are used and braces are not included to clearly indicate the desired structure. Without braces the compiler defaults to pairing elses with the closest unpaired ifs, which sometimes destroys the original intent of the selection statement. To avoid this problem and to create code that is readily adaptable to change it is useful to write all if - e 1s e statements as compound statements in the form if
{
4.7
Chapter Summary
187
By using this form, no matter how many statements are added later, the original integrity and intent of the if statement is maintained.
4.7
Chapter Summary
1. Relational expressions, which are also called simple conditions, are used to com-pare operands. If a relational expression is true, the value of the expression is the integer 1. If the relational expression is false, it has an integer value of O.Relational expressions are created using the following relational operators:
Meaning
Example
less than greater than less than or equal to greater than or equal to equal to not equal to
age < 30 height> 6.2 taxable <= 20000 temp >=98.6 grade == 100 number !=250
2. More complex conditions can be constructed from relational expressions using C's logical operators, && (AND), I I (OR), and! (NOT). 3. if - e 1s e statements are used to select between two alternative statements based on the value of an expression. Although relational expressions are usually used for the tested expression, any valid expression can be used. In testing an expression, if-else statements interpret a nonzero value as true and a zero value as false. The general form of an i f - e 1s e statementis:
if (expression) statementI; else statement2;
This is a two-way selection statement. If the expression has a nonzero value it is considered as true, and statementl is executed; otherwise statement2 is executed .. 4. if - e 1 s e statements can contain other if - e 1 s e statements. In the absence of braces, each e 1s e is associated with the closest unpaired if. 5. The if - e 1s e chain is a multiway selection statement having the general form:
if (expression_I) statement_I; else if (expression_2) statement_2;
188
Chapter Four
Selection
Each expression is evaluated in the order it appears in the chain. Once an expression is true (has a nonzero value), only the statement between that expression and the next else if or else is executed, and no further expressions are tested. The final else is optional, and the statement corresponding to the final e 1s e is only executed if none of the previous expressions were true. 6. A compound statement consists of any number of individual statements enclosed within the brace pair { and }. Compound statements are treated as a single unit and can be used anywhere a single statement is called for. 7. The swi tch statement is a multiway selection statement. The general form of a swi tch statement is: switch(expression)
{
/*
*/
<---terminated
with a colon
breaki
<---terminated
with a colon
<---terminated
with a colon
/*
end of switch
*/
4.8
189
For this statement the value of an integer expression is compared to a number of integer or character constants or constant expressions. Program execution is transferred to the first matching case and continues through the end of the swi tch statement unless an optional break statement is encountered. cases in a switch statement can appear in any order and an optional default case can be included. The defaul t case is executed if none of the other cases is matched.
printf("There are four syntax errors here\n") printf(" Can you find tern);
contains four syntax errors. These errors are: 1. 2. 3. 4. The relational operator in the first line is incorrect and should be the symbol <. The closing parenthesis is missing in line 1. The third line is missing the terminating semicolon (;). The string within parentheses in the fourth line is not terminated with quotes.
190
Chapter Four
Selection
All of these errors will be detected by the compiler when the program is compiled. This is true of all syntax errors-since they violate the basic rules of C, if they are not discovered by desk checking, the compiler will detect them and display an error message indicating that a syntax error exists.2 In some cases the error message is extremely clear and the error is obvious; in other cases it takes a little detective work to understand the error message displayed by the compiler. Since all syntax errors are detected at compile time, the terms compile-time and syntax errors are frequently used interchangeably. Strictly speaking, however, compile-time refers to when the error was detected and syntax refers to the type of error detected. Note that the misspelling of the word tem in the second printf ( ) function call is not a syntax error. Although this spelling error will result in an undesirable output line being displayed, it is not a violation of C's syntactical rules. This spelling error is a rather simple example of a logic error. Logic errors are characterized by erroneous, unexpected, or unintentional errors that are a direct result of some flaw in the program's logic. These errors, which are never caught by the compiler, may be detected either by desk checking, by program testing, by accident when a user obtains an obviously erroneous output, while the program is executing, or not at all. If the error is detected while the program is executing a run-time error occurs that results in an error message being generated and/ or abnormal and premature program termination. Since logic errors may not be detected by the compiler, they are always more difficult to detect than syntax errors. Logic errors typically cause one or more of the following to occur: No output. This is caused either by an omission of a print f ( ) statement or a sequence of statements that inadvertentlybypasses a print f ( ) function call. Unappealing or misaligned output. This is always caused by an error in a printf ( ) statement. Incorrect numerical results. This is always caused by incorrect values assigned to the variables used in an expression, the use of an incorrect arithmetic expression, an omission of a statement, roundoff error, or the use of an improper sequence of statements. This type of error may also cause a run-time error. See if you can detect the logic error in Program 4-10. Following is a sample run of Program 4-10: This program calculates the amount of money in a bank account for an initial deposit invested for n years at an interest rate r. Enter Enter the the initial interest amount amount in the account: 1000. rate (ex. 5 for 5%): 5 of money is
$ 1000.00
The final
2
They may not, however, all the detected at the same time. Frequently, one syntax error "masks" another error and the second error is only detected after the first error is corrected.
4.8
191
,101,
Program 4-10
#include #include main( )
{
program nyrs;
*/
float
printf("This program calculates the amount of money\n"); printf("in a bank account for an initial deposit\n"); printf("invested for n years at an interest rate r.\n\n"); printf("Enter the initial amount in the account: "); scanf("%f", &amount);
printf("Enter the interest rate (ex. 5.0 for 5.0%):
II);
scanf("%f", &rate); capital = amount * pow( (1 + rate/100.), printf("\nThe final amount of money is
As indicated in the output, the final amount of money is identical to the initial amount input. Did you spot the error in Program 4-10 that produced this apparently erroneous output? Unlike a misspelled output message, the error in Program 4-10 causes a mistake in a computation. Here the error is that the program does not initialize the variable nyears before this variable is used in the calculation of capital. When the assignment statement that calculates capi tal is executed the computer uses whatever value is stored in nyears. On those systems that initialize all variables to zero, the value 0 will be used for nyears. However, on those systems that do not initialize all variables to zero, whatever "garbage" value that happens to occupy the storage locations corresponding to the variable nyears will be used (the manuals supplied with your compiler will indicate which of these two actions your compiler takes). In either case an error is produced. Although the logic error in this example program did not cause premature program termination, faulty or incomplete program logic can cause run-time errors. Examples of this type of logic error are attempts to divide by zero or take the square root of a negative number. Testing and Debugging In theory, a comprehensive set of test runs would reveal all logic errors and ensure that a program will work correctly for any and all combinations of input and computed data. In practice this requires checking all possible combinations of statement execution. Due to the time and effort required, this is an impossible goal except for extremely simple programs. Let us see why this is so. Consider Program 4-11.
192
Chapter Four
Selection
,lQI,
Program 4-11
main(
{
int
num;
Program 4-11 has two paths that can be traversed from when the program is run to when the program reaches its closing brace. The first path, which is executed when the input number is 5, is in this sequence:
printf ("Enter a number"); scanf (" %d", &num); printf("Bingo!") ;
The second path, which is executed whenever any number except 5 is input, includes this sequence of instructions:
printf ("Enter a number"); scanf (" %d", &num); printf("Bongo!");
To test each possible path through Program 4-11 requires two runs of the program, with a judicious selection of test input data to ensure that both paths of the if statement are exercised. The addition of one more if statement in the program increases the number of possible execution paths by a factor of 2 and requires four (22) runs of the program for complete testing. Similarly, two additional if statements increase the number of paths by a factor of 4 and requires eight (23) runs for complete testing and three additional if statements would produce a program that required sixteen (24) test runs. Now consider a modestly sized application program consisting of only 10 functions, each function containing five if statements. Assuming the functions are always called in the same sequence, there are 32 possible paths through each function (2 raised to the fifth power) and more than 1,000,000,000,000,000(2 raised to the fiftieth power) possible paths through the complete program (all functions executed in sequence). The time needed to create individual test data to exercise each path and the actual computer run time required to check each path make the complete testing of such a program impossible to achieve. The inability to fully test all combinations of statement execution sequences
4.8
193
has led to the programming proverb, "There is no error-free program." It has also led to the realization that any testing that is done should be well thought out to maximize the possibility of locating errors. An important corollary to this is the realization that although a single test can reveal the presence of an error, it does not verify the absence of one. The fact that one error is revealed by testing does not indicate that another error is not lurking somewhere else in the program; the fact that one test revealed no errors does not indicate that there are no errors. Once an error is discovered, however, the programmer must locate where the error occurs, and then fix it. In computer jargon, a program error is referred to as a "bug" and the process of isolating, correcting, and verifying the correction is called "debugging." Although there are no hard and fast rules for isolating the cause of an error, some useful techniques can be applied. The first is a preventive technique. Frequently many errors are simply introduced by the programmer in the rush to code and run a program before fully understanding what is required and how the result is to be achieved. A symptom of this haste to get a program entered into the computer is the lack of an outline of the proposed program (pseudocode or flowcharts) or a handwritten program itself. Many errors can be eliminated simply by desk checking a copy of the program before it is ever entered or compiled. A second useful technique is to mimic the computer and execute each statement, by hand, as the computer would. This means writing down each variable as it is encountered in the program and listing the value that should be stored in the variable as each input and assignment statement is encountered. Doing this also sharpens your programming skills, because it requires that you fully understand what each statement in your program causes to happen. Such a check is called program tracing. A third and very powerful debugging technique is to use one or more diagnostic print f ( ) function calls to display the values of selected variables. For example, consider Program 4-10 again. Since this program produced an incorrect value for capi tal, it is worthwhile placing a print f ( ) statement immediately before the assignment statement for capi tal to display the value of all variables used in the computation. If the displayed values are correct, then the problem is in the assignment statement; if the values are incorrect, we must determine where the incorrect values were actually obtained. Another use of printf ( ) function calls in debugging is to immediately display the values of all input data. This technique is referred to as echo printing, and is useful in establishing that the computer is correctly receiving and interpreting the input data. Finally, no discussion of debugging is complete without mentioning the primary ingredient needed for successful isolation and correction of errors: the attitude and spirit you bring to the task. Since you wrote the program your natural assumption is that it is correct or you would have changed it before it was compiled. It is extremely difficult to back away and honestly test and find errors in your own software. As a programmer you must constantly remind yourself that just because you think your program is correct does not make it so. Finding errors in your own programs is a sobering experience, but one that will help you become a master programmer. It can also be exciting and fun if approached as a detection problem with you as the master detective.
Repetition
Chapter Five
5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.8 The while Statement scanf ( ) Within a while Loop The for Statement for Loop Programming Applications Nested Loops The do Statement Common Programming Errors Chapter Summary
195
The programs examined so far have been useful in illustrating the correct structure of C programs and in introducing fundamental C input, output, assignment, and selection capabilities. By this time you should have gained enough experience to be comfortable with the concepts and mechanics of the C programming process. It is now time to move up a level in our knowledge and abilities. The real power of most computer programs resides in their ability to repeat the same calculation or sequence of instructions many times over, each time using different data, without the necessity of rerunning the program for each new set of data values. In this chapter we explore the C statements that permit this. These statements are the while, for, and do-while statements.
The expression contained within the parentheses is evaluated in exactly the same manner as an expression contained in an if-else statement; the difference is how the expression is used. As we have seen, when the expression is true (has a nonzero value) in an if - e 1s e statement, the statement following the expression is executed once. In a while statement the statement following the expression is executed repeatedly as long as the expression retains a nonzero value. This naturally means that somewhere in the while statement there must be a statement that alters the value of the tested expression. As we will see, this is indeed the case. For now, however, considering just the expression and the statement following the parentheses, the process used by the computer in evaluating a while statement is: 1. test the expression 2. if the expression has a nonzero (true) value a. execute the statement following the parentheses b. go back to step 1
else
exit the whi 1e statement Notice that step 2b forces program control to be transferred back to step 1.The transfer of control back to the start of a whi 1e statement in order to reevaluate the expression is an example of a program loop. The while statement literally loops back on itself to recheck the expression until it evaluates to zero (becomes false). This looping process is illustrated in Figure 5-1. A diamond shape is used to show the two entry and two exit points required in the decision part of the while statement.
196
.Exitthe while statement (a false condition) Expression evaluates to a nonzero number (a true condition) Execute the statement after the parentheses (step 2a)
To make this a little more tangible, consider the relational expression count 10 and the statement printf ( "%d ", count) ;. Using these, we can write the following valid whi 1e statement:
<=
<=
10) ",count);
Although the above statement is valid, the alert reader will realize that we have created a situation in which the printf ( ) function either is called forever (or until we stop the program) or is not called at all. Let us see why this happens. If count has a value less than or equal to 10 when the expression is first evaluated, a call to printf ( ) is made. The while statement then automatically loops back on itself and retests the expression. Since we have not changed the value stored in count, the expression is still true and another call to printf ( ) is made. This process continues forever, or until the program containing this statement is prematurely stopped by the user. However, if count starts with a value greater than 10, the expression is false to begin with and the print f ( ) function call is never made. How do we set an initial value in count to control what the while statement does the first time the expression is evaluated? The answer, of course, is to assign
197
values to each variable in the tested expression before the while statement is encountered. For example, the following sequence of instructions is valid:
count = 1; while (count printf("%d
<= 10)
",count);
Using this sequence of instructions, we have ensured that count starts with a value of 1. We could assign any value to count in the assignment statementthe important thing is to assign some value. In practice, the assigned value depends on the application. We must still change the value of count so that we can finally exit the while statement. To do this requires an expression such as count = count + 1 to increment the value of count each time the while statement is executed. The fact that a while statement provides for the repetition of a single statement does not prevent us from including an additional statement to change the value of count. All we have to do is replace the single statement with a compound statement. For example:
count while 1; (count
/* initialize <=
count
*/
10)
/* increment
count
*/
Note that, for clarity, we have placed each statement in the compound statement on a different line. This is consistent with the convention adopted for compound statements in the last chapter. Let us now analyze the above sequence of instructions. The first assignment statement sets count equal to 1. The while statement is then entered and the expression is evaluated for the first time. Since the value of count is less than or equal to 10, the expression is true and the compound statement is executed. The first statement in the compound statement is a call to the printf ( ) function to display the value of count. The next statement adds 1 to the value currently stored in count, making this value equal to 2. The while statement now loops back to retest the expression. Since count is still less than or equal to 10, the compound statement is again executed. This process continues until the value of count reaches 11. Program 5-1 illustrates these statements in an actual program. The output for Program 5-1 is:
1 2 3 4 5 6 7 8 9 10
There is nothing special about the name count used in Program 5-1. Any valid integer variable could have been used. Before we consider other examples of the while statement two comments concerning Program 5-1 are in order. First, the statement count = count + 1 can be replaced with any statement that changes the value of count. A statement such as count = count + 2, for example, would cause every second integer
198
Chapter Five
Repetition
,[QI,
Program 5~ 1
#include main( )
{
<stdio.h>
int
count; = 1; (count
/* initialize
count while
count
*/
<= 10)
/* add 1 to
count
*/
to be displayed. Second, it is the programmer's responsibility to ensure that count is changed in a way that ultimately leads to a normal exit from the while. For example, if we replace the expression count = count + 1 with the expression count = count - 1, the value of count will never exceed 10 and an infinite loop will be created. An infinite loop is a loop that never ends. The computer will not reach out, touch you, and say, "Excuse me, you have created an infinite loop." It just keeps displaying numbers until you realize that the program is not working as you expected. Now that you have some familiarity with the while statement, see if you can read and determine the output of Program 5-2.
,1ql,
Program 5-2
#include main( )
{
<stdio.h>
int
i;
i = 10;
while
{
(i >= 1)
The assignment statement in Program 5-2 initially sets the int variable i to 10. The while statement then checks to see if the value of i is greater than or
199
equal to 1. While the expression is true, the value of i is displayed by the call to printf ( ) and the value of i is decremented by 1.When i finally reaches zero, the expression is false and the program exits the whi 1 e statement. Thus, the following display is obtained when Program 5-2 is run:
10 9 8 7 6 5 4 3 2 1
To illustrate the power of the while statement, consider the task of printing a table of numbers from 1 to 10 with their squares and cubes. This can be done with a simple while statement as illustrated by Program 5-3.
}Oll
Program 5-3
#include main(
{
<stdio.h>
SQUARE ------
CUBE\n") ;
----\n");
%3d
%4d\n",
num, num*num,
SQUARE
------
1 2 3 4 5 6 7 8 9 10
1 4 9 16 25 36 49 64 81 100
Note that the expression used in Program 5-3 is num < 11. For the integer variable num, this expression is exactly equivalent to the expression num <= 10. The choice of which to use is entirely up to you.
200
Chapter Five
Repetition
If we want to use Program 5-3 to produce a table of 1000 numbers, all we do is change the expression in the while statement from i < 11 to i < 100l. Changing the 11 to 1001 produces a table of 1000 lines-not bad for a simple five-line while statement. All the program examples illustrating the whi 1e statement have checked for a fixed-count condition. Since any valid expression can be evaluated by a while statement we are not restricted to constructing such loops. For example, consider the task of producing a Celsius to Fahrenheit temperature conversion table. Assume that Fahrenheit temperatures corresponding to Celsius temperatures ranging from 5 to 50 degrees are to be displayed in increments of five degrees. The desired display can be obtained with this series of statements:
celsius = 5; /* starting while (celsius <= 50)
{
Celsius
value
*/
As before, the while statement consists of everything from the word while through the closing brace of the compound statement. Prior to entering the while loop we have made sure to assign a value to the operand being evaluated, and there is a statement to alter the value of celsius to ensure an exit from the whi 1e loop. Program 5-4 illustrates the use of this code in a complete program.
lql,
Program 5-4
#include <stdio.h> main() /* program to convert
{
Celsius
to Fahrenheit
*/
int celsius; float fahren; printf ("DEGREES printf ("CELSIUS printf ("------DEGREES\n") ; FAHRENHEIT\n");
----------\n") ;
Celsius
value
*/
fahren = (9.0/5.0) * celsius + 32.0; printf ("%5d%12. 2f\n" ,celsius, fahren); celsius = celsius + 5;
The while
Statement
201
Exercises 5.1
1. Rewrite Program 5-1 to print the numbers 2 to 10 in increments of two. The output of your program should be: 2 4 6 8 10
2. Rewrite Program 5-4 to produce a table that starts at a Celsius value of -10 and ends with a Celsius value of 60, in increments of 10 degrees. 3a. For the following program determine the total number of items displayed. Also determine the first and last numbers printed. #include main (
{
<stdio.h>
int while
{
",num);
b. Enter and run the program from Exercise 3a on a computer to verify your answers to the exercise. c. How would the output be affected if the two statements within the compound statement were reversed (that is, if the printf ( ) call were made before the Hnum statement)? 4. Write a C program that converts gallons to liters. The program should display gallons from 10 to 20 in I-gallon increments and the corresponding liter equivalents. Use the relationship: liters = 3.785 * gallons. 5. Write a C program that converts feet to meters. The program should display feet from
202
Chapter Five
Repetition
3 to 30 in 3-foot increments and the corresponding meter equivalents. Use the relationship: meters = feet I 3.28. 6. A machine purchased for $28,000 is depreciated at a rate of $4,000 a year for 7 years. Write and run a C program that computes and displays a depreciation table for 7 years. The table should have the form: Year 1 2
3
---------------------------------------------------------------------------
4 5
6
7. An automobile travels at an average speed of 55 miles per hour for 4 hours. Write a C program that displays the distance driven in miles that the car has traveled after .5,1,1.5, etc., hours until the end of the trip. 8a. An approximate conversion formula for converting Fahrenheit to Celsius temperatures is: Celsius
=
(Fahrenheit - 30) /2
Using this formula and starting with a Fahrenheit temperature of zero degrees, write a C program that determines when the approximate equivalent Celsius temperature differs from the exact equivalent value by more than 4 degrees. (Hint: Use a while loop that terminates when the difference between approximate and exact Celsius equivalents exceeds 4 degrees.) b. Using the approximate Celsius conversion formula given in Exercise 8a, write a C program that produces a table of Fahrenheit temperatures, exact Celsius equivalent temperatures, approximate Celsius equivalent temperatures, and the difference between the correct and approximate equivalent Celsius values. The table should begin at zero degrees Fahrenheit, use 2-degree Fahrenheit increments, and terminate when the difference between exact and approximate values differs by more than 4 degrees. 9. The value of Euler's number, e, can be approximated using the formula e = 1 + 1/1! + 1/2! + 1/3! + 1/4! + 1/5! + ... Using this formula, write a C program that approximates the value of e using a while loop that terminates when the difference between two successive approximations differs by less than 1.0e-6. 10. The value of sin x can be approximated using the formula
SIn
x = x - - +- - - +- . 3! 5! 7! 9!
x3
x5
x7
x9
Using this formula, determine how many terms are needed to approximate the value returned by the intrinsic sin ( ) function with an error less than 1.0e-6, when x =30 degrees. (Hints: Use a while loop that terminates when the difference between the value returned by the intrinsic sin ( ) function and the approximation is less than le-6. Also note that x must first be converted to radian measure and that the alternating sign in the approximating series can be determined as (-1) (n+l) where n is the number of terms used in the approximation.)
203
5.2
Combining the scanf ( ) function with the repetition capabilities of the while statement produces very adaptable and powerful programs. To understand the concept involved, consider Program 5-5, where a while statement is used to accept and then display four user-entered numbers, one at a time. Although it uses a very simple idea, the program highlights the flow of control concepts needed to produce more useful programs.
}Ol,
#include main( )
{
Program 5-5
~stdio.h>
program
some numbers.\n");
(count <= 4)
printf("\nEnter a number: "); scanf("%f". &num); printf ("The number entered is %f". num); count = count + 1;
Following is a sample run of Program 5-5. The italicized items were input in response to the appropriate prompts.
This program will ask you to enter some numbers.
Enter a number: 26.2 The number entered is 26.200000 Enter a number: 5 The number entered is 5.000000 Enter a number: 103.456 The number entered is 103.456000 Enter a number: 1267.89 The number entered is 1267.890000
204
Chapter Five
Repetition
Print a message
No Stop (Condition is false) End of program Yes (condition is true) Print the message Enter a number:
Loop
Add 1 to count
Go back and retest count FIGURE 5-2 Flow of Control Diagram for Program 5-5
205
Let us review the program to clearly understand how the output was produced. The first message displayed is caused by execution of the first pr in t f ( ) function call. This call is outside and before the while statement, so it is executed once before any statement in the while loop. Once the while loop is entered, the statements within the compound statement are executed while the tested condition is true. The first time through the compound statement, the message Enter a number: is displayed. The program then calls scanf ( ), which forces the computer to wait for a number to be entered at the keyboard. Once a number is typed and the RETURN key is pressed, the call to printf ( ) displaying the number is executed. The variable count is then incremented by one. This process continues until four passes through the loop have been made and the value of count is 5. Each pass causes the message Enter a number: to be displayed, causes one call to scanf ( ) to be made, and causes the message The number entered is to be displayed. Figure 5-2 illustrates this flow of control. Rather than simply displaying the entered numbers, Program 5-5 can be modified in order to use the entered data. For example, let us add the numbers entered and display the total. We must be careful in how we add the numbers, since the same variable, num, is used for each number entered. Because of this the entry of a new number in Program 5-5 automatically causes the previous number stored in num to be lost. Thus, each number entered must be added to the total before another number is entered. The required sequence is:
Enter a number Add the number
to the total
How do we add a single number to a total? A statement such as total total + num does the job perfectly. This is the accumulating statement introduced in Section 2.3. After each number is entered, the accumulating statement adds the number into the total, as illustrated in Figure 5-3 (206). The complete flow of control required for adding the numbers is illustrated in Figure 5-4 (p.208). In reviewing Figure 5-4, observe that we have made a provision for initially setting the total to zero before the while loop is entered. If we were to clear the total inside the while loop, it would be set to zero each time the loop was executed and any value previously stored would be erased. Program 5-6 (p. 207) incorporates the necessary modifications to Program 5-5 to total the numbers entered. As indicated in the flow diagram shown in Figure 5-4, the statement total = total + num; is placed immediately after the scanf ( ) function call. Putting the accumulating statement at this point in the program ensures that the entered number is immediately "captured" into the total. Let us review Program 5-6. The variable total was created to store the total of the numbers entered. Prior to entering the while statement the value of total is set to zero. This ensures that any previous value present in the storage location(s) assigned to the variable total is erased. Within the while loop the statement total = total + num; is used to add the value of the entered number into total. As each value is entered, it is added into the existing total to create a new total. Thus, total becomes a running subtotal of all the values entered. Only when all numbers are entered does total contain the final sum of
206
Chapter Five
Repetition
New number
total
The variable tot a 1
total + num
New
~l
total
FIGURE 5-3
all the numbers. After the while loop is finished, the last printf ( ) function call is used to display this sum. Using the same data that was entered in the sample run for Program 5-5, the following sample run of Program 5-6 was made:
This program will ask you to enter some numbers.
Enter a number: 26.2 The total is now 26.200000 Enter a number: 5 The total is now 31.200000 Enter a number: 103.456 The total is now 134.656000 Enter a number: 1267.89 The total is now 1402.546000 The final total is 1402.546000
Having used an accumulating assignment statement to add the numbers entered, we can now go further and calculate the average of the numbers. Where do we calculate the average-within the while loop or outside it? In the case at hand, calculating an average requires that both a final sum and
207
}Ol,
#include main(
{
Program 5-6
<stdio.h>
program
some numbers.\n");
(count <= 4)
printf("\nEnter a number: "); scanf("%f", &num); total = total + num; printf("The total is now %f", total); count = count + 1;
}
printf("\n\nThe
the number of items in that sum be available. The average is then computed by dividing the final sum by the number of items. At this point, we must ask, "At what point in the program is the correct sum available, and at what point is the number of items available?" In reviewing Program 5-6 we see that the correct sum needed for calculating the average is available after the while loop is finished. In fact, the whole purpose of the whi 1e loop is to ensure that the numbers are entered and added correctly to produce a correct sum. After the loop is finished, we also have a count of the number of items used in the sum. However, due to the way th~ while loop was constructed, the number in count (5) when the loop is finished is one more than the number of items (4) used to obtain the total. Knowing this, we simply subtract one from count before using it to determine the average. With this as background, see if you can read and understand Program 5-7 (p. 209). Program 5-7 is almost identical to Program 5-6, except for the calculation of the average. We have also removed the constant display of the total within and after the while loop. The loop in Program 5-7 is used to enter and add four numbers. Immediately after the loop is exited, the average is computed and displayed. Following is a sample run using Program 5-7:
This program will ask you to enter 26.2 5 some numbers.
208
Chapter Five
Repetition
Start
No
Print total
Stop Acccept a
num
Add num
to total
FIGURE 5-4
AccumulationFlowof Control
103.456 1267.89 is 350.636500
of the numbers
Sentinels In many situations we do not know the exact number of items to be entered in advance or the items are too numerous to count beforehand. For example, when entering a large amount of market research data we might -not want to take the time to count the number of actual data items that are to be entered. In cases like this we want to be able to enter data continuously and, at the end, type in a special data value to signal the end of data input. In computer programming, data values used to signal either the start or end of a data series are called sentinels. The sentinel values, of course, must not con-
209
lOll
#include main( )
{
Program 5-7
<stdio.h>
program
printf("Enter a number: scanf ("%f ", &num); total total + num; count = count + 1;
is %f",average);
flict with legitimate data values. For example, if we were constructing a program that accepts a student's grades, and assuming that no extra credit is given that could produce a grade higher than 100,we could use any grade higher than 100 as a sentinel value. Program 5-8 (p. 210) illustrates this concept. In Program 5-8 data is continuously requested and accepted until a number larger than 100 is entered. Entry of a number higher than 100 alerts the program to exit the while loop and display the sum of the numbers entered. Following is a sample run using Program 5-8. As long as grades less than or equal to 100 are entered, the program continues to request and accept additional data. When a number greater than 100 is entered, the program adds this number to the total and exits the while loop. Outside of the loop and within the printf ( ) function call, the value of the sentinel that was added to the total is subtracted. and the sum of the legitimate grades that were entered is display~d.
To stop entering grades, greater than 100. Enter a grade: Enter a grade: Enter a grade: Enter a grade: type in any number
95
100 82 101 is 277.000000
210
Chapter Five
Repetition
}Ol\
Program 5-8
#include main( )
{
<stdio.h>
float grade,
total;
grade = 0; total = 0; printf("\nTo stop entering grades, printf("\n greater than 100.\n"); while
{
printf("\nThe
is %f",total-grade);
One of the most useful sentinels provided in C is the named constant EOF, which stands for End Of File. The actual value of EOF is compiler dependent, but it is always assigned a code that is not used by any other character. This is how EOF works. Each computer operating system has its own code for an end-of-file mark. In the UNIX operating system this mark is generated whenever the CTRL and D keys are pressed simultaneously, while in the IBM-DOS operating system the mark is generated whenever the CTRL and Z keys are pressed simultaneously. When a C program detects this combination of keys as an input value, it converts the input value into its own EOF code, as illustrated in Figure 5-5. The actual definition of the EOF constant, using the #define statement previOlisly described in Section 3.4, is available in the stdio. h compiler source file included in all C programs. For example, consider Program 5-9. Notice that the first line in Program 5-9 is the #include <stdio. h> statement. Since the stdio.h file contains the definition of EOF, this constant may now be referenced in the program.
Constant by the scanf ( ) Function
FIGURE 5-5
Generation of the
EOF
---.
c'
scanf ()
---.-
EOF
211
,101,
Program 5-9
= 0;
/*
*/
printf("\nTo stop entering grades, press either the F6 key"); printf("\n or the CTRL and Z keys simultaneously on IBM computers"); printf("\n or the CTRL and D keys for UNIX operating systems.\n\n"); printf ("Enter a grade: "); while ( scanf ("%f", &grade)
{
!=
EOF )
EOF is used in Program 5-9 to control the while loop. The expression scanf ("% f", &grades) ! = EOF makes use of the fact that the scanf ( ) function returns an EOF value if an attempt is made to read an end-of-file mark.
From a user's viewpoint, assuming an IBM computer is being used, pressing both the CTRL and Z keys simultaneously generates an end-of-file mark, which is converted to the EOF constant by scanf ( ).Following is a sample run using Program 5-9:
To stop entering grades, press either the F6 key or the CTRL and Z keys simultaneously on IBM computers or the CTRL and D keys for UNIX operating systems. Enter Enter Enter Enter a a a a grade: grade: grade: grade: 100 200 300
"z
is 600.000000
One distinct advantage of Program 5-9 over Program 5-8 is that the sentinel value is never added into the total, so it does not have to be subtracted later.1 One disadvantage of Program 5-9, however, is that it requires the user to type in an unfamiliar combination of keys to terminate data input.
This method of input will be very useful when reading data from files rather than from the keyboard. (File input is presented in Chapter 9.)
212
Chapter Five
Repetition
Two useful statements in connection with repetition statements are the break and continue statements. We have previously encountered the break statement in relation to the swi tch statement. The,general form of this statement is:
break;
A break statement, as its name implies, forces an immediate break, or exit, from switch, while, and the for and do-while statements presented in the next sections. For example, execution of the following while loop is immediately terminated if a number greater than 76 is entered:
while(count
{
<= 10)
");
printf("You break;
}
lose!"); /* break
out
of the
loop
*/
else printf("Keep
}
on truckin!"); */
/* break
jumps to here
The break statement violates pure structured programming principles because it provides a second, nonstandard exit from a loop. Nevertheless, the break statement is extremely useful and valuable for breaking out of loops when an unusual condition is detected. The break statement is also used to exit from a swi tch statement, but this is because the desired case has been detected and processed. The continue statement is similar to the break statement but applies only to loops created with while, do-while, and for statements. The general format of a continue statement is:
continue;
When continue is encountered in a loop, the next iteration of the loop is immediately begun. For while loops this means that execution is automatically transferred to the top of the loop and reevaluation of the tested expression is initiated. Although the continue statement has no direct effect on a switch statement, it can be included within a swi tch statement that itself is contained in a loop. Here the effect of continue is the same: the next loop iteration is begun. As a general rule the continue statement is less useful than the break statement, but it is convenient for skipping over data that should not be processed while remaining in a loop. For example, invalid grades are simply ignored in the following section of code and only valid grades are added into the total:
213
while
{
printf("Enter a ~rade: scanf("%f", &grade); if(grade < 0 II grade> total = total + grade;
The Null Statement Statements are always terminated by a semicolon. A semicolon with nothing preceding it is also a valid statement, called the null statement. Thus, the statement
is a null statement. This is a do-nothing statement that is used where a statement is syntactically required, but no action is called for. Null statements typically are used with either while or for statements. An example of a for statement using a null statement is found in Program 5-11c in the next section.
Exercises 5.2
1. Rewrite Program 5-6 to compute the total of eight numbers. 2. Rewrite Program 5-6 to display the prompt: Please type in the total number of data values to be added:
In response to this prompt, the program should accept a user-entered number and then use this number to control the number of times the while loop is executed. Thus, if the user enters 5 in response to the prompt, the program should request the input of five numbers and display the total after five numbers have been entered. 3a. Write a C program to convert Celsius degrees to Fahrenheit. The program should request the starting Celsius value, the number of conversions to be made, and the increment between Celsius values. The display should have appropriate headings and list the Celsius value and the corresponding Fahrenheit value. Use the relationship Fahrenheit = (9.0 / 5.0) * Celsius + 32.0. b. Run the program written in Exercise 3a on a computer. Verify that your program starts at the correct starting Celsius value and contains the exact number of conversions specified in your input data. 4a. Modify the program written in Exercise 3 to request the starting Celsius value, the ending Celsius value, and the increment. Thus, instead of the condition checking for a fixed count, the condition will check for the ending Celsius value. . b. Run the program written in Exercise 4a on a computer. Verify that your output starts at the correct beginning value and ends at the correct ending value. 5. Rewrite Program 5-7 to compute the average of ten numbers. 6. Rewrite Program 5-7 to display this prompt: Please type in the total number of data values to be averaged:
214
Chapter Five
Repetition
In response to this prompt, the program should accept a user-entered number and then use this number to control the number of times the whi 1e loop is executed. Thus, if the user enters 6 in response to the prompt, the program should request the input of six numbers and display the average of the next six numbers entered. 7. By mistake, a programmer put the statement average = total/count; within the while loop immediately after the statement total = total + num; in Program 5-7. Thus, the while loop becomes: while
{
(count
<=
4) ");
printf("Enter a number: scanf("%f", &num); total = total + num; average = total/count; count = count + 1;
Will the program yield the correct result with this while loop? From a programming perspective, which while loop is better to use, and why? 8. In addition to the arithmetic average of a set of numbers both a geometric and harmonic mean can be calculated. The geometric mean of a set of n numbers Xv X2t. is defined as:
. Xn
1 1 -+-+ ... +XI x2 xn Using these formulas, write a C program that continues to accept numbers until the number 999 is entered, and then calculates and displays both the geometric and harmonic means of the entered numbers. (Hint: It will be necessary for your program to correctly count the number of values entered.)
9a. The following data were collected on a recent automobile trip:
Mileage Start of trip: 22495 22841 23185 23400 23772 24055 24434 24804 25276
Gallons Full tank 12.2 11.3 10.5 11.0 12.2 14.7 14.3 15.2
Write a C program that accepts a mileage and gallons value and calculates the rnilesper-gallon (mpg) achieved for that segment of the trip. The miles-per-gallon is obtained
215
as the difference in mileage between fillups divided by the number of gallons of gasoline used in the fillup. . b. Modify the program written for Exercise 9a to additionally compute and display the cumulative mpg achieved after each fillup. The cumulative mpg is calculated as the difference between each fillup mileage and the mileage at the start of the trip divided by the sum of the gallons used to that point in the trip. lOa. A bookstore summarizes its monthly transactions by keeping the following information for each book in stock: Book identification Inventory balance Number of copies Number of copies number at the beginning of the month received during the month sold during the month
Write a C program that accepts this data for each book and then di~plays the book identification number and an updated book inventory balance using the relationship: New Balance = Inventory balance at the beginning of the month + Number of copies received during the month - Number of copies sold during the month Your program should use a while loop with a fixed count condition so that information on only three books is requested. b. Run the program written in Exercise lOa on a computer. Review the display produced by your program and verify that the output produced is correct. 11. Modify the program you wrote for Exercise lOa to keep requesting and displaying results until a sentinel identification value of 999 is entered. Run the program on a computer. l2a. The outstanding balance on Rhona Karp's car loan is $8,000. Each month Rhona is required to make a payment of $300, which includes both interest and principal repayment of the car loan. The monthly interest is calculated as .10/12 of the outstanding balance of the loan. After the interest is deducted the remaining part of the payment is used to payoff the loan. Using this information, write a C program that produces a table indicating the beginning monthly balance, the interest payment, the principal payment, and the remaining loan balance after each payment is made. Your output should resemble and complete the entries in the following table until the outstanding loan balance is zero.
0.000000
b. Modify the program written in Exercise 12a to display the total of the interest and principal paid at the end of the table produced by your program.
216
ChapterFive Repetition
5.3
The for statement performs the same functions as the while statement, but uses a different form. In many situations, especially those that use a fixed count condition, the for statement format is easier to use than its while statement equivalent. The general form of the for statement is:
for (initializing statement; list; expression; altering list)
Although the for statement looks a little complicated, it is really quite simple if we consider each of its parts separately. Within the parentheses of the for statement are three items, separated by semicolons. Each of these items is optional and can be described individually, but the semicolons must be present. In its most common form, the initializing list consists of a single statement used to set the starting (initial value) of a counter; the expression contains the maximum or minimum value the counter can have and determines when the loop is finished; and the altering list provides the increment value that is added to or subtracted from the counter each time the loop is executed. Examples of simple for statements having this form are: for (count printf("%d" and for (i = 5; i <= i5; i printf("%d ", i);
i + 2)
1; count
< 10;
count
count
1)
count);
In the first for statement, the counter variable is named count, the initial value assigned to count is 1, the loop continues as long as the value in count is less than 10, and the value of count is incremented by one each time through the loop. In the next for statement, the counter variable is named i, the initial value assigned to i is 5, the loop continues as long as i's value is less than or equal to 15, and the value of i is incremented by 2 each time through the loop. In both cases a printf ( ) function call is used to display the value of the counter. Another example of a for loop is contained within Program 5-10. When Program 5-10 is executed, the following display is produced:
NUMBER 1
2
3
4 5
217
~Oll Program
#include #include main( )
{
5-10
<stdio.h> <math.h>
ROOT");
") ;
The first two lines displayed by the program are produced by the two
printf st<j.tementsplaced before the for statement. The remaining output is produced by the for loop. This loop begins with the for statement and is exe-
cuted as follows. The initial value assigned to the counter variable count is 1.Since the value in count does not exceed the final value of 5,the execution of the printf statement within the loop produces the display
1 1.000000
Control is then transferred back to the for statement, which then increments the value in count to 2,and the loop is repeated, producing the display
2 1. 414214
This process continues until the value in count exceeds the final value of 5, producing the complete output table. For comparison purposes, an equivalent while loop to the for loop contained in Program 5-10 is:
count while = 1 (count <= 5) %f",count, sqrt(count));
As seen in this example, the difference between the for and while loops is the placement of the initialization, condition test, and incrementing items. The grouping together of these items in the for statement is very convenient when fixed-count loops must be constructed. See if you can determine the output produced by Program 5-11 (p. 218).
218
Chapter Five
Repetition
}Ol,
Program 5-11
#include main( )
{
<stdio.h>
int
count; count
+
2)
Did you figure it out? The loop starts with a count initialized to 2, stops when count exceeds 20, and increments count in steps of 2. The output of Program 5-11is: 2 4 6 8 10 12 14 16 18 20
The for statement does not require that any of the items in parentheses be present or that they be used for initializing or altering the values in the expression statements. However, the two semicolons must be present within the for's parentheses. For example, the construction for ( ; count <= 20 ;) is valid. If the initializing list is missing, the initialization step is omitted when the for statement is executed. This, of course, means that the programmer must provide the required initializations before the for statement is encountered. Similarly, if the altering list is missing, any expressions needed to alter the evaluation of the tested expression must be included directly within the statement part of the loop. The for statement only ensures that all expressions in the initializing list are executed once, before evaluation of the tested expression, and that all expressions in the altering list are executed at the end of the loop before the tested expression is rechecked. Thus, Program 5-11 can be rewritten in any of the three ways shown in Programs 5-11a,5-11b,and 5-11c.
}Ol,
Program 5-11a
#include main( ) int <stdio.h>
219
JOII
Program 5-11b
#include main ( )
{
<stdio.h>
int
count;
/* initializer <= 20; )
outside
for
loop
*/
/*
alteration
statement
*/
,101,
{
Program 5-11c
#include <stdio.h> main( } /* all expressions within the for's parentheses int count; for (count
2;
*/
",count),
count
count
+ 2);
In Program 5-l1a count is initialized outside the for statement and the first list inside the parentheses is left blank. In Program 5-l1b, both the initializing list and the altering list are removed from within the parentheses. Program 5-l1b also uses a compound statement within the for loop, with the expressionaltering statement included in the compound statement. Finally, Program 5-l1c has included all items within the parentheses, so there is no need for any useful statement following the parentheses. Here the null statement satisfies the syntactical requirement of one statement to follow the for's parentheses. Observe also in Program 5-11c that the altering list (last set of items in parentheses) consists of two items, and that a comma has been used to separate these items. The use of commas to separate items in both the initializing and altering lists is required if either of these two lists contains more than one item. Last, note the fact that Programs 5-11a, 5-l1b, and 5-l1c are all inferior to Program 5-11. The for statement in Program 5-11 is much clearer since all the expressions pertaining to the tested expression are grouped together within the parentheses. Although the initializing and altering lists can be omitted from a for statement, omitting the tested expression results in an infinite loop. For example, such a loop is created by the statement
220
Chapter Five
Repetition
count
+ 1)
As with the while statement, both break and continue statements can be used within a for loop. The break forces an immediate exit from the for loop, as it does in the while loop. The continue, however, forces control to be passed to the altering list in a for statement, after which the tested expression is reevaluated. This differs from the action of continue in a while statement, where control is passed directly to the reevaluation of the tested expression. Figure ~ illustrates the internal workings of a for loop. As shown, when the for loop is completed control is transferred to the first executable statement
FIGURE 5-6
for statement
Initializing statements
Expression's value is nonzero (true condition) Execute the statement after the parentheses
Loop
221
following the loop. To avoid the necessity of always illustrating these steps, a simplified set of flowchart symbols is available for describing for loops. Using the fact that a for statement can be represented by the flowchart symbol
for
statement
complete for loops can alternatively be illustrated as shown in Figure 5-7 (p.222). . To understand the enormous power of for loops, consider the task of printing a table of numbers from 1 to 10, including their squares and cubes, using this statement. Such a table was previously produced using a while loop in Program 5-3. You may wish to review Program 5-3 and compare it to Program 5-12 to get a further sense of the equivalence between for and while loops.
1
{
1, Program 5-12
<stdio.h>
#include main( )
num, num*num,
num*num*num);
SQUARE 1 4
9
8
9
10
16 25 36 49 64 81 100
222
Chapter Five
Repetition
Enter
for statement
FIGURE 5-7
Simply changing the number 10 in the for statement of Program 5-12 to 1000 creates a loop that is executed 1000 times and produces a table of numbers from 1 to 1000. As with the while statement this small change produces an immense increase in the processing and output provided by the program. Notice also that the expression ++num was used in the altering list in place of the usual num
num + 1.
Exercises 5.3
1. Write individual for statements for the following cases: a. Use a counter named i that has an initial value of 1, a final value of 20, and an increment of 1. b. Use a counter named icount that has an initial value of 1, a final value of 20, and an increment of 2. c. Use a counter named j that has an initial value of 1, a final value of 100, and an increment of 5. d. Use a counter named icount that has an initial value of 20, a final value of 1, and an increment of -1. e. Use a counter named icount that has an initial value of 20, a final value of 1, and an increment of - 2. f. Use a counter named count that has an initial value of 1.0, a final value of 16.2, and an increment of 0.2. g. Use a counter named xcnt that has an initial value of 20.0, a final value of 10.0, and an increment of -0.5. 2. Determine the number of times that each for loop is executed for the for statements written for Exercise 1. 3. Determine the value in total after each of the following loops is executed: a. total = 0:
for (i total
223
b. total = 1; for (count = 1; count <= 10; count total total * 2; c. total = 0 i + 1) 10; i <= 15; i for ( i total + i; total d. total = 50 i + 1) for (i = 1; i <=10; i total = total - i; e. total = 1 for (icnt = 1; icnt <= 8; ++icnt) total = total * icnt; f. total = 1.0 for (j = 1; j <= 5; ++j) total = total / 2.0; 4. Determine the output of the following program: #include <stdio.h> main ( )
{
count + 1)
5. Modify Program 5-12 to produce a table of the numbers zero through 20 in increments of 2, with their squares and cubes. 6. Modify Program 5-12 to produce a table of numbers from 10 to I, instead of 1 to 10 as it currently does. 7. Write and run a C program that displays a table of 20 temperature conversions from Fahrenheit to Celsius. The table should start with a Fahrenheit value of 20 degrees and be incremented in values of 4 degrees. Recall that Celsius = (5.0/9.0) * (Fahrenheit - 32). 8. Modify the program written for Exercise 7 to initially request the number of conversions to be made. 9. The expansion of a steel bridge as it is heated to a final Celsius temperature, TF, from an initial Celsius temperature, TO, can be ap,proximated using the formula increase in length
=a
* L * (TF-
TO)
where a is the coefficient of expansion, which for steel is 11.7 E-6, and L is the length of the bridge at temperature TO. Using this formula, write a C program that displays a table of expansion lengths for a steel bridge that is 7365 feet long at zero degrees Celsius, as the temperature increases to 40 degrees in five-degree increments. 10. The probability that an individual telephone call will last less than t minutes can be approximated by the exponential probability function Probability that a call lasts less than t minutes
=1-
e-t1a
where a is the average call length and e is Euler's number (2.71828). For example, assuming that the average call length is 2.5 minutes, the probability that a call will last less than one minute is calculated as 1 - e-1/2.5 = 0.3297.
224
Chapter Five
Repetition
Using this probability function, write a C program that calculates and displays a list of probabilities of a call lasting less than one to less than 10 minutes, in one-minute increments. Assume that the average call length is 5 minutes.
lla. The arrival rate of customers in a busy New York bank can be estimated using the
P(x)
a'e-
xl
where x = the number of customer arrivals per minute, a = the average number of arrivals per minute, and c = Euler's number (2.71828). For example, if the average number of customers entering the bank is three customers per minute, then a is equal to .three. Thus, the probability of 0 customers arriving in anyone minute = p(x
30 -3 0) + _c_ O!
.498
1)
= _e_
1!
31
-3
.1494
Using the Poisson probability function, write a C program that calculates and displays the probability of 0 to 20 customer arrivals when the average arrival rate is 3 customers per minute. b. The formula given in Exercise 11a is also applicable for estimating the arrival rate of planes at a busy airport (here, an arriving "customer" is an incoming airplane). Using this same formula modify the program written in Exercise 11a to accept the average arrival rate as an input data item. Then run the modified program to determine the probability of zero to ten planes attempting to land in anyone-minute period at an airport during peak arrival times. Assume that the average arrival rate for peak arrival times is two planes per minute. 12. Write and run a program that calculates and displays the amount of money available in a bank account that initially has $1,000 deposited in it and that earns 8 percent interest a year. Your program should display the amount available at the end of each year for a period of ten years. Use the relationship that the money available at the end of each year equals the amount of money in the account at the start of the year plus .08 times the amount available at the start of the year. 13. A machine purchased for $28,000 is depreciated at a rate of $4,000 a year for seven years. Write and run a C program that computes and displays a depreciation table for seven years. The table should have the form:
Depreciation Schedule Year 1 2 3 4 5 6 7 Depreciation 4000 4000 4000 4000 4000 4000 4000 End-of-Year Value 24000 20000 16000 12000 8000 4000 0 Accumulated Depreciation 4000 8000 12000 16000 20000 24000 28000
-------------------------------------------
225
14. A manufacturer of widgets has been losing 4 percent of its sales each year. The annual profit for the firm is 10 percent of sales. This year the firm has had $10 million in sales and a profit of $1 million. Determine the expected sales and profit for the next 10 years. Your program should complete and produce a display as follows:
Sales and Profit Projection Year 1 2 3 Expected Sales $10000000.00 $ 9600000.00 Projected Profit $1000000.00 $ 960000.00
10 Totals:
5.4
for
In this section we present four common programming techniques associated with for loops. Technique 1: Interactive Input Within a
for
Loop
Using a scanf ( ) statement inside a for loop produces the same effect as when this statement is used inside a while loop, For example, in Program 5-13 a scanf ( ) statement is used to input a set of numbers. As each number is input,
)01\
Program 5-13
#define MAXCOUNT 5 #include <stdio.h> main ( ) /* this program calculates the average o( five user-entered numbers
{
*/
int count; float num, total, average; for (total = 0.0, count = 1; count <= MAXCOUNT; ++count)
{
printf ("Enter a number: "); scanf ("%f", &num); total = total + num;
}
226
Chapter Five
Repetition
it is added to a total. When the for loop is exited, the average is calculated and displayed. The for statement in Program 5-13 creates a loop that is executed five times. The user is prompted to enter a number' each time through the loop. After each number is entered, it is immediately added to the total. Notice that total is initialized to zero when the initializing list of the for statement is executed. The loop in Program 5-13 is executed as long as the value in count is less than or equal to five, and is terminated when count becomes six (the increment to six, in fact, is what causes the loop to end).
Technique 2: Selection Within a for Loop
Another common programming technique is to use a for loop to cycle through a set of numbers and select those numbers that meet one or more criteria. For example, assume that we want to find both the positive and negative sum of a set of numbers. The criterion here is whether the number is positive or negative, and the logic for implementing this program is given by the pseudocode
for each number
{
Enter a number If the number is greater than zero add the number to the positive sum else add the number to the negative sum
} Program 5-14 describes this algorithm in C for the case where five numbers are to be entered. Following is a sample run using Program 5-14: Enter Enter Enter Enter Enter a a a a a number number number number number (positive (positive (positive (positive (positive is is or or or or or negative) negative) negative) negative) negative)
10 -10 5 -7 11
total total
26.000000 -17.000000
A for loop can be conveniently constructed to determine and display the values of a single variable mathematical function for a set of values over any specified interval. For example, assume that we want to know the values of the function y
= 10.r + 3x - 2
for x between two and six. Assuming that x has been declared as an integer variable, the following for loop can be used to calculate the required values:
227
(~QI\
Program 5-14
#include <stdio.h> main ( ) /* this program computes the positive and negative sums of a set */ /* of five user entered numbers */
{
*/ */
printf ("Enter a number (positive or negative) scanf ("%f", &usenum); if (usenum > 0) postot - postot + usenum; else negtot - negtot + usenum;
}
");
printf("\nThe positive total is %f", postot); printf("\nThe negative total is %f", negtot);
for
(
For this loop we have used the variable x as both the counter variable and the unknown (independent variable) in the function. For each value of x from two to six a new value of y is calculated and displayed. This for loop is contained within Program 5-15 (p. 228), which also displays appropriate headings for the values printed. The following is displayed when Program 5-15 is executed:
x value
2 3 y value 44
4 5
6
Two items are important here. First, any equation with one unknown can be evaluated using a single for loop. The method requires substituting the desired
228
ChapterFive Repetition
1,
Program 5-15
#include #include main( )
{
<stdio.h> <math.h>
int
x, y;
* pow(x,2)
%3d
printf("\n
+ 3 * x - 2; %3d", x, y);
equation into the for loop in place of the equation used in Program 5-15, and adjusting the counter values to match the desired solution range. Second, we are not constrained to using integer values for the counter variable. For example, by specifying a noninteger increment, solutions for fractional values can be obtained. This is shown in Program 5-16, where the equation y = 10~ + 3x - 2 is evaluated in the range x = 2 to x = 6 in increments of 0.5.
,1PI,
Program 5-16
#include #include main( )
{
<stdio.h> <math.h>
float
x, y;
y = 10.0
5-16, to allow these variables to take on fractional values. The following is the
229
x value 2.000000 2.500000 3.000000 3.500000 4.000000 4.500000 5.000000 5.500000 6.000000
y value
Technique 4: Interactive Loop Control Values used to control a for loop may be set using variables rather than constant values. For example, the four statements
5; 10; 1; k for (count
j
i; count
<=
j; count
count
+ k)
The advantage of the first for statement, where variables are used in the initialization, condition, and altering expression lists, is that it allows us to assign values for these expressions external to the for statement. This is especially useful when a scanf ( ) function call is used to set the actual values. To make this a little more tangible, consider Program 5-17.
1
{
1,
Program 5-17
#include <stdio.h> main ( ) /* this program displays a table of numbers, their squares and cubes */ /* starting from the number 1. The final number in the table is */ /* input by the user */ int num, final; printf("Enter the final number for the table: "); scanf ("%d", &final); printf ("\nNUMBER SQUARE CUBE") ; printf("\n------ ------ ----"); for (num = 1; num <= final; +~num) printf ("\n%3d %3d %4d", num, num*num, num*num*num);
230
Chapter Five
Repetition
In Program 5-17, we have used a variable name within the condition (middle) expression only. Here a scanf ( ) statement has been placed before the for statement to allow the user to decide what the final value should be. Notice that this arrangement permits the user to set the size of the table at run time, rather than having the programmer set the table size at compile time. This also makes the program more general, since it now can be used to create a variety of tables without the need for reprogramming and recompiling.
Exercises 5.4
1. (scanf ( ) Within a Loop) Write and run a C program that accepts six Fahrenheit temperatures, one at a time, and converts each value entered to its Celsius equivalent before the next value is requested. Use a for loop in your program. The conversion required is Celsius = (5.0/9.0) * (Fahrenheit - 32). 2. (scanf ( ) Within a Loop) Write and run a C program that accepts ten individual values of gallons, one at a time, and converts each value entered to its liter equivalent before the next value is requested. Use a for loop in your program. The conversion required is liters = 3.785 * gallons. 3. (Interactive Loop Control) Modify the program written for Exercise 2 to initially request the number of data items that will be entered and converted. 4. (Interactive Loop Control) Modify Program 5-14 so that the number of entries to be in
input is specified by the user when the program is executed.
5. (Selection) Modify Program 5-14 so that it displays the average of the positive and negative numbers. (Hint: Be careful not to count the number zero as a negative number.) Test your program by entering the numbers 17, -10,19,0, -4. The positive average displayed by your program should be 18.5 and the negative average -7. 6a. (Selection) Write a C program that selects and displays the maximum value of five numbers that are to be entered when the program is executed. (Hint: Use a for loop with both a scanf ( ) and an if statement internal to the loop.) b. Modify the program written for Exercise 6a so that it displays both the maximum value and the position in the input set of numbers where the maximum occurs. 7. (Mathematical Functions) Modify Program 5-16 to produce a table of y values for the following: a. y b. y c. Y
= 3x 5
2x3 + x
234
= l+x+~+~+~ =
2e.
8t
8. (Mathematical Functions) A model of worldwide population in billions of people is given by the equation Population
= 4.880
+ e.02t)
where t is the time in years (t = 0 represents January 1985 and t = 1 represents January 1986). Using this formula, write a C program that displays a yearly population table for the years 1988 through 1994. 9. (Mathematical Functions) The x and y coordinates, as a function of time, t, of a projectile fired with an initial velocity v at an angle of 6 with respect to the ground is given by
Nested Loops
231
= =
v t cos(a) v t sin(a)
Using these formulas, write a C program that displays a table of x and y values for a projectile fired with an initial velocity of 500ft/ sec at an angle of 22.8degrees. (Hint: Remember to convert to radian measure.) The table should contain values corresponding to the time interval 0 to 10seconds in increments of one-half seconds. 10. (Interactive Loop Control) Modify Program 5-17to accept the starting and increment values of the table produced by the program. 11. (Interactive Loop Control) Write a C program that converts Fahrenheit to Celsius temperature in increments of 5 degrees. The initial value of the Fahrenheit temperature and the total conversions to be made are to be requested as user input during program execution. Recallthat Celsius = (5.0/9.0) * (Fahrenheit - 32.0).
/* /* printf("\ni is now %d\n",i);/* for(j = 1; j <= 4; ++j) /* printf(" j = %d", j); /* /* = 1; i <= 5; ++i) .
start of outer
loop <------+
*/ */
start of inner loop <--, end of inner loop <---' end of outer loop <-------+
*/ */ */ */
The first loop, controlled by the value of i, is called the outer loop. The second loop, controlled by the value of j, is called the inner loop. Notice that all statements. in the inner loop are contained within the boundaries of the outer loop and that we have used a different variable to control each loop. For each single trip through the outer loop, the inner loop runs through its entire sequence. Thus, each time the i counter increases by I, the inner for loop executes completely. This situation is illustrated in Figure 5-8 (p. 232). Program 5-18 (p. 232) includes the above code in a working program. Following is the output of a sample run of Program 5-18:
i is now j = 1 i is now j = 1 i is now j = 1 i is now j = 1 i is now j = 1 1
j 2 j 3 j 2 2 2 2 2 j j j j j
3 3 3 3 3
j j j j j
4 4 4 4 4
4
j
5
j
232
ChapterFive Repetition
1
{
1, Program 5-18
<stdio.h>
#include main( )
/* /* printf("\ni is now %d\n",i) ;/* for(j = 1; j <= 4; ++j) /* printf (" j = %d", j); /* /*
start of outer
loop <------+
*/
start of inner loop <--, end of inner loop <---' end of outer loop <-------+
*/ */ */ */ */
FIGURE 5-8
j=3
j=2
i=2
j=2
Nested Loops
233
Let us use a nested loop to compute the average grade for each student in a class of 20 students. Each student has taken four exams during.the course of the semester. The final grade is calculated as the average of these examination grades. Program Analysis The pseudocode for this example is:
for 20 times
{
set student grade total to zero for 4 times
{
input a grade add the grade to the total } j* end of inner for loop *j calculate student's average grade print the student's average grade } j* end of outer for loop *j
As described by the pseudocode, an outer loop consisting of 20 passes will be used to compute the average grade for each student. The inner loop will consist of 4 passes. One examination grade is entered in each inner loop pass. As each grade is entered it is added to the total for the student, and at the end of the loop the average is calculated and displayed. Program 5-19 uses a nested loop to make the required calculations.
,101,
{
Program 5-19
#include <stdio.h> main ( ) int i,j; float grade, total, average; for (i = 1; i <= 20; ++i)
{
/* /* /*
*/ */
printf("Enter an examination grade for this student: ") ; scanf ("%f", &grade); /* add the grade into the total */ total total + grade; /* end of the inner for loop */ /* calculate the average */ average total / 4; printf("\n\nThe average for student %d is %f", i,average) ; */ /* end of the outer for loop
234
Chapter Five
Repetition
is calculated and displayed immediately after the inner loop is finished. Since the statements that compute and print the average are also contained within the outer loop, 20 averages are calculated and displayed. The entry and addition of each grade within the inner loop use techniques we have seen before, which should now be familiar to you.
Exercises 5.5
1. Four experiments are performed, each experiment consisting of six test results. The results for each experiment are given below. Write a program using a nested loop to compute and display the average of the test results for each experiment. 1st experiment 2nd experiment 3rd experiment 4th experiment results: results: results: results: 23.2 34.8 19.4
36.9
16.9 27.9
10.2
49.2
28.6
39.4
13.4 50.6
2. Modify the program written for Exercise 1 so that the number of test results for each experiment is entered by the user. Write your program so that a different number of test results can be entered for each experiment. 3. a. A bowling team consists of five players. Each player bowls three games. Write a C program that uses a nested loop to enter each player's individual scores and then computes and displays the average score for each bowler. Assume that each bowler has the following scores: 1st bowler: 2nd bowler: 3rd bowler: 4th bowler: 5th bowler: 286 212 252
192
186
b. Modify the program written for Exercise 3a to calculate and display the average team score. (Hint: Use a second variable to store the total of all the players' scores.)
4. Rewrite the program written for Exercise 3a to eliminate the inner loop. To do this, you will have to input three scores for each bowler rather than one at a time. 5. Write a program that calculates and displays values for Y when Y = X3Zj(X-Z) Your program should calculate Y for values of X ranging between 1 and 5 and values of Z ranging between 2 and 6. X should control the outer loop and be incremented in steps of 0.2 and Z should be incremented in steps of 0.5. Your program should also display the message FUNCTION UNDEFINED when the X and Z values are equal. 6. Write a program that calculates and displays the yearly amount available if $1,000 is invested in a bank account for 10 years. Your program should display the amounts available for interest rates from 6 percent to 12 percent inclusively, at 1 percent increments. Use a nested loop, with the outer loop having a fixed count of 7 and the inner
The do Statement
235
loop a fixed count of 10.The first iteration of the outer loop should use an interest rate of 6 percent and display the amount of money available at the end of the first 10 years. In each subsequent pass through the outer loop, the interest rate should be increased by 1 percent. Use the relationship that the money available at the end of each year equals the amount of money in the account at the start of the year, plus the interest rate times the amount available at the start of the year. .
salestax = RATE * price; printf("The sales tax is $%5.2f",salestax); printf("\nEnter a price: "); scanf("%f", &price);
Using this while statement requires either duplicating the prompt and scanf ( ) function calls before the loop and then within the loop, as we have done, or resorting to some other artifice to force initial execution of the statements within the while loop. The do statement, as its name implies, allows us to do some statements before an expression is evaluated. In many situations this can be used to eliminate the duplication illustrated in the previous example. The general form of the do statement is: do
statement; while (expression);.-
As with all C programs, the single statement in the do may be replaced with a compound statement. A flow-control diagram illustrating the operation of the do statement is shown in Figure 5-9. As illustrated in Figure 5-9, all statements within the do statement are executed at least once before the expression is evaluated. Then, if the expression has a nonzero value, the statements are executed again. This process continues until the expression evaluates to zero. For example, consider the following do statement:
236
Chapter Five
Repetition
do
{
printf("\nEnter a price: "I; scanf("%f", &price); salestax = RATE * price; printf("The sales tax is $%5.2f",
}
salestax);
while
(price != SENTINEL);
Observe that only one prompt and scanf ( ) statement are required because the tested expression is evaluated at the end of the loop. As with all repetition statements, the do statement can always replace or be replaced by an equivalent while or for statement. The choice of which statement to use depends on the application and the style preferred by the programmer. In general, the while and for statements are preferred because they clearly let anyone reading the program know what is being tested "right up front" at the top of the program loop. Validity Checks The do statement is particularly useful in filtering user-entered input and providing data validity checks. For example, assume that an operator is required to enter a valid customer identification number between the numbers 1000 and 1999. A number outside this range is to be rejected and a new request for a valid
FIGURE 5-9
do
statement
Expression's value is zero (false condition) Expression's value is nonzero (true condition) Go back and execute the statement
Exit the
do
statement
The do Statement
237
number made. The following section of code provides the necessary data filter to verify the en~ryof a valid identification number:
do
{
number:
");
while
(id_num
<
1000 I I id_num
>
1999);
Here, a request for an identification number is repeated until a valid number is entered. This section of code is "bare bones" in that it neither alerts the operator to the cause of the new request for data nor allows premature exit from the loop if a valid identification number cannot be found. An alternative removing the first drawback is:
do
{
printf("\nEnter an identification number: scanf ( %f", &id_num); if (id_num < 1000 I I id_num > 1999)
II
");
printf("\n An invalid number was just entered"); printf("\nPlease check the ID number and re-enter");
}
*/
Here we have used a break statement to exit from the loop. Since the expression being evaluated by the do statement is always 1 (true), an infinite loop has been created-that is only exited when the break statement is encountered.
Exercises 5.6
1 a. Using a do statement, write a C program to accept a grade. The program should request a grade continuously as long as an invalid grade is entered. An invalid grade is any grade less than 0 or greater than 100. After a valid grade has been entered, your program should display the value of the grade entered. b. Modify the program written for Exercise 1a so that the user is alerted when an invalid grade has been entered. c. Modify the program written for Exercise Ib so that it allows the user to exit the program by entering the number 999. d. Modify the program written for Exercise 1b so that it automatically terminates after five invalid grades are entered.
2
a. Write a C program that continuously requests a grade to be entered. If the grade is less than 0 or greater than 100, your program should print an appropriate message informing the user that an invalid grade has been entered, else the grade should be
238
Chapter Five
Repetition
added to a total. When a grade of 999 is entered the program should exit the repetition loop and compute and display the average of the valid grades entered. b. Run the program written in Exercise 2a on a computer and verify the program using appropriate test data. 3 a. Write a C program to reverse the digits of a positive integer number. For example, if the number 8735 is entered, the number displayed should be 5378. (Hint: Use a do statement and continuously strip off and display the units digit of the number. If the variable nurn initially contains the number entered, the units digit is obtained as (nurn % 10) . After a units digit is displayed, dividing the number by 10 sets up the number for the next iteration. Thus, (8735 % 10) is 5 and (8735 / 10) is 873. The do statement should continue as long as the remaining number is not zero.) b. Run the program written in Exercise 3a on a computer and verify the program using appropriate test data. 4. Repeat any of the exercises in Section 5.3 using a do statement rather than a for statement.
10; ++ count);
Here the semicolon at the end of the first line of code is a null statement. This has the effect of creating a loop that is executed 10 times with nothing done except the incrementing and testing of count. This error tends to occur because C programmers are used to ending most lines with a semicolon. The next error occurs when commas are used to separate the items in a for statement instead of the required semicolons. An example of this is the statement for (count
= 1, count
<
10,
++count)
Chapter Summary
239
Commas must be used to separate items within the initializing and altering lists, and semicolons must be used to separate these lists from the tested expression. The last error occurs when the final semicolon is omitted from the do statement. This error is usually made by programmers who have learned to omit the semicolon after the parentheses of a while statement and carry over this habit when the reserved word whi 1 e is encountered at the end of a do statement.
5.8
Chapter Summary
1. The while, for, and do repetition statements create program loops. These statements evaluate an expression and, based on the resulting expression value, either terminate the loop or continue with it. 2. The while statement checks its expression before any other statement in the loop. This requires that any variables in the tested expression have values assigned before while is encountered. Within a while loop there must be a statement that alters the tested expression's value. . 3. The for statement is extremely useful in creating loops that must be executed a fixed number of times. Initializing expressions, the tested expression, and expressions affecting the tested expression can all be included in parentheses at the top of a for loop. Additionally, any other loop statement can be included within the for's parentheses as part of its altering list. 4. The do statement checks its expression at the end of the loop. This ensures that the body of a do loop is executed at least once. Within a do loop there must be at least one statement that alters the tested expression's value.
Arrays
Chapter Six
6.1 6.2 6.3 6.4 6.5 6.6 6.7 One-Dimensional Arrays Array Initialization Two-Dimensional Arrays Applications Common Programming Errors Chapter Summary Enrichment Study: Sorting Methods
240
6.1
One-Dimensional Arrays
241
The variables used so far have all had a common characteristic: Each variable could only be used to store a single value at a time. For example, although the variables key, count, and grade declared in the statements
char key; int count; float grade;
are of different data types, each variable can only store one value of the declared data type. These types of variables are called scalar variables. A scalar variable is a single variable that cannot be further subdivided or separated into a legitimate data type. Frequently we may have a set of values, all of the same data type, that form a logical group. For example, Figure 6-1 illustrates three groups of items. The first group is a list of five floating point temperatures, the second group is a list of four character codes, and the last group is a list of six integer voltages. A simple list containing individual items of the same scalar data type is called a one-dimensional array. In this chapter we describe how one-dimensional arrays are declared, initialized, stored inside a computer, and used. Additionally, we explore the use of one-dimensional arrays with example programs and present the procedures for declaring and using multi-dimensional arrays.
FIGURE 6-1
z
C K
12 5 3 55 16 6
242
Chapter Six
Arrays
Temperatures 95.75 83.0 97.625 72.5 86.25 FIGURE 6-2 A List of Temperatures
type of the items in the array, the array (or list) name, and the number of items in the array. Further examples of array declarations are: int volts [5] ; char code[4]; float amount[lOO]; Each array has sufficient memory reserved for it to hold the number of data items given in the declaration statement. Thus, the array named volts has storage reserved for five integers, the array named code has storag