Download presentation
Presentation is loading. Please wait.
Published byIvo Brož Modified over 5 years ago
1
Computer Science 1 3.14 And he made a molten sea, ten cubits from the one brim to the other: it was round all about, and his height was five cubits: and a line of thirty cubits did compass it about. (I Kings 7: 23) 950 BC
2
Learning Targets Be able to read a program that uses functions.
Be able to write a program that uses functions.
3
Dry Run the following begin a:= 6; b:= 12; writeln(a:4,b:4);
program funInFunction; var a,b:integer; function one(a,b:integer):integer; c:integer; begin c:= a+2*b; one:= c DIV 2; end; function two:integer; first, second:integer; first:= 15; second:= 10; two:= first - second; Dry Run the following begin a:= 6; b:= 12; writeln(a:4,b:4); a:= one(b,a); writeln(two:4); b:= one(a,b) + two - 6; writeln(a:4,b:4 ); end.
4
Functions Program #1 Write a function to convert from Radians to degrees Convert from Fahrenheit to Celsius Sent the radius and height and calculate the volume of a cylinder.
5
Function #2 Program Options
Sent nothing, but inside the functions gets 10 scores and calculates and returns the average Factorial Function Input: Any positive integer; Output: The factorial of the number Example: Input: 5 Output: 5! = 120 Create the header and call statement for one.
6
Function #2 Program Options
Sent nothing, but inside the functions gets 10 scores and calculates and returns the average Factorial Function Input: Any positive integer; Output: The factorial of the number Example: Input: 5 Output: 5! = 120 Create the header and call statement for one.
7
Program 3: Today there are 3 PI themed options
Describe each methods Give an example on how each works Online time to generate the code to create a function to calculate PI. At the end of class on Monday, EPIC ticket to the person whose code calculates the most digits of PI.
8
PI Memorizers
9
How many digits of PI can you recite?
10
Write a program to approximate PI
1400 method (Madhana of Sangamagrama, and rediscovered by Leibniz in the 1600s) Use this method to estimate PI. What value do you get after 500 iterations, 1000, 10000, …? Push: How could you write a program to use this method to calculate PI to +/- 1/1000
11
Wallis Method (1655) Use this method to estimate PI. What value do you get after 500 iterations, 1000, 10000, …? Push: How could you write a program to use this method to calculate PI to +/- 1/1000
12
Random Circle Area Method
Using a 100 pixel x100 pixel square with a circle inscribed with a 50 pixel radius. Generate random points (x, y) inside the square. ( 0 <=x <=100, 0<=y<=100). Count how many of these points are inside the circle and use this to estimate the value of PI. Try with 500 points, 1000 points, points … Push: Show graphically Push: Compare with Squares and Circles of different sizes to see if it makes a difference. (Dots in)/(total Dots) = (Circle Area)/(Total Area) = (PI*r2)/(2*r)2 = (PI*r2)/(4*r2) = PI/4 Multiplying both sides by 4 4*(Dots in)/(total Dots) = PI
13
Function Programs Function Program #2: PI 1) 2)
Write a function to convert from Radians to degrees Convert from Fahrenheit to Celsius Sent the radius and height and calculate the volume of a cylinder. Function Program #2 Sent nothing, but inside the functions gets 10 scores and calculates and returns the average Factorial Function Input: Any positive integer; Output: The factorial of the number Example: Input: 5 Output: 5! = 120 Function Program #2: PI 1) 2) 3) (Dots in)/(total Dots) = (Circle Area)/(Total Area) = (PI*r2)/(2*r)2 = (PI*r2)/(4*r2) = PI/4 Multiplying both sides by 4 4*(Dots in)/(total Dots) = PI
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.