54
Function Examples Lecture 19 Robb T. Koether Hampden-Sydney College Fri, Oct 11, 2019 Robb T. Koether (Hampden-Sydney College) Function Examples Fri, Oct 11, 2019 1 / 33

Lecture 19 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019/Lecture … · Lecture 19 Robb T. Koether Hampden-Sydney College Fri, Oct

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Lecture 19 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019/Lecture … · Lecture 19 Robb T. Koether Hampden-Sydney College Fri, Oct

Function ExamplesLecture 19

Robb T. Koether

Hampden-Sydney College

Fri, Oct 11, 2019

Robb T. Koether (Hampden-Sydney College) Function Examples Fri, Oct 11, 2019 1 / 33

Page 2: Lecture 19 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019/Lecture … · Lecture 19 Robb T. Koether Hampden-Sydney College Fri, Oct

1 Convert Fahrenheit to CelsiusThe ProblemThe Input and OutputAn ExampleThe AlgorithmThe Code

2 Find the Dimensions of a RectangleThe ProblemThe Input and OutputAn ExampleThe AlgorithmThe Code

Robb T. Koether (Hampden-Sydney College) Function Examples Fri, Oct 11, 2019 2 / 33

Page 3: Lecture 19 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019/Lecture … · Lecture 19 Robb T. Koether Hampden-Sydney College Fri, Oct

Outline

1 Convert Fahrenheit to CelsiusThe ProblemThe Input and OutputAn ExampleThe AlgorithmThe Code

2 Find the Dimensions of a RectangleThe ProblemThe Input and OutputAn ExampleThe AlgorithmThe Code

Robb T. Koether (Hampden-Sydney College) Function Examples Fri, Oct 11, 2019 3 / 33

Page 4: Lecture 19 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019/Lecture … · Lecture 19 Robb T. Koether Hampden-Sydney College Fri, Oct

Outline

1 Convert Fahrenheit to CelsiusThe ProblemThe Input and OutputAn ExampleThe AlgorithmThe Code

2 Find the Dimensions of a RectangleThe ProblemThe Input and OutputAn ExampleThe AlgorithmThe Code

Robb T. Koether (Hampden-Sydney College) Function Examples Fri, Oct 11, 2019 4 / 33

Page 5: Lecture 19 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019/Lecture … · Lecture 19 Robb T. Koether Hampden-Sydney College Fri, Oct

The Problem

The ProblemWrite a program that will

Compute the equivalent Celsius temperature of each Fahrenheittemperature within a specified range.Print the Fahrenheit temperature in that range and its Celsiusequivalent.Round the Celsius temperature to the nearest 10th of a degree.

Robb T. Koether (Hampden-Sydney College) Function Examples Fri, Oct 11, 2019 5 / 33

Page 6: Lecture 19 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019/Lecture … · Lecture 19 Robb T. Koether Hampden-Sydney College Fri, Oct

Outline

1 Convert Fahrenheit to CelsiusThe ProblemThe Input and OutputAn ExampleThe AlgorithmThe Code

2 Find the Dimensions of a RectangleThe ProblemThe Input and OutputAn ExampleThe AlgorithmThe Code

Robb T. Koether (Hampden-Sydney College) Function Examples Fri, Oct 11, 2019 6 / 33

Page 7: Lecture 19 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019/Lecture … · Lecture 19 Robb T. Koether Hampden-Sydney College Fri, Oct

The Input and Output

Input: the starting temperature and the ending temperature, inFahrenheit.Output: each Fahrenheit temperature in the range, its Celsiusequivalent, rounded to the nearest 0.1.

Robb T. Koether (Hampden-Sydney College) Function Examples Fri, Oct 11, 2019 7 / 33

Page 8: Lecture 19 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019/Lecture … · Lecture 19 Robb T. Koether Hampden-Sydney College Fri, Oct

Outline

1 Convert Fahrenheit to CelsiusThe ProblemThe Input and OutputAn ExampleThe AlgorithmThe Code

