Savitch Prog Prob Solv wC 3rd PPT Slides - PowerPoint PPT Presentation

1 / 45
About This Presentation
Title:

Savitch Prog Prob Solv wC 3rd PPT Slides

Description:

The subproblems should be smaller and easier to understand and to solve than the big problem. ... given the user's budget. #include iostream #include cmath ... – PowerPoint PPT presentation

Number of Views:43
Avg rating:3.0/5.0
Slides: 46
Provided by: davidb65
Category:

less

Transcript and Presenter's Notes

Title: Savitch Prog Prob Solv wC 3rd PPT Slides


1
Problem Solving with C The Object of Programming
Walter Savitch Chapter 3 Procedural Abstraction
and Functions That Return a Value Slides by
David B Teague, Western Carolina University, A
constituent institution of the University of
North Carolina
2
3.1 Top Down Design
  • Step wise refinement, also known as divide and
    conquer, means dividing the problem into
    subproblems such that once each has been solved,
    the big problem is solved.
  • The subproblems should be smaller and easier to
    understand and to solve than the big problem.

3
3.2 Predefined FunctionsLibraries
  • C comes with libraries of predefined functions.
  • How do we use predefined (or library) functions?
  • Everything must be declared before it is used.
    The statement include ltfilegt brings the
    declarations of library functions into your
    program, so you can use the library functions.

4
3.2 Predefined FunctionsLibraries
  • include ltcmathgt // include declarations of
    math
  • // library
    functions
  • include ltiostreamgt // include definitions of
  • //
    iostream objects
  • using namespace std // make names available
  • int main()
  • cout ltlt sqrt(3.0) ltlt endl // call math
    library function
  • //
    sqrt with argument 3.0, send
  • //
    the returned value to cout

4
5
3.2 Function call
  • A function call is an expression consisting of a
    function name followed by arguments enclosed in
    parentheses. Multiple arguments are separated by
    commas.
  • Syntax
  • FunctionName(Arg_List)
  • where Arg_List is a comma separated list of
    arguments.
  • Examples
  • side sqrt(area)
  • cout ltlt2.5 to the power 3.0 is
  • ltlt pow(2.5, 3.0)

6
Display 3.1 A Function call
//Computes the size of a dog house that can be
purchased //given the users budget. include
ltiostreamgt include ltcmathgt using namespace
std int main( ) const double
COST_PER_SQ_FT 10.50 double budget, area,
length_side cout ltlt "Enter the amount
budgeted for your dog house " cin gtgt
budget area budget/COST_PER_SQ_FT
length_side sqrt(area) // the
function call

7
Display 3.1 A Function call (b)
cout.setf(iosfixed)
cout.setf(iosshowpoint) cout.precision(2)
cout ltlt "For a price of " ltlt budget ltlt endl
ltlt "I can build you a luxurious square
dog house\n" ltlt "that is " ltlt
length_side ltlt " feet on each
side.\n" return 0

8
PITFALL Problems with Library functions
  • Some compilers do not comply with the ISO
    Standard.
  • If your compiler does not work with
  • include ltiostreamgt
  • use
  • include ltiostream.hgt
  • Similarly, for headers like cstdlib use
    stdlib.h, and for cmath, use math.h
  • Most compilers at least coexist with the headers
    without the .h, but some are hostile to these
    headers.

9
3.2 Predefined Functions Type changing functions
  • Question 9/2 is an int, but we want the 4.5
    floating point result. We want the integer part
    of some floating point number. How do we manage?
  • Answer Type casting, or type changing
    functions.
  • C provides a function named double that takes a
    value of some other type and converts it to
    double.
  • Example
  • int total, number
  • double ratio
  • // input total, number
  • winnings double(total) / number

10
PITFALL Integer division drops the fractional
part
  • REMEMBER integer division returns just the
    quotient. The remainder (and any fractional part)
    are dropped.
  • If the numerator and denominator are both integer
    values, the division is done as integer
    arithmetic, dropping the fractional part.
  • Example 11 / 2 is 5, NOT 5.5
  • 10 / 3 is 3, NOT 3.333
  • Even if assigned to a double variabledouble
    ratio 10/3 // assigned value is 3, NOT 3.3

