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.

Slides:



Advertisements
Similar presentations
Functions. COMP104 Functions / Slide 2 Introduction to Functions * A complex problem is often easier to solve by dividing it into several smaller parts,
Advertisements

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.
© 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
1 ICS103 Programming in C Lecture 5: Introduction to Functions.
CS 201 Functions Debzani Deb.
Chapter 3 Top-Down Design with Functions Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.
Chapter 6: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
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.
Chapter 6: User-Defined Functions I
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.
Topic 4 – Programmer- Defined Functions. CISC 105 – Topic 4 Functions So far, we have only seen programs with one function, main. These programs begin.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
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.
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.
1 ICS103 Programming in C Ch3: Top-Down Design with Functions.
CP104 Introduction to Programming Selection structures_2 Lecture 10 __ 1 If statement (preview version) Syntax form: if ( condition) Statements else Statements.
(2-2) Functions I H&K Chapter 3 Instructor - Andrew S. O’Fallon CptS 121 (Spetember 9, 2015) Washington State University.
C Programming Lecture 8-1 : Function (Basic). What is a Function? A small program(subroutine) that performs a particular task Input : parameter / argument.
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
1 ICS103 Programming in C Lecture 7: Introduction to Functions.
Programming in C++ Language ( ) Lecture 5: Functions-Part1 Dr. Lubna Badri.
Chapter 3 Top-Down Design with Functions and Classes Lecture Notes Prepared By: Blaise W. Liffick, PhD Department of Computer Science Millersville University.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 6 User-Defined Functions I. Objectives Standard (predefined) functions What are they, and How to use them User-Defined Functions Value returning.
chap3 Chapter 3 Top-Down Design with Functions.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
More about Quadratic Equations November 16, 2009.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
EASTERN MEDITERRANEAN UNIVERSITY EENG212 Algorithms and Data Structures Functions in C.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Problem Session Working in pairs of two, solve the following problem...
1 MODULAR DESIGN AND ABSTRACTION. 2 SPECIFYING THE DETAILS OF A PROBLEM INTO A RELATED SET OF SMALLER PROBLEMS.
Principles of Programming - NI Simple Recursion Recursion is where a function calls itself. Concept of recursive function: A recursive function is.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
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.
Functions. Why use functions? They can break your problem down into smaller sub-tasks (modularity).  easier to solve complex problems They make a program.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
USING & CREATING FUNCTIONS. MODULAR PROGRAMMING  Why Modular Programming?  Improves Readability & Understandability  Improve Maintainability  Allows.
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.
CS1001 Programing Fundamental Lecture 5 Top-Down Design with Functions
Chapter 6: User-Defined Functions I
Function Topic 4.
Functions Examples CSCI 230
Functions Department of Computer Science-BGU יום רביעי 12 ספטמבר 2018.
FUNCTIONS EXAMPLES.
Functions Examples CSCI N305
Lec8.
Chapter 6: User-Defined Functions I
月夜憶舍弟 戍鼓斷人行,邊秋一雁聲。 露從今夜白,月是故鄉明。 有弟皆分散,無家問死生。 寄書長不達,況乃未休兵。 杜甫
月夜憶舍弟 戍鼓斷人行,邊秋一雁聲。 露從今夜白,月是故鄉明。 有弟皆分散,無家問死生。 寄書長不達,況乃未休兵。 杜甫
Top-Down Design with Functions
Top-Down Design with Functions
Presentation transcript:

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 design and structure charts Basic concepts about functions –Prototype, definition and function call –Input arguments –Output arguments –Void function and void argument –Actual parameter / formal parameter –Local variable Building programs from existing information

CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 2 Reuse of Existing Code C has a rich function library consists many predefined functions. –What are they? arguments, output, and their data types. Use C library functions –To simply a program –To reduce the errors –To write program efficiently Example: mathematical function library –contains commonly used math function routines: ceil(x), cos(x), exp(x), fabs(x), floor(x), log(x), log10(x), pow(x,y), sqrt(x), sin(x), tan(x) –To include math library by adding #include –To call a math function

CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 3 Case Study: Find the Roots of a Quadratic Equation ax 2 +bx+c = 0 Problem –Get the coefficients a, b, and c. Compute and display the roots of ax 2 +bx+c = 0 Analysis –Input: a, b, c –Output: root_1, root_2 –Relevant formulas root_1 = root_2 =

CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 4 Find the Roots of a Quadratic Equation (Cont’d) Algorithm design Algorithm 1 1.Get a, b, c 2.Calculate root_1 = (-b + sqrt( pow(b,2) – 4*a*c ))/(2*a) 3.Calculate root_2 = (-b - sqrt( pow(b,2) – 4*a*c ))/(2*a) 4.Display root_1, root_2. Algorithm 2 1.Get a, b, c 2.Calculate the discriminant: disc = pow(b,2) – 4*a*c; 3.Calculate the square root of discriminant: sqrt_ disc = sqrt(disc); 4.Calculate root_1 = (-b + sqrt_disc)/(2*a) 5.Calculate root_2 = (-b – sqrt_disc)/(2*a) 6.Display root_1, root_2 Optimize the algorithm by reducing the number of operations. Tradeoff between space and time

CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 5 Implementation: using existing math functions #include main() { double a, b, c; double root_1, root_2; double disc, sqrt_disc; printf("Enter the coefficients a, b, and c:\n"); scanf("%lf%lf%lf", &a, &b, &c); disc = b*b - 4*a*c; sqrt_disc = sqrt(disc); /* sqrt() is a math function in math lib */ root_1 = (-b + sqrt_disc)/(2*a); root_2 = (-b - sqrt_disc)/(2*a); printf("The roots of %fx^2 + %fx + %f = 0 are \nroot_1 = %f, root_2 = %f\n", a, b, c, root_1, root_2); fflush(stdin); getchar(); }

CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 6 Case Study: Finding the Area and Circumferences of a Circle Problem –Get the radius of a circle. Compute and display the circle’s area and circumference. Analysis –Input: radius –Output: area, circumference –Relevant formulas Problem constants: PI Area = PI * radius 2 Circum = 2* PI * radius –Data requirements: double radius, area, circum Algorithm design 1.Get the radius of a circle 2.Calculate the area: area = PI * radius*radius 3.Calculate the circumference: circum = 2*PI*radius 4.Display the area and the circumference Implementation

CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 7 Calculating the Area and the Circumference of a Circle

CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 8 User Defined Function and Top-Down Design Method C allow a user to define a function. This makes th top-down design possible Top-down design_ a problem-solving method in which one first break a problem up into its major subproblems and then solve the subproblems to derive the solution to the original problem Structure chart __ a documentation tool that shows the relationships among the sub-problems of a problem.

CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 9 Using Self-Defined Functions #include #define PI double area_circle(double); double circum_circle(double); 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 */ printf("Enter radius> "); scanf("%lf", &radius); area = area_circle(radius); circum = circum_circle(radius); /* Display the area and circumference */ printf("The area is %.4f\n", area); printf("The circumference is %.4f\n", circum); return 0; } /* Calculate the area */ double area_circle(double r) { return(PI * r * r); } /* Calculate the circumference */ double circum_circle(double r) { return(2*PI *r); }

CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 10 Structured Programming A program in which individual program tasks are performed by independent section of program code Advantages –Easier to write structured program become complex programming problems are broken into a number of smaller and simpler taskes –Easier to debug, i.e., easier to isolate a bug to a specific section of code –Code reuse, functions in one program can be used in another program for the same task With functions top-down design method can be applied to write structured program.

CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 11 Case study: Draw a Simple Diagram Decomposition of the figure Draw a circle Draw a intersecting lines Draw a base line

CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 12 Structure Chart for Drawing a Stick Figure

CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 13 Function Prototypes and Main Function for Stick Figure

CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 14 Function draw_circle

CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 15 Function draw_triangle

CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 16 Program to Draw a Stick Figure

CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 17 Program to Draw a Stick Figure (cont’d)

CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 18 Flow of Control Between the main Function and a Function Subprogram

CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 19 Case Study: Multiply two Numbers (floating) #include /* function prototype */ void printMessage(void); double multiply(double, double); int main(void){ double a, b, c; printMessage(); scanf(“%lf %lf”,&a,&b); c = multiply(a,b); printf(“%f”, c); } /* definition of function multiply */ void printMessage(){ printf(“Input two numbers:\n”); } double multiply(double x, double y){ double z; z = x * y; return(z); } #include /* definition of function multiply */ void printMessage(){ printf(“Input two numbers:\n”); } double multiply(double x, double y){ double z; z = x * y; return(z); } main(void){ double a, b, c; printMessage(); scanf(“%lf %lf”,&a,&b); c = multiply(a,b); printf(“%f”, c); }