2 Find the Dimensions of a RectangleThe ProblemThe Input and OutputAn ExampleThe AlgorithmThe Code

Robb T. Koether (Hampden-Sydney College) Function Examples Fri, Oct 11, 2019 8 / 33

Page 9: Lecture 19 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019/Lecture … · Lecture 19 Robb T. Koether Hampden-Sydney College Fri, Oct

Example

Let the starting temperature be 65◦ F and the ending temperature70◦ F.

We must compute the Celsius equivalent of 65, 66, 67, 68, 69,and 70 F.For 65 F, the calculation is

temp in C = (65− 32) · 5/9= 33 · 5/9= 165/9= 18.3333 . . .

The output should be 65 and 18.3.Do the same for the other temperatures.

Robb T. Koether (Hampden-Sydney College) Function Examples Fri, Oct 11, 2019 9 / 33

Page 10: Lecture 19 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019/Lecture … · Lecture 19 Robb T. Koether Hampden-Sydney College Fri, Oct

Example

Let the starting temperature be 65◦ F and the ending temperature70◦ F.We must compute the Celsius equivalent of 65, 66, 67, 68, 69,and 70 F.

For 65 F, the calculation is

temp in C = (65− 32) · 5/9= 33 · 5/9= 165/9= 18.3333 . . .

The output should be 65 and 18.3.Do the same for the other temperatures.

Robb T. Koether (Hampden-Sydney College) Function Examples Fri, Oct 11, 2019 9 / 33

Page 11: Lecture 19 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019/Lecture … · Lecture 19 Robb T. Koether Hampden-Sydney College Fri, Oct

Example

Let the starting temperature be 65◦ F and the ending temperature70◦ F.We must compute the Celsius equivalent of 65, 66, 67, 68, 69,and 70 F.For 65 F, the calculation is

temp in C = (65− 32) · 5/9= 33 · 5/9= 165/9= 18.3333 . . .

The output should be 65 and 18.3.Do the same for the other temperatures.

Robb T. Koether (Hampden-Sydney College) Function Examples Fri, Oct 11, 2019 9 / 33

Page 12: Lecture 19 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019/Lecture … · Lecture 19 Robb T. Koether Hampden-Sydney College Fri, Oct

Example

Let the starting temperature be 65◦ F and the ending temperature70◦ F.We must compute the Celsius equivalent of 65, 66, 67, 68, 69,and 70 F.For 65 F, the calculation is

temp in C = (65− 32) · 5/9= 33 · 5/9= 165/9= 18.3333 . . .

The output should be 65 and 18.3.

Do the same for the other temperatures.

Robb T. Koether (Hampden-Sydney College) Function Examples Fri, Oct 11, 2019 9 / 33

Page 13: Lecture 19 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019/Lecture … · Lecture 19 Robb T. Koether Hampden-Sydney College Fri, Oct

Example

Let the starting temperature be 65◦ F and the ending temperature70◦ F.We must compute the Celsius equivalent of 65, 66, 67, 68, 69,and 70 F.For 65 F, the calculation is

temp in C = (65− 32) · 5/9= 33 · 5/9= 165/9= 18.3333 . . .

The output should be 65 and 18.3.Do the same for the other temperatures.

Robb T. Koether (Hampden-Sydney College) Function Examples Fri, Oct 11, 2019 9 / 33

Page 14: Lecture 19 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019/Lecture … · Lecture 19 Robb T. Koether Hampden-Sydney College Fri, Oct

Outline

1 Convert Fahrenheit to CelsiusThe ProblemThe Input and OutputAn ExampleThe AlgorithmThe Code

2 Find the Dimensions of a RectangleThe ProblemThe Input and OutputAn ExampleThe AlgorithmThe Code

Robb T. Koether (Hampden-Sydney College) Function Examples Fri, Oct 11, 2019 10 / 33

Page 15: Lecture 19 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019/Lecture … · Lecture 19 Robb T. Koether Hampden-Sydney College Fri, Oct

The Algorithm

Begin with the starting temperature. Call it “fahr_temp.”

