Chapter 3 TopDown Design with Functions Problem Solving
Chapter 3: Top-Down Design with Functions Problem Solving & Program Design in C Seventh Edition By Jeri R. Hanly & Elliot B. Koffman Addison Wesley is an imprint of © 2012 Pearson Addison-Wesley. All rights reserved.
Figure 3. 1 Edited Data Requirements and Algorithm for Conversion Program 1 -2 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -2
Figure 3. 2 Outline of Program Circle 1 -3 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -3
Figure 3. 3 Calculating the Area and the Circumference of a Circle 1 -4 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -4
Figure 3. 3 Calculating the Area and the Circumference of a Circle (cont’d) 1 -5 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -5
Figure 3. 4 Computing the Rim Area of a Flat Washer 1 -6 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -6
Figure 3. 5 Flat Washer Program 1 -7 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -7
Figure 3. 5 Flat Washer Program (cont’d) 1 -8 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -8
Figure 3. 6 Function sqrt as a “Black Box” 1 -9 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -9
Figure 3. 7 Square Root Program 1 -10 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -10
Figure 3. 7 Square Root Program (cont’d) 1 -11 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -11
Figure 3. 9 House and Stick Figure 1 -12 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -12
Figure 3. 10 Structure Chart for Drawing a Stick Figure 1 -13 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -13
Figure 3. 11 Function Prototypes and Main Function for Stick Figure 1 -14 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -14
Figure 3. 12 Function draw_circle 1 -15 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -15
Figure 3. 13 Function draw_triangle 1 -16 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -16
Figure 3. 14 Program to Draw a Stick Figure 1 -17 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -17
Figure 3. 14 Program to Draw a Stick Figure (cont’d) 1 -18 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -18
Figure 3. 14 Program to Draw a Stick Figure (cont’d) 1 -19 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -19
Figure 3. 15 Flow of Control Between the main Function and a Function Subprogram 1 -20 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -20
Figure 3. 16 Function instruct and the Output Produced by a Call 1 -21 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -21
Figure 3. 18 Function print_rboxed and Sample Run 1 -22 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -22
Figure 3. 18 Function print_rboxed and Sample Run (cont’d) 1 -23 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -23
Figure 3. 19 Effect of Executing print_rboxed (135. 68); 1 -24 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -24
Figure 3. 20 Function with Input Arguments and One Result 1 -25 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -25
Figure 3. 21 Functions find_circum and find_area 1 -26 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -26
Figure 3. 22 Effect of Executing circum = find_circum (radius); 1 -27 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -27
Figure 3. 23 Function scale 1 -28 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -28
Figure 3. 24 Testing Function scale 1 -29 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -29
Figure 3. 25 Data Areas After Pass by Value Call scale(num_1, num_2); 1 -30 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -30
Figure 3. 26 Referencing pixels in a window 1 -31 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -31
Figure 3. 27 Drawing intersecting lines 1 -32 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -32
Figure 3. 28 Window drawn by lines. c 1 -33 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -33
Figure 3. 29 Drawing a house 1 -34 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -34
Figure 3. 30 House drawn by house. c 1 -35 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -35
Chapter 4: Selection Structures: if and switch Statements Problem Solving & Program Design in C Seventh Edition By Jeri R. Hanly & Elliot B. Koffman Addison Wesley is an imprint of © 2012 Pearson Addison-Wesley. All rights reserved.
Figure 4. 1 Evaluation Tree and Step-by. Step Evaluation for !flag || (y + z >= x - z) 1 -37 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -37
Figure 4. 2 Range of True Values for min <= x && x <= max 1 -38 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -38
Figure 4. 3 Range of True Values for z > x || x > y 1 -39 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -39
Figure 4. 4 Flowcharts of if Statements with (a) Two Alternatives and (b) One Alternative 1 -40 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -40
Figure 4. 5 Program Using an if statement for selection 1 -41 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -41
Figure 4. 6 if Statement to Order x and y 1 -42 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -42
Figure 4. 7 Structure Chart for Water Bill Problem 1 -43 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -43
Figure 4. 8 Program for Water Bill Problem 1 -44 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -44
Figure 4. 8 Program for Water Bill Problem (cont’d) 1 -45 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -45
Figure 4. 8 Program for Water Bill Problem (cont’d) 1 -46 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -46
Figure 4. 8 Program for Water Bill Problem (cont’d) 1 -47 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -47
Figure 4. 9 Sample Run of Water Bill Program 1 -48 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -48
Figure 4. 10 Function comp_use_charge Revised 1 -49 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -49
Figure 4. 10 Function comp_use_charge Revised (cont’d) 1 -50 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -50
Figure 4. 11 Function comp_tax 1 -51 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -51
Figure 4. 12 Flowchart of Road Sign Decision Process 1 -52 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -52
Figure 4. 13 Program Using a switch Statement for Selection Can we remove the break statements? Try it out? 1 -53 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -53
Figure 4. 13 Program Using a switch Statement for Selection (cont’d) 1 -54 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -54
Chapter 5: Repetition and Loop Statements Problem Solving & Program Design in C Seventh Edition By Jeri R. Hanly & Elliot B. Koffman Addison Wesley is an imprint of © 2012 Pearson Addison-Wesley. All rights reserved.
Figure 5. 1 Flow Diagram of Loop Choice Process 1 -56 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -56
Figure 5. 2 Program Fragment with a Loop 1 -57 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -57
Figure 5. 3 Flowchart for a while Loop 1 -58 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -58
Figure 5. 4 Program to Compute Company Payroll 1 -59 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -59
Figure 5. 4 Program to Compute Company Payroll (cont’d) 1 -60 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -60
Figure 5. 5 Using a for Statement in a Counting Loop 1 -61 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -61
Figure 5. 6 Comparison of Prefix and Postfix Increments 1 -62 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -62
Figure 5. 7 Function to Compute Factorial 1 -63 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -63
Figure 5. 13 Nested Counting Loop Program 1 -64 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -64
Figure 5. 14 Validating Input Using do-while Statement 1 -65 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -65
Assignment 1 • Write a program that computes and displays the sum of a collection of Celsius temperatures entered at the terminal until a sentinel value of -275 is entered. 1 -66 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -66
Self Study • Other variations of writing for loops: – Can we write “for (; ; ) {}” • How to use break and continue statements in loops? • Can functions returning void use the return statement? if yes, how? 1 -67 © 2012 Pearson Addison-Wesley. All rights reserved. 1 -67
- Slides: 67