On this page:
2.3.1 Objectives
2.3.2 Exercises (due 2011/  09/  14 08:  45:  00)
2.3.3 Optional Exercises (due 2011/  09/  19 08:  45:  00)
2.3.4 Notes

2.3 Function Composition and Conditionals

A fork in the path

Data dependency: found

Which shall I follow?

2.3.1 Objectives

At the end of this class, you should know:

At the end of this class, you should be able:

2.3.2 Exercises (due 2011/09/14 08:45:00)

1. Write a function called sign that consumes a number, and returns one of the words positive, negative, or zero depending on the sign of the number. For example, sign(-2) would return negative.

2. Develop the function areaOfTube. It computes the surface area of a tube, which is an open cylinder. The program consumes three values: the tube’s inner radius, its length, and the thickness of its wall. [Use Google if you don’t know the equation.] You should write at least two auxiliary functions.

3. Develop the program height, which computes the height that a rocket reaches in a given amount of time. If the rocket accelerates at a constant rate g (= 2m/s^2), it reaches a speed of g * t in t time units and a height of 1/2 * v * t where v is the speed at t. You should write an auxiliary function.

4. Recall fahrenheitToCelsius, write celsiusToFahrenheit.

Now consider the function:

// I : double -> double

// to convert a Fahrenheit temperature to Celsius and back

double I ( double f ) {

       return celsiusToFahrenheit( fahrenheitToCelsius( f ) );

}

Evaluate I( 32.0 ) by hand. What does this suggest about the composition of the two functions?

2.3.3 Optional Exercises (due 2011/09/19 08:45:00)

5. Develop areaOfCylinder. The program consumes the radius of the cylinder’s base disk and its height. Its result is the surface area of the cylinder. [Use Google if you don’t know the equation.] You should write a few auxiliary functions.

6. Develop milesToFeet. The program should be a composition of at least three other functions that convert from miles to other Imperial units, like furlongs, rods, or yards.

2.3.4 Notes

These notes are primarily for my sake, but I don’t see any reason to hide them from you.

 

Guess: Divide by 0

 

Programs are many definitions

Functions compose with functions

- the one we really want... the main one

- the auxiliary or helpers

 

Compare un-composed areaOfRing

- no divide and conquer or abstractions

 

Let's experiment with a problem:

 

"Imagine the owner of a movie theater who has complete freedom in setting ticket prices. The more he charges, the fewer the people who can afford tickets. In a recent experiment the owner determined a precise relationship between the price of a ticket and average attendance. At a price of $5.00 per ticket, 120 people attend a performance. Decreasing the price by a dime ($.10) increases attendance by 15. Unfortunately, the increased attendance also comes at an increased cost. Every performance costs the owner $180. Each attendee costs another four cents ($0.04). The owner would like to know the exact relationship between profit and ticket price so that he can determine the price at which he can make the highest profit."

 

What information?

- profits, revenue, costs, attendees

 

What data?

 

What depends on what?

 

Write function headers, contracts, and purposes

 

Find numbers of attendees for $3, $4, and $5 tickets. What is an equation for this?

 

How much does it cost to run the show at $3, 4, and 5?

 

What should we charge?

 

What if there were no fixed costs, but each attendee cost $1.5?

 

Give names to all the constants

 

drop static when naming values inside functions

 

make a new function to find the best price of three options

- using if and strings