Subtract 32 from fahr_temp and multiply by 5/9.Round off the result to one decimal place. Call it cels_temp.Output fahr_temp and cels_temp.Add 1 to fahr_temp.Repeat the previous three steps until fahr_temp exceeds theending temperature.

Robb T. Koether (Hampden-Sydney College) Function Examples Fri, Oct 11, 2019 11 / 33

Page 16: Lecture 19 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019/Lecture … · Lecture 19 Robb T. Koether Hampden-Sydney College Fri, Oct

The Algorithm

Begin with the starting temperature. Call it “fahr_temp.”Subtract 32 from fahr_temp and multiply by 5/9.

Round off the result to one decimal place. Call it cels_temp.Output fahr_temp and cels_temp.Add 1 to fahr_temp.Repeat the previous three steps until fahr_temp exceeds theending temperature.

Robb T. Koether (Hampden-Sydney College) Function Examples Fri, Oct 11, 2019 11 / 33

Page 17: Lecture 19 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019/Lecture … · Lecture 19 Robb T. Koether Hampden-Sydney College Fri, Oct

The Algorithm

Begin with the starting temperature. Call it “fahr_temp.”Subtract 32 from fahr_temp and multiply by 5/9.Round off the result to one decimal place. Call it cels_temp.

Output fahr_temp and cels_temp.Add 1 to fahr_temp.Repeat the previous three steps until fahr_temp exceeds theending temperature.

Robb T. Koether (Hampden-Sydney College) Function Examples Fri, Oct 11, 2019 11 / 33

Page 18: Lecture 19 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019/Lecture … · Lecture 19 Robb T. Koether Hampden-Sydney College Fri, Oct

The Algorithm

Begin with the starting temperature. Call it “fahr_temp.”Subtract 32 from fahr_temp and multiply by 5/9.Round off the result to one decimal place. Call it cels_temp.Output fahr_temp and cels_temp.

Add 1 to fahr_temp.Repeat the previous three steps until fahr_temp exceeds theending temperature.

Robb T. Koether (Hampden-Sydney College) Function Examples Fri, Oct 11, 2019 11 / 33

Page 19: Lecture 19 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019/Lecture … · Lecture 19 Robb T. Koether Hampden-Sydney College Fri, Oct

The Algorithm

Begin with the starting temperature. Call it “fahr_temp.”Subtract 32 from fahr_temp and multiply by 5/9.Round off the result to one decimal place. Call it cels_temp.Output fahr_temp and cels_temp.Add 1 to fahr_temp.

Repeat the previous three steps until fahr_temp exceeds theending temperature.

Robb T. Koether (Hampden-Sydney College) Function Examples Fri, Oct 11, 2019 11 / 33

Page 20: Lecture 19 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019/Lecture … · Lecture 19 Robb T. Koether Hampden-Sydney College Fri, Oct

The Algorithm

Begin with the starting temperature. Call it “fahr_temp.”Subtract 32 from fahr_temp and multiply by 5/9.Round off the result to one decimal place. Call it cels_temp.Output fahr_temp and cels_temp.Add 1 to fahr_temp.Repeat the previous three steps until fahr_temp exceeds theending temperature.

Robb T. Koether (Hampden-Sydney College) Function Examples Fri, Oct 11, 2019 11 / 33

Page 21: Lecture 19 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019/Lecture … · Lecture 19 Robb T. Koether Hampden-Sydney College Fri, Oct

Outline

1 Convert Fahrenheit to CelsiusThe ProblemThe Input and OutputAn ExampleThe AlgorithmThe Code

2 Find the Dimensions of a RectangleThe ProblemThe Input and OutputAn ExampleThe AlgorithmThe Code

Robb T. Koether (Hampden-Sydney College) Function Examples Fri, Oct 11, 2019 12 / 33

Page 22: Lecture 19 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019/Lecture … · Lecture 19 Robb T. Koether Hampden-Sydney College Fri, Oct

The Code

We will use two functions.float Fahr2Cels(int fahr_temp);float round(float value, int places);