11
3.3 Programmer Defined Functionsfunction
definitions
  • You can define your own functions.
  • You can put your functions in the same file as
    the main function or in a separate file.
  • If you put your function after the function
    containing a call to your function, or in a
    separate file, you must put a prototype (defined
    in the next slide) for your function some place
    in the file where it is called, prior to the
    call.

12
3.3 Programmer Defined Functionsfunction
prototypes
  • A function prototype tells you all the
    information you need to call the function. A
    prototype of a function (or its definition) must
    appear in your code prior to any call to the
    function.
  • Syntax
    Dont forget the semicolon
  • Type_of_returned_value Function_Name(Parameter_lis
    t)
  • Place prototype comment here.
  • Parameter_list is a comma separated list of
    parameter definitions
  • type_1 param_1, type_2 param_2, . type_N
    param_N
  • Example
  • double total_weight(int number, double
    weight_of_one)
  • // Returns total weight of number of items
    that
  • // each weigh weight_of_one

13
3.3 Programmer Defined FunctionsA function is
like a small program
  • To understand functions, keep these points in
    mind
  • A function definition is like a small program and
    calling the function is the same thing as running
    this small program.
  • A function has formal parameters, rather than
    cin, for input. The arguments for the function
    are input and they are plugged in for the formal
    parameters.
  • A function of the kind discussed in this chapter
    does not send any output to the screen, but does
    send a kind of output back to the program. The
    function returns a return-statement instead of
    cout-statement for output.

14
3.3 Programmer Defined FunctionsDisplay 3.3 A
function Definition (Slide 1 of 2)
  • include ltiostreamgt
  • using namespace std
  • double total_cost(int number_par, double
    price_par)
  • //Computes the total cost, including 5 sales
    tax,
  • //on number_par items at a cost of price_par
    each.
  • int main( )
  • double price, bill
  • int number
  • cout ltlt "Enter the number of items purchased
    "
  • cin gtgt number
  • cout ltlt "Enter the price per item "
  • cin gtgt price
  • bill total_cost(number, price)
    The function call
  • cout.setf(iosfixed)
  • cout.setf(iosshowpoint)
  • cout.precision(2)
  • cout ltlt number ltlt " items at "
  • ltlt "" ltlt price ltlt " each.\n"

15
3.3 Programmer Defined Functions Display 3.3 A
function Definition (Slide 2 of 2)
  • double total_cost(int number_par, double
    price_par) The function


  • heading
  • heading
  • const double TAX_RATE 0.05 //5 sales tax

    The function
  • double subtotal
    The
    function definition


  • body
  • subtotal price_par number_par
  • return (subtotal subtotalTAX_RATE)

16
3.3 Programmer Defined FunctionsCall-by-value
Parameters
  • Consider the function call
  • bill total_cost(number, price)
  • The values of the arguments number and price are
    plugged in for the formal parameters . This
    process is (A precise definition of what
    plugged means will be presented later. For now,
    we will use this simile.)
  • A function of the kind discussed in this chapter
    does not send any output to the screen, but does
    send a kind of output back to the program. The
    function returns a return-statement instead of
    cout-statement for output.

17
3.3 Programmer Defined FunctionsAlternate form
for Function Prototypes
  • The parameter names are not required
  • double total_cost(int number, double price)
  • It is permissible to write
  • double total_cost(int, double )
  • Nevertheless, code should be readable to
    programmers as well as understandable by the
    compiler, so check for readability and chose to
    use parameter names when it increases
    readability.
  • Function HEADERS (in the definition) must use
    parameter names. (There is an exception to this
    rule that we will not deal with in this course.)

18
3.3 Programmer Defined FunctionsAnatomy of a
Function Call (part 1 of 3)
  • Review Display 3.3
  • 0 Before the function is called, the value of the
    variables number and price are set to 2 and
    10.10, by cin.
  • 1 The following statement, which includes a
    function call, begins executing
  • bill total_cost(number, price)
  • 2 The value of number (which is 2) is plugged in
    for number_par and the value of price (which is
    10.10) is plugged for price_par
  • double total_cost(int number_par, double
    price_par)
  • const double TAX_RATE 0.05 //5 sales
    tax
  • double subtotal
  • subtotal price_par number_par
  • return (subtotal subtotalTAX_RATE)

19
3.3 Programmer Defined Functions Anatomy of a
Function Call (part 2 of 3)
  • The execution produces the following effect
  • double total_cost(int 2, double 10.10)
  • const double TAX_RATE 0.05 //5 sales
    tax
  • double subtotal
  • subtotal 10.10 2
  • return (subtotal subtotalTAX_RATE)

20
3.3 Programmer Defined Functions Anatomy of a
Function Call (part 3 of 3)
  • 3 The body of the function is executed, i.e., the
    following is executed
  • const double TAX_RATE 0.05 //5 sales
    tax
  • double subtotal
  • subtotal 10.10 2
  • return (subtotal subtotalTAX_RATE)
  • 4 When the return statement is executed, the
    value of the expression after the return is the
    value returned by the function. In this case,
    when
  • return (subtotal subtotalTAX_RATE)
  • is executed, the value of (subtotal
    subtotalTAX_RATE), which is 21.21 is returned by
    the function call
  • bill total_cost(number, price)
  • the value of bill is set to 21.21 when the
    following statement ends
  • bill total_cost(number, price)

21
PITFALL Arguments in the wrong order
  • When a function is called, C substitutes the
    first argument given in the call for the first
    parameter in the definition, the second argument
    for the second parameter, and so on.
  • There is no check for reasonableness. The only
    things checked are i) that there is agreement of
    argument type with parameter type and ii) that
    the number of arguments agrees with the number of
    parameters.
  • If you do not put correct arguments in call in
    the correct order, C will happily assign the
    wrong arguments to the right parameters.

22
3.3 Programmer Defined FunctionsSummary of
Syntax for a Function that Returns a Value.
  • Function Prototype
  • Type_Returned Function_Name(Parameter_List)
  • Prototype Comment

  • function header
  • Function Definitions
  • Type_Returned Function_Name(Parameter_List)
  • Declaration_1
  • Declaration_2
  • . . .
    Must include one or
  • Declaration_Last
    more return statements.
  • body Executable_1
  • Executable_2
  • . . .
  • Executable_Last

23
3.4 Procedural AbstractionPrinciple of
Information Hiding
  • David Parnas, in 1972 stated the principle of
    information hiding.
  • A functions author (programmer) should know
    everything about how the function does its job,
    but nothing but specifications about how the
    function will be used.
  • The client programmer -- the programmer who will
    call the function in her code -- should know only
    the function specifications, but nothing about
    how the function is implemented.

24
3.4 Procedural AbstractionThe Black Box Analogy
  • Based on the Principle of Information Hiding, the
    text describes the BLACK BOX analogy.
  • When using a function, we behave as if we know
    nothing about how the function does its job, that
    we know only the specifications for the function.
  • When writing a function, we behave as if we know
    nothing but the specifications about how the
    function is to be used.
  • In this way, we avoid writing either application
    code that depends on the internals of the
    function or writing function code that depends in
    some way on the internal structure of the
    application.

25
3.4 Procedural AbstractionThe Principle
  • When applied to a function definition, the
    principle of procedural abstraction means that
    your function should be written so that it can be
    used like a black box. This means the user of a
    function should not need to look at the
    internals of the function to see how the function
    works. The function prototype and accompanying
    commentary should provide enough information for
    the programmer to use the function.
  • To ensure that your function definitions have
    this property, you should adhere to these rules
  • The prototype comment should tell the programmer
    any and all conditions that are required of the
    arguments to the function and should describe the
    value returned by the function.
  • All variables used in the function body should be
    declared in the function body. (The formal
    parameters are already declared in the function
    header.)

26
3.5 Local Variables
  • Variables declared within the body of a function
    definition are said to be local to that function,
    or have that functions block as their scope.
  • Variables declared within the body of the main
    function are said to be local to the main
    function, or to have that functions block as
    their scope.
  • When we say a function is a local variable
    without further mention of a function, we mean
    that variable is local to some function
    definition.
  • If a variable is local to a function, you can
    have another variable with the same name that is
    local to either main or some other function.

27
3.5 Local Variables Global Constants and Global
Variables
  • A named constant declared outside the body of any
    function definition is said to be a global named
    constant. A global named constant can be used in
    any function definition that follows the constant
    declaration. There is an exception to this we
    will point out later.
  • Variables declared outside the body any function
    is said to be global variables or to have global
    scope. Such a variable can be used in any
    function definition that follows the constant
    declaration. There is also an exception to this
    we will point out later.
  • Use of global variables ties functions that use
    the global variables in a way that makes
    understanding the functions individually nearly
    impossible. There is seldom any reason to use
    global variables.

28
Display 3.11 Global Named Constants (Part 1 of 2)
  • //Computes the area of a circle and the volume of
    a sphere.
  • //Uses the same radius for both calculations.
  • include ltiostreamgt
  • include ltcmathgt
  • using namespace std
  • const double PI 3.14159
  • double area(double radius)
  • //Returns the area of a circle with the specified
    radius.
  • double volume(double radius)
  • //Returns the volume of a sphere with the
    specified radius.
  • int main( )
  • double radius_of_both, area_of_circle,
    volume_of_sphere
  • cout ltlt "Enter a radius to use for both a
    circle\n"

29
Display 3.11 Global Named Constants (Part 2 of 2)
  • area_of_circle area(radius_of_both)
  • volume_of_sphere volume(radius_of_both)
  • cout ltlt "Radius " ltlt radius_of_both ltlt "
    inches\n"
  • ltlt "Area of circle " ltlt
    area_of_circle
  • ltlt " square inches\n"
  • ltlt "Volume of sphere " ltlt
    volume_of_sphere
  • ltlt " cubic inches\n"
  • return 0
  • double area(double radius)
  • return (PI pow(radius, 2))
  • double vo lume(double radius)

30
3.5 Local Variables Call-by-value Formal
Parameters are Local Variables
  • Formal parameters are more than just blanks to be
    filled in with values from arguments.
  • We promised a more precise discussion of what
    value plugged in for the argument means. Here
    is that discussion.
  • Formal parameters are actually local variables
    that are initialized by the call mechanism to the
    value of the argument. They may be used in any
    way that a local variable can be used.
  • Display 3.12 (next slide) is an example of a
    formal parameter used as a local variable.

31
Display 3.12 Formal Parameter Used as Local
Variable (Part 1 of 2)
  • //Law office billing program.
  • include ltiostreamgt
  • using namespace std
  • const double RATE 150.00 //Dollars per quarter
    hour.
  • double fee(int hours_worked, int minutes_worked)
  • //Returns the charges for hours_worked hours and
  • //minutes_worked minutes of legal services.
  • int main( )
  • int hours, minutes
  • double bill
  • cout ltlt "Welcome to the offices of\n"
  • ltlt "Dewey, Cheatham, and Howe.\n"
  • ltlt "The law office with a heart.\n"

32
Display 3.12 Formal Parameter Used as Local
Variable (Part 2 of 2)
  • bill fee(hours, minutes) The
    value of minutes is not

  • changed by a call to fee.
  • cout.setf(iosfixed)
  • cout.setf(iosshowpoint)
  • cout.precision(2)
  • cout ltlt "For " ltlt hours ltlt " hours and " ltlt
    minutes
  • ltlt " minutes, your bill is " ltlt bill ltlt
    endl
  • return 0

  • minutes_worked is a local variable

  • initialized to the value of minutes
  • double fee(int hours_worked, int minutes_worked)
  • int quarter_hours
  • minutes_worked hours_worked60
    minutes_worked
  • quarter_hours minutes_worked/15
  • return (quarter_hoursRATE)

33
3.5 Local Variables Namespaces Revisited (1 of 2)
  • All our use of namespaces has amounted to
  • include ltiostreamgt
  • using namespace std
  • While this is correct, we are sidestepping the
    reason namespaces were introduced into C,
    though we have done this for good teaching
    reasons.
  • In short, we have been polluting the global
    namespace.
  • So long as our programs are small, so this is not
    a problem.
  • This wont always be the case, so you should
    learn to put the using directive in the proper
    place.

34
3.5 Local Variables Namespaces Revisited (2 of 2)
  • Placing a using directive anywhere is analogous
    to putting all the definitions from the namespace
    there. This is why we have been polluting the
    global namespace. We have been putting all the
    namespace std names from our header file in the
    global namespace.
  • The rule, then is
  • Place the namespace directive,
  • using namespace std
  • inside the block where the names will be used.
    The next slide is Display 3.13 with the namespace
    directive place correctly.

35
3.5 Local Variables Display 3.13 Using
Namespaces (1 of 2)
  • //Computes the area of a circle and the volume of
    a sphere.
  • //Uses the same radius for both calculations.
  • include ltiostreamgt
  • include ltcmathgt//Some compilers may use math.h
    instead of cmath.
  • const double PI 3.14159
  • double area(double radius)
  • //Returns the area of a circle with the specified
    radius.
  • double volume(double radius)
  • //Returns the volume of a sphere with the
    specified radius.
  • int main( )
  • using namespace std
  • double radius_of_both, area_of_circle,
    volume_of_sphere

36
3.5 Local Variables Display 3.13 Using
Namespaces (1 of 2)
  • area_of_circle area(radius_of_both)
  • volume_of_sphere volume(radius_of_both)
  • cout ltlt "Radius " ltlt radius_of_both ltlt "
    inches\n"
  • ltlt "Area of circle " ltlt
    area_of_circle
  • ltlt " square inches\n"
  • ltlt "Volume of sphere " ltlt
    volume_of_sphere
  • ltlt " cubic inches\n"
  • return 0
  • double area(double radius)
    The behavior of this program is exactly

  • the same as that of Display
    3.11
  • using namespace std
  • return (PI pow(radius, 2))
  • double volume(double radius)
  • using namespace std

37
3.6 Overloading Function Names
  • C distinguishes two functions by examining the
    function name and the argument list for number
    and type of arguments.
  • The function that is chosen is the function with
    the same number of parameters as the number of
    arguments and and that matches the types of the
    parameter list sufficiently well.
  • This means you do not have to generate names for
    functions that have very much the same task, but
    have different types.

38
3.6 Overloading Function NamesDisplay 3.15
Overloading a Function Name (1 of 2)
  • //Illustrates overloading the function name ave.
  • include ltiostreamgt
  • double ave(double n1, double n2)
  • //Returns the average of the two numbers n1 and
    n2.
  • double ave(double n1, double n2, double n3)
  • //Returns the average of the three numbers n1,
    n2, and n3.
  • int main( )
  • using namespace std
  • cout ltlt "The average of 2.0, 2.5, and 3.0 is
    "
  • ltlt ave(2.0, 2.5, 3.0) ltlt endl
  • cout ltlt "The average of 4.5 and 5.5 is "
  • ltlt ave(4.5, 5.5) ltlt endl
  • return 0

39
3.6 Overloading Function NamesDisplay 3.15
Overloading a Function Name (2 of 2)
  • double ave(double n1, double n2) Both these
    functions have the

  • same name, but have parameter
  • return ((n1 n2)/2.0)
    lists that are have different

  • numbers of parameters.
  • double ave(double n1, double n2, double n3)
  • return ((n1 n2 n3)/3.0)

40
3.6 Overloading Function NamesAutomatic Type
Conversion
  • We pointed out that when overloading function
    names, the C compiler compares the number and
    sequence of types of the arguments to the number
    and sequence of types for candidate functions.
  • In choosing which of several candidates for use
    when overloading function names, the compiler
    will choose an exact match if one if available.
  • An integral type will be promoted to a larger
    integral type if necessary to find a match. An
    integral type will be promoted to a floating
    point type if necessary to get a match.

41
3.6 Overloading Function NamesDisplay 3.16
Overloading a function name (1 of 4)
  • //Determines whether a round pizza or a
    rectangular pizza is the best buy.
  • include ltiostreamgt
  • double unitprice(int diameter, double price)
  • //Returns the price per square inch of a round
    pizza.
  • //The formal parameter named diameter is the
    diameter of the pizza
  • //in inches. The formal parameter named price is
    the price of the pizza.
  • double unitprice(int length, int width, double
    price)
  • //Returns the price per square inch of a
    rectangular pizza
  • //with dimensions length by width inches.
  • //The formal parameter price is the price of the
    pizza.
  • int main( )
  • using namespace std
  • int diameter, length, width
  • double price_round, unit_price_round,
  • price_rectangular,
    unitprice_rectangular

42
3.6 Overloading Function NamesDisplay 3.16
Overloading a function name (2 of 4)
  • cout ltlt "Welcome to the Pizza Consumers
    Union.\n"
  • cout ltlt "Enter the diameter in inches"
  • ltlt " of a round pizza "
  • cin gtgt diameter
  • cout ltlt "Enter the price of a round pizza
    "
  • cin gtgt price_round
  • cout ltlt "Enter length and width in inches\n"
  • ltlt "of a rectangular pizza "
  • cin gtgt length gtgt width
  • cout ltlt "Enter the price of a rectangular
    pizza "
  • cin gtgt price_rectangular
  • unitprice_rectangular unitprice(length,
    width, price_rectangular)
  • unit_price_round
    unitprice(diameter, price_round)
  • cout.setf(iosfixed)
  • cout.setf(iosshowpoint)
  • cout.precision(2)

43
3.6 Overloading Function NamesDisplay 3.16
Overloading a function name (3 of 4)
  • cout ltlt endl
  • ltlt "Round pizza Diameter "
  • ltlt diameter ltlt " inches\n"
  • ltlt "Price " ltlt price_round
  • ltlt " Per square inch " ltlt
    unit_price_round
  • ltlt endl
  • ltlt "Rectangular pizza length "
  • ltlt length ltlt " inches\n"
  • ltlt "Rectangular pizza Width "
  • ltlt width ltlt " inches\n"
  • ltlt "Price " ltlt price_rectangular
  • ltlt " Per square inch " ltlt
    unitprice_rectangular
  • ltlt endl
  • if (unit_price_round lt unitprice_rectangular)
  • cout ltlt "The round one is the better
    buy.\n"
  • else
  • cout ltlt "The rectangular one is the
    better buy.\n"
  • cout ltlt "Buon Appetito!\n"

44
Chapter Summary( 1 of 2)
  • A good plan of attack on a problem is to
    decompose the problem into smaller, more
    accessible problems, the decompose these into
    still more manageable problems. This is known as
    Top-Down Design.
  • A function that returns value is like a small
    program. Arguments provide input and the return
    value provides the output.
  • When a subtask for a program takes some values as
    input and produces a single value as its only
    output, then that subtask can be implemented as a
    function.
  • A function should be defied so that it can be
    used as a black box. The program author should
    know no more than the specification of the client
    use, and the client author should know nothing
    more than the specification of the implementation
    of the function. This rule is the principle of
    procedural abstraction.
  • A variable that is defined in a function is said
    to be local to the function.
  • Global named constants are declared using the
    const modifier. Declarations of global named
    constants are normally placed at the start of a
    program after the include directives, before the
    function prototypes.

45
Chapter Summary (2 of 2)
  • Call by value formal parameters (the only kind we
    have discussed so far) are local variables to the
    function. Occasionally a formal parameter may be
    useful as a local variable.
  • When two or more function definitions have the
    same name, this is called function name
    overloading. When you overload a name, the
    function definitions must have different numbers
    of formal parameters or formal parameters of
    different types.
Write a Comment
User Comments (0)
About PowerShow.com