CS1001 Programing Fundamental Lecture 5 Top-Down Design with Functions

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs.
Advertisements

Chapter 3: Top-Down Design with Functions Problem Solving & Program Design in C Sixth Edition By Jeri R. Hanly & Elliot B. Koffman.
Chapter Five Functions
Computer Programming w/ Eng. Applications
Chapter 3 Top-Down Design with Functions. 3-2 Outline 3.1 BUILDING PROGRAMS FROM EXISING INFORMATION –CASE STUDY: FINDING THE AREA AND CIRCUMFERENCE OF.
Chapter 3 Top-Down Design with Functions Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
Representation of Data Types Comparing double and int data types Using integers are faster and more precise round-off errors when using doubles The range.
Copyright © 2002 W. A. Tucker1 Chapter 3 Lecture Notes Bill Tucker Austin Community College COSC 1315.
© 2012 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 3: Top-Down Design with Functions Problem Solving & Program.
COS120 Software Development Using C++ AUBG Fall semester 2010 Ref book: Problem Solving, Abstraction and Design Using C++ Authors: Frank Friedman, Elliot.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3: Top-Down Design with Functions Problem Solving and Program Design.
ICS103: Programming in C 3: Top-Down Design with Functions
UNIT 4 Top-Down Design & Functions.
1 ICS103 Programming in C Lecture 5: Introduction to Functions.
Top-Down Design with Functions and Classes By Dr. Awad Khalil Computer Science & Engineering Department.
TDBA66, VT-03 Lecture - Ch. 21 A complete C-program Display Fig. 2.1 and comment on different things such as Preprocessor directives Header files Identifiers.
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
Software Development Method. Assignments Due – Homework 0, Warmup Reading – Chapter 2 –
CS 201 Functions Debzani Deb.
Chapter 3 Top-Down Design with Functions Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.
CS 201 Functions Debzani Deb.
Top-Down Design with Functions 4 What do programmer’s (not programs!) use as input to the design of a program? –Documentation Problem definition Requirements.
1 ICS103 Programming in C Lecture 2: Introduction to C (1)
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 3: Top-Down Design with Functions Problem Solving & Program.
CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 1 Top-Down Design with Functions C Library functions Case studies Top-down.
Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept.
1 Introduction to Computers II Lecture 4 Dr. Mehmet Demirer Dr. Seniha Esen Yuksel.
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CP 202 Chapter 3 Slides By Dr. Daniyal Alghazzawi.
Lecture 5 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
Top-Down Design with Functions and Classes Chapter 3.
1 ICS103 Programming in C Lecture 7: Introduction to Functions.
Chapter 3 Top-Down Design with Functions and Classes Lecture Notes Prepared By: Blaise W. Liffick, PhD Department of Computer Science Millersville University.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3: Top-Down Design with Functions and Classes Problem Solving, Abstraction,
chap3 Chapter 3 Top-Down Design with Functions.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
Modularity using Functions Chapter 4. Modularity In programming blocks of code often can be "called up" and reused whenever necessary, for example code.
Chapter 3 : Top Down Design with Functions By Suraya Alias.
Chapter 3 Top-Down Design with Functions. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-2 Figure 3.1 Edited Data Requirements and Algorithm.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
1 Lecture Three I/O Formatting and Arithmetic Dr. Sherif Mohamed Tawfik.
© 2012 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 3: Top-Down Design with Functions Problem Solving & Program.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 3 (Functions) © CPCS /1433 – Term 1.
Lecture2.
CS1010 Programming Methodology
Lesson #6 Modular Programming and Functions.
Function Topic 4.
Lesson #6 Modular Programming and Functions.
Functions, Part 2 of 2 Topics Functions That Return a Value
ICS103 Programming in C Lecture 3: Introduction to C (2)
Representation of data types
Lecture2.
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
Lesson #6 Modular Programming and Functions.
Compiled and ready to run Memory Stack /*
Functions.
Chapter 3: Top-Down Design with Functions and Classes
Lec8.
Functions, Part 2 of 3 Topics Functions That Return a Value
Lecture3.
Lesson #6 Modular Programming and Functions.
Top-Down Design with Functions
Functions, Part 2 of 3 Topics Functions That Return a Value
Top-Down Design with Functions
Computer Science II CS132/601* Lecture #C-1.
Presentation transcript:

CS1001 Programing Fundamental Lecture 5 Top-Down Design with Functions (Chapter 3) Lecturer: Narong Wesnarat

Building Programs from Existing Information and programs Programs are often developed from existing solution(s) to other problems or other programs write a new program by editing source code of an existing program can safe time in typing necessary parts of the program, for examples Preprocessor directives and macros Constant and variable declarations #include <stdio.h> /* printf, scanf definitions */ #define PI 3.14159 /* The constant PI */ int main(void) { double x,y,z; int i,j,k; these statements are common to all programs

Building Programs from Existing Information and programs System documentation during software development phase are useful and may be reused description of a problem’s data requirement solution algorithms Initial algorithm and its refinements can be used as program comments int main(void) { double miles; /* input - distance in miles. */ double kms; /* output - distance in kilometers */ /* Get the distance in miles. */ ………………. insert C statement(s) here /* Convert the distance to kilometers. */ /* Distance in kilometers is 1.609 * distance in miles. */ /* Display the distance in kilometers. */ return (0);

Case Study: Finding the Area and Circumference Problem Get the radius of a circle. Compute and display the circled’s area and circumference. ANALYSIS Clearly, the input is the circle’s radius Two outputs are required the circle’s area the circumference These variable should be type double because input/outputs may contain fraction parts Data Requirements Problem Constant PI 3.14159 Problem Input radius /* radius of a circle */ Problem Outputs area /*area of a circle */ circum /* circumference of a circle */ Relevant Formulas area of a circle = ¶ x radius 2 circumference of a circle = 2 ¶ x radius

Case Study: Finding the Area and Circumference of a Circle Design Initial Algorithm get the circle radius calculate the area calculate the circumference display the area and the circumference Algorithm Refinements Step 2 Refinement 2.1 Assign PI * radius * radius to area Step 3 Refinement 3.1 Assign 2* PI * radius to circum.

Case Study: Finding the Area and Circumference of a Circle IMPLEMENTATION Figure 3.2 Outline of Program Circle /* * Calculates and displays the area and circumference of a circle */ #include <stdio.h> #define PI 3.14159 int main(void) { double radius; /* input - radius of a circle */ double area; /* output - area of a circle */ double circum; /* output - circumference */ /* Get the circle radius */ /* Calculate the area */ /* Assign PI * radius * radius to area. */ /* Calculate the circumference */ /* Assign 2 * PI * radius to circum. */ /* Display the area and circumference */ return (0);

Case Study: Finding the Area and Circumference Figure 3.3 Calculating the Area and the Circumference of a Circle /* * Calculates and displays the area and circumference of a circle */ #include <stdio.h> #define PI 3.14159 int main(void) (continued) Figure 3.3 (continued) { double radius; /* input - radius of a circle */ double area; /* output - area of a circle */ double circum; /* output - circumference */ /* Get the circle radius */ printf("Enter radius> "); scanf("%lf", &radius); /* Calculate the area */ area = PI * radius * radius; /* Calculate the circumference */ circum = 2 * PI * radius; /* Display the area and circumference */ printf("The area is %.4f\n", area); printf("The circumference is %.4f\n", circum); return (0); } Sample Run: Enter radius> 5.0 The area is 78.5397 The circumference is 31.4159

Case Study: Computing the Weight of a Batch of a Flat Watchers Problem You work for a hardware company that manufactures flat washers. To estimate shipping costs, your company needs a program that computes the weights of a specified quantity of flat washers.

Case Study: Computing the Weight of a Batch of a Flat Watchers ANALYSIS To compute the weight of a single flat washers you need to know its rim area its thickness the density of material Data Requirements Problem Constant PI 3.14159 Problem Inputs double hole_diameter; /* input - diameter of hole */ double edge_diameter; /* input - diameter of outer edge */ double thickness; /* input - thickness of washer */ double density; /* input - density of material used */ double quantity; /* input - number of washers made */

Case Study: Computing the Weight of a Batch of a Flat Watchers Problem Outputs double weight; /* output - weight of washer batch */ Program Variables double hole_radius; /* radius of hole */ double edge_radius; /* radius of outer edge */ double rim_area; /* area of rim */ double unit_weight; /* weight of 1 washer */ Relevant Formulas area of a circle = ¶ x radius 2 radius of a circle = diameter/2 rim area = area of outer circle – area of hole unit weight = rim area x thickness x density

Case Study: Computing the Weight of a Batch of a Flat Watchers Design Initial Algorithm Get the washer’s inner diameter, outer diameter, thickness Get the material density and quantity of washers manufactured Compute the rim area Compute the weight of one flat washer Computer the weight of the batch of washers Display the weight of the batch of washers Step 3 Refinement 3.1 Compute hole_radius and edge_radius 3.2 rim_area is PI * edge_radius * edge_radius – PI * hole_radius * hole_radius Step 4 Refinement 4.1 unit_weight is rim_area * thichness * density

Case Study: Computing the Weight of a Batch of a Flat Watchers Figure 3.5 Flat Washer Program /* Computes the weight of a batch of flat washers. */ #include <stdio.h> #define PI 3.14159 int main(void) { double hole_diameter; /* input - diameter of hole */ double edge_diameter; /* input - diameter of outer edge */ double thickness; /* input - thickness of washer */ double density; /* input - density of material used */ double quantity; /* input - number of washers made */ double weight; /* output - weight of washer batch */ double hole_radius; /* radius of hole */ double edge_radius; /* radius of outer edge */ double rim_area; /* area of rim */ double unit_weight; /* weight of 1 washer */ /* Get the inner diameter, outer diameter, and thickness.*/ printf("Inner diameter in centimeters> "); scanf("%lf", &hole_diameter); printf("Outer diameter in centimeters> "); scanf("%lf", &edge_diameter); printf("Thickness in centimeters> "); scanf("%lf", &thickness); Implementation

Implementation (cont.) Case Study: Computing the Weight of a Batch of a Flat Watchers /* Get the material density and quantity manufactured. */ printf("Material density in grams per cubic centimeter> "); scanf("%lf", &density); printf("Quantity in batch> "); scanf("%lf", &quantity); /* Compute the rim area. */ hole_radius = hole_diameter / 2.0; edge_radius = edge_diameter / 2.0; rim_area = PI * edge_radius * edge_radius - PI * hole_radius * hole_radius; /* Compute the weight of a flat washer. */ unit_weight = rim_area * thickness * density; /* Compute the weight of the batch of washers. */ weight = unit_weight * quantity; /* Display the weight of the batch of washers. */ printf("\nThe expected weight of the batch is %.2f", weight); printf(" grams.\n"); return (0); } Implementation (cont.) Inner diameter in centimeters> 1.2 Outer diameter in centimeters> 2.4 Thickness in centimeters> 0.1 Material density in grams per cubic centimeter> 7.87 Quantity in batch> 1000 The expected weight of the batch is 2670.23 grams.

3.2 Library Functions Predefined Functions and Code Reuse A Goal of Software Engineering write error-free code Code Reuse, reusing program fragments that have already been written and tested whenever possible. Promotion of Code Reuse in C predefined functions C’s standard math library Example: a function sqrt( ) performs the square root computation y = sqrt(x); function name function call argument

Fig. 3.7 Square Root Program /* * Performs three square root computations */ #include <stdio.h> /* definitions of printf, scanf */ #include <math.h> /* definition of sqrt */ int main(void) { double first, second, /* input - two data values */ first_sqrt, /* output - square root of first */ second_sqrt, /* output - square root of second */ sum_sqrt; /* output - square root of sum */ /* Get first number and display its square root. */ printf("Enter the first number> "); scanf("%lf", &first); first_sqrt = sqrt(first); printf("The square root of the first number is %.2f\n", first_sqrt); /* Get second number and display its square root. */ printf("Enter the second number> "); scanf("%lf", &second); second_sqrt = sqrt(second); printf("The square root of the second number is %.2f\n", second_sqrt); /* Display the square root of the sum of the two numbers. */ sum_sqrt = sqrt(first + second); printf("The square root of the sum of the two numbers is %.2f\n", sum_sqrt); return (0); } Enter the first number> 9.0 The square root of the first number is 3.00 Enter the second number> 16.0 The square root of the second number is 4.00 The square root of the sum of the two numbers is 5.00

Some Mathematical Library Functions

Some Mathematical Library Functions (cont.)

Using C Library Functions Example 3.2

Using C Library Functions Example 3.3 Triangle with an Unknown Side If we know the lengths of two sides (b and c0 of a triangle and the angle between them in degree ( α), the length of the third side (a) can be computed a 2 = b 2 + c 2 – 2bc cos α In C language: a = sqrt(pow(b,2) + pow(c,2) - 2*b*c*cos(alpha *PI/180.0));

3.3 Top-Down Design and Structure Charts Some algorithms are more complex Programmers must break up the problem into sub-problems to develop the program solution Attempting to solve a problem at one level, new sub-problems at lower levels may be needed This process is called “Top-Down Design” approach proceeds from the original problem at the top level to the sub-problems at each lower level Structure Chart is a documentation tool for Top-Down Design

3.3 Top-Down Design and Structure Charts An example of a Structure Chart Fig. 3.10

3.3 Top-Down Design and Structure Charts Case Study: Drawing Simple Diagrams Problem Draw a house and a female stick figure Analysis The house and the female stick figure can be drawn from simple shapes, i.e. a circle parallel lines a base line intersecting lines Fig. 3.9

3.3 Top-Down Design and Structure Charts Case Study: Drawing Simple Diagrams (cont.) DESIGN Initial Algorithm Draw a circle Draw a triangle Draw intersecting lines Algorithm Refinements Step 2 Refinement 2.1 Draw intersecting lines 2.2 Dray a base The structure chart is shown in Fig. 3.10

3.4 Functions without arguments functions with argument (s) functions without argument Example: The atatement draw_circle(); /*call a function draw_circle with out argument */ This statement call a function draw_circle that implement the algorithm step Draw a circle. Function Call Statement (without argument) SYNTAX: fname(); EXAMPLE: draw_circle(); Interpretation: The function fname is called. after fname has finished execution, the statement that follows the function call will be executed.

Function Prototypes A function must be declared before it can be referenced (called) To declare a function: insert a function prototype before the main function Figure 3.11 Function Prototypes and Main Function for Stick Figure /* Draws a stick figure */ #include <stdio.h> /* function prototypes */ void draw_circle(void); /* Draws a circle */ void draw_intersect(void); /* Draws intersecting lines */ void draw_base(void); /* Draws a base line */ void draw_triangle(void); /* Draws a triangle */ int main(void) { /* Draw a circle. */ draw_circle(); /* Draw a triangle. */ draw_triangle(); /* Draw intersecting lines. */ draw_intersect(); return (0); } Function Prototype (without argument) FORM: ftype fname(void); EXAMPLE: void draw_circle(void); ftype specifies the type of returned value, e.g., int, double, char, etc. ftype = void means the function does not return a value argument = void means no argument

Function Definitions Function definitions Function Heading (not ended by ‘;’ ) Function body enclosed in brackets Return statement (can be eliminated if no value is returned) Figure 3.23 Function scale /* * Multiplies its first argument by the power of 10 specified * by its second argument. * Pre : x and n are defined and math.h is included. */ double scale(double x, int n) { double scale_factor; /* local variable */ scale_factor = pow(10, n); return (x * scale_factor); }

Placement of Functions in a Program Figure 3.14 Program to Draw a Stick Figure /* Draws a stick figure */ #include <stdio.h> /* Function prototypes */ void draw_circle(void); /* Draws a circle */ void draw_intersect(void); /* Draws intersecting lines */ void draw_base(void); /* Draws a base line */ void draw_triangle(void); /* Draws a triangle */ int main(void) { /* Draw a circle. */ draw_circle(); /* Draw a triangle. */ draw_triangle(); /* Draw intersecting lines. */ draw_intersect(); return (0); } /* Draws a circle */ void draw_circle(void) printf(" * \n"); printf(" * * \n"); printf(" * * \n"); Placement of Functions in a Program

Placement of Functions in a Program /* * Draws intersecting lines */ void draw_intersect(void) { printf(" / \\ \n"); /* Use 2 \'s to print 1 */ printf(" / \\ \n"); printf("/ \\\n"); } * Draws a base line draw_base(void) printf("-------\n"); * Draws a triangle draw_triangle(void) draw_intersect(); draw_base(); Figure 3.14 Program to Draw a Stick Figure (continue) Placement of Functions in a Program