The function Fahr2Cels() will convert the Fahrenheittemperature and return its Celsius equivalent, rounded to onedecimal place.The function round() will round the first parameter to the numberof decimal places specified by the second parameter.

Robb T. Koether (Hampden-Sydney College) Function Examples Fri, Oct 11, 2019 13 / 33

Page 23: Lecture 19 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019/Lecture … · Lecture 19 Robb T. Koether Hampden-Sydney College Fri, Oct

The Code

Clearly, we should use a for loop to step through the Fahrenheittemperatures.For each temperature, we will call on the Fahr2Cels() functionto get the Celsius equivalent.Then output the two temperatures.

Robb T. Koether (Hampden-Sydney College) Function Examples Fri, Oct 11, 2019 14 / 33

Page 24: Lecture 19 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019/Lecture … · Lecture 19 Robb T. Koether Hampden-Sydney College Fri, Oct

The Code

The Code – main()int main(){

int start;int end;cin >> start >> end;for (int fahr_temp = start; fahr_temp <= end; fahr_temp++){

float cels_temp = Fahr2Cels(fahr_temp);cout << fahr_temp << " " << cels_temp << endl;

}system("pause");return 0;

}

Robb T. Koether (Hampden-Sydney College) Function Examples Fri, Oct 11, 2019 15 / 33

Page 25: Lecture 19 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019/Lecture … · Lecture 19 Robb T. Koether Hampden-Sydney College Fri, Oct

The Code

The Code – Fahr2Cels()float Fahr2Cels(int fahr_temp){

float cels_temp = (fahr_temp - 32)*5.0/9.0;return round(cels_temp, 1);

}

Robb T. Koether (Hampden-Sydney College) Function Examples Fri, Oct 11, 2019 16 / 33

Page 26: Lecture 19 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019/Lecture … · Lecture 19 Robb T. Koether Hampden-Sydney College Fri, Oct

The Code

The Code – round()float round(float value, int places){

float shift = power(10.0, places);float temp = value*shift;temp = round(temp)/shift;return temp;

}

Robb T. Koether (Hampden-Sydney College) Function Examples Fri, Oct 11, 2019 17 / 33

Page 27: Lecture 19 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019/Lecture … · Lecture 19 Robb T. Koether Hampden-Sydney College Fri, Oct

Outline

1 Convert Fahrenheit to CelsiusThe ProblemThe Input and OutputAn ExampleThe AlgorithmThe Code

2 Find the Dimensions of a RectangleThe ProblemThe Input and OutputAn ExampleThe AlgorithmThe Code

Robb T. Koether (Hampden-Sydney College) Function Examples Fri, Oct 11, 2019 18 / 33

Page 28: Lecture 19 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019/Lecture … · Lecture 19 Robb T. Koether Hampden-Sydney College Fri, Oct

Outline

1 Convert Fahrenheit to CelsiusThe ProblemThe Input and OutputAn ExampleThe AlgorithmThe Code

2 Find the Dimensions of a RectangleThe ProblemThe Input and OutputAn ExampleThe AlgorithmThe Code

Robb T. Koether (Hampden-Sydney College) Function Examples Fri, Oct 11, 2019 19 / 33

Page 29: Lecture 19 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019/Lecture … · Lecture 19 Robb T. Koether Hampden-Sydney College Fri, Oct

The Problem

The ProblemGiven the area and perimeter of a rectangle, find the length andwidth of that rectangle.

Robb T. Koether (Hampden-Sydney College) Function Examples Fri, Oct 11, 2019 20 / 33

Page 30: Lecture 19 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019/Lecture … · Lecture 19 Robb T. Koether Hampden-Sydney College Fri, Oct

Outline

1 Convert Fahrenheit to CelsiusThe ProblemThe Input and OutputAn ExampleThe AlgorithmThe Code

2 Find the Dimensions of a RectangleThe ProblemThe Input and OutputAn ExampleThe AlgorithmThe Code

Robb T. Koether (Hampden-Sydney College) Function Examples Fri, Oct 11, 2019 21 / 33

Page 31: Lecture 19 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019/Lecture … · Lecture 19 Robb T. Koether Hampden-Sydney College Fri, Oct

The Input and Output

The ProblemThe input is the area and perimeter of the rectangle.The output is the length and width of the rectangle.

Robb T. Koether (Hampden-Sydney College) Function Examples Fri, Oct 11, 2019 22 / 33

Page 32: Lecture 19 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019/Lecture … · Lecture 19 Robb T. Koether Hampden-Sydney College Fri, Oct

Outline

1 Convert Fahrenheit to CelsiusThe ProblemThe Input and OutputAn ExampleThe AlgorithmThe Code

2 Find the Dimensions of a RectangleThe ProblemThe Input and OutputAn ExampleThe AlgorithmThe Code

Robb T. Koether (Hampden-Sydney College) Function Examples Fri, Oct 11, 2019 23 / 33

Page 33: Lecture 19 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019/Lecture … · Lecture 19 Robb T. Koether Hampden-Sydney College Fri, Oct

Example

ExampleLet the area be 24 and the perimeter be 20.

Let the length be L and the width be W .Then LW = 24 and L + W = 10 (half the perimeter).How do we find L and W?

Robb T. Koether (Hampden-Sydney College) Function Examples Fri, Oct 11, 2019 24 / 33

Page 34: Lecture 19 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019/Lecture … · Lecture 19 Robb T. Koether Hampden-Sydney College Fri, Oct

Example

ExampleLet the area be 24 and the perimeter be 20.Let the length be L and the width be W .

Then LW = 24 and L + W = 10 (half the perimeter).How do we find L and W?

Robb T. Koether (Hampden-Sydney College) Function Examples Fri, Oct 11, 2019 24 / 33

Page 35: Lecture 19 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019/Lecture … · Lecture 19 Robb T. Koether Hampden-Sydney College Fri, Oct

Example

ExampleLet the area be 24 and the perimeter be 20.Let the length be L and the width be W .Then LW = 24 and L + W = 10 (half the perimeter).

How do we find L and W?

Robb T. Koether (Hampden-Sydney College) Function Examples Fri, Oct 11, 2019 24 / 33

Page 36: Lecture 19 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019/Lecture … · Lecture 19 Robb T. Koether Hampden-Sydney College Fri, Oct

Example

ExampleLet the area be 24 and the perimeter be 20.Let the length be L and the width be W .Then LW = 24 and L + W = 10 (half the perimeter).How do we find L and W?

Robb T. Koether (Hampden-Sydney College) Function Examples Fri, Oct 11, 2019 24 / 33

Page 37: Lecture 19 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019/Lecture … · Lecture 19 Robb T. Koether Hampden-Sydney College Fri, Oct

Example

ExampleSolve the equations for L by eliminating W .

We have W = 10− L, so

24 = L(10− L)

24 = 10L− L2

L2 − 10L + 24 = 0.

We can factor that equation as

(L− 4)(L− 6) = 0.

Thus, L = 4 or L = 6.And so W = 6 or W = 4.

Robb T. Koether (Hampden-Sydney College) Function Examples Fri, Oct 11, 2019 25 / 33

Page 38: Lecture 19 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019/Lecture … · Lecture 19 Robb T. Koether Hampden-Sydney College Fri, Oct

Example

ExampleSolve the equations for L by eliminating W .We have W = 10− L, so

24 = L(10− L)

24 = 10L− L2

L2 − 10L + 24 = 0.

We can factor that equation as

(L− 4)(L− 6) = 0.

Thus, L = 4 or L = 6.And so W = 6 or W = 4.

Robb T. Koether (Hampden-Sydney College) Function Examples Fri, Oct 11, 2019 25 / 33

Page 39: Lecture 19 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019/Lecture … · Lecture 19 Robb T. Koether Hampden-Sydney College Fri, Oct

Example

ExampleSolve the equations for L by eliminating W .We have W = 10− L, so

24 = L(10− L)

24 = 10L− L2

L2 − 10L + 24 = 0.

We can factor that equation as

(L− 4)(L− 6) = 0.

Thus, L = 4 or L = 6.And so W = 6 or W = 4.

Robb T. Koether (Hampden-Sydney College) Function Examples Fri, Oct 11, 2019 25 / 33

Page 40: Lecture 19 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019/Lecture … · Lecture 19 Robb T. Koether Hampden-Sydney College Fri, Oct

Example

ExampleSolve the equations for L by eliminating W .We have W = 10− L, so

24 = L(10− L)

24 = 10L− L2

L2 − 10L + 24 = 0.

We can factor that equation as

(L− 4)(L− 6) = 0.

Thus, L = 4 or L = 6.

And so W = 6 or W = 4.

Robb T. Koether (Hampden-Sydney College) Function Examples Fri, Oct 11, 2019 25 / 33

Page 41: Lecture 19 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019/Lecture … · Lecture 19 Robb T. Koether Hampden-Sydney College Fri, Oct

Example

ExampleSolve the equations for L by eliminating W .We have W = 10− L, so

24 = L(10− L)

24 = 10L− L2

L2 − 10L + 24 = 0.

We can factor that equation as

(L− 4)(L− 6) = 0.

Thus, L = 4 or L = 6.And so W = 6 or W = 4.

Robb T. Koether (Hampden-Sydney College) Function Examples Fri, Oct 11, 2019 25 / 33

Page 42: Lecture 19 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019/Lecture … · Lecture 19 Robb T. Koether Hampden-Sydney College Fri, Oct

Example

ExampleIn the example, we solved the quadratic equation

L2 − 10L + 24 = 0

by factoring it.To solve it in a program, we should use the quadratic formula:

L =10±

√102 − 4(1)(24)

2(1)

=10±

√4

2= 5± 1= 4 or 6.

Robb T. Koether (Hampden-Sydney College) Function Examples Fri, Oct 11, 2019 26 / 33

Page 43: Lecture 19 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019/Lecture … · Lecture 19 Robb T. Koether Hampden-Sydney College Fri, Oct

Outline

1 Convert Fahrenheit to CelsiusThe ProblemThe Input and OutputAn ExampleThe AlgorithmThe Code

2 Find the Dimensions of a RectangleThe ProblemThe Input and OutputAn ExampleThe AlgorithmThe Code

Robb T. Koether (Hampden-Sydney College) Function Examples Fri, Oct 11, 2019 27 / 33

Page 44: Lecture 19 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019/Lecture … · Lecture 19 Robb T. Koether Hampden-Sydney College Fri, Oct

The Algorithm

The AlgorithmRead the area and the perimeter.

Let b be half the perimeter and c be the area.Solve the quadratic equation

x2 − bx + c = 0.

The solutions are

L =−b ±

√b2 − 4c

2.

Find W = area/L.Output @ and L.

Robb T. Koether (Hampden-Sydney College) Function Examples Fri, Oct 11, 2019 28 / 33

Page 45: Lecture 19 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019/Lecture … · Lecture 19 Robb T. Koether Hampden-Sydney College Fri, Oct

The Algorithm

The AlgorithmRead the area and the perimeter.Let b be half the perimeter and c be the area.

Solve the quadratic equation

x2 − bx + c = 0.

The solutions are

L =−b ±

√b2 − 4c

2.

Find W = area/L.Output @ and L.

Robb T. Koether (Hampden-Sydney College) Function Examples Fri, Oct 11, 2019 28 / 33

Page 46: Lecture 19 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019/Lecture … · Lecture 19 Robb T. Koether Hampden-Sydney College Fri, Oct

The Algorithm

The AlgorithmRead the area and the perimeter.Let b be half the perimeter and c be the area.Solve the quadratic equation

x2 − bx + c = 0.

The solutions are

L =−b ±

√b2 − 4c

2.

Find W = area/L.Output @ and L.

Robb T. Koether (Hampden-Sydney College) Function Examples Fri, Oct 11, 2019 28 / 33

Page 47: Lecture 19 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019/Lecture … · Lecture 19 Robb T. Koether Hampden-Sydney College Fri, Oct

The Algorithm

The AlgorithmRead the area and the perimeter.Let b be half the perimeter and c be the area.Solve the quadratic equation

x2 − bx + c = 0.

The solutions are

L =−b ±

√b2 − 4c

2.

Find W = area/L.Output @ and L.

Robb T. Koether (Hampden-Sydney College) Function Examples Fri, Oct 11, 2019 28 / 33

Page 48: Lecture 19 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019/Lecture … · Lecture 19 Robb T. Koether Hampden-Sydney College Fri, Oct

The Algorithm

The AlgorithmRead the area and the perimeter.Let b be half the perimeter and c be the area.Solve the quadratic equation

x2 − bx + c = 0.

The solutions are

L =−b ±

√b2 − 4c

2.

Find W = area/L.

Output @ and L.

Robb T. Koether (Hampden-Sydney College) Function Examples Fri, Oct 11, 2019 28 / 33

Page 49: Lecture 19 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019/Lecture … · Lecture 19 Robb T. Koether Hampden-Sydney College Fri, Oct

The Algorithm

The AlgorithmRead the area and the perimeter.Let b be half the perimeter and c be the area.Solve the quadratic equation

x2 − bx + c = 0.

The solutions are

L =−b ±

√b2 − 4c

2.

Find W = area/L.Output @ and L.

Robb T. Koether (Hampden-Sydney College) Function Examples Fri, Oct 11, 2019 28 / 33

Page 50: Lecture 19 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019/Lecture … · Lecture 19 Robb T. Koether Hampden-Sydney College Fri, Oct

Outline

1 Convert Fahrenheit to CelsiusThe ProblemThe Input and OutputAn ExampleThe AlgorithmThe Code

2 Find the Dimensions of a RectangleThe ProblemThe Input and OutputAn ExampleThe AlgorithmThe Code

Robb T. Koether (Hampden-Sydney College) Function Examples Fri, Oct 11, 2019 29 / 33

Page 51: Lecture 19 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019/Lecture … · Lecture 19 Robb T. Koether Hampden-Sydney College Fri, Oct

The Code

The CodeWe will break the program up into functions.

int main()void findDims(float area, float perim,float& len, float& wid)void solveQuadEq(float a, float b, float c,float& root1, float& root2)

Robb T. Koether (Hampden-Sydney College) Function Examples Fri, Oct 11, 2019 30 / 33

Page 52: Lecture 19 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019/Lecture … · Lecture 19 Robb T. Koether Hampden-Sydney College Fri, Oct

The Code

The Code – main()int main(){

float area;float perim;cin >> area >> perim;float length;float width;findDims(area, perim, length, width);cout << "Length = " << length;cout << ", width = " << width << endl;return 0;

}

Robb T. Koether (Hampden-Sydney College) Function Examples Fri, Oct 11, 2019 31 / 33

Page 53: Lecture 19 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019/Lecture … · Lecture 19 Robb T. Koether Hampden-Sydney College Fri, Oct

The Code

The Code – findDims()void findDims(float area, float perim, float& len, float& wid){

solveQuadEq(1.0, -perim/2.0, area, len, wid);return;

}

Robb T. Koether (Hampden-Sydney College) Function Examples Fri, Oct 11, 2019 32 / 33

Page 54: Lecture 19 Robb T. Koether - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2019/Lecture … · Lecture 19 Robb T. Koether Hampden-Sydney College Fri, Oct

The Code

The Code – solveQuadEq()void solveQuadEq(float a, float b, float c, float& root1,

float& root2){

float discr = sqrt(b*b - 4.0*a*c);root1 = (-b + discr)/(2.0*a);root2 = (-b - discr)/(2.0*a);return;

}

Robb T. Koether (Hampden-Sydney College) Function Examples Fri, Oct 11, 2019 33 / 33