160 likes | 296 Vues
Quiz 1. What is the Output of this code fragment when N = 85 (3 Marks) [5 Minutes] A = N 10 B = N Mod 10 C = 0 If (N Mod 2 = 0) Then C = C + 1 Else C = C + 2 End If If (A > 5) And (B > 5) Then C = C + 3 ElseIf (A > 5) Or (B > 5) Then C = C + 5 Else C = C + 6
E N D
What is the Output of this code fragment when N = 85 (3 Marks) [5 Minutes] A = N \ 10 B = N Mod 10 C = 0 If (N Mod 2 = 0) Then C = C + 1 Else C = C + 2 End If If (A > 5) And (B > 5) Then C = C + 3 ElseIf (A > 5) Or (B > 5) Then C = C + 5 Else C = C + 6 End If N = B * 100 + C * 10 + A MsgBox ("N = " & N)
Example 1 Cones and Cylinders
Problem 1 • Find the surface area of a cylinder with a given radius and height • Area of top and bottom is a circle • Area Top = Area Bottom = r2 • Area of side Circumference * Height • Area Side = 2rh • Total Area • Area = 2r2 + 2rh = 2r (r + h)
Name : Cylinder Given : r h Results : A Intermediate : None Definition A := Cylinder(r, h) Get r Get h A = 2r(r + h) Give A Algorithm Cylinder
Problem 2 • Find the surface area of a cone, of given radius and height • Area = r (r + s) • Where s = (r2 + h2) • This came from an internet research
Name : Cone Given : r, h Results : A Intermediate: S Definition A := Cone (r, h) Get r Get h s = sqrt(r^2 + h^2) A = r (r + s) Give A Cone Algorithm
Problem 3 • You are in charge of ordering supplies for Fennell Funnel Co. • You need to order the plastic pellets for the manufacturing process. • Calculate the volume of plastic needed if you know, • the diameter of the top, and bottom • the length of the cone, and the spout • the thickness of the walls • You may assume the “extra” is needed in the manufacturing process
Name: Funnel Given: Dtop, Htop, Dbot, Hbot, t Results : Vol Intermediates: Acone, Acyl Definition Vol :=Funnel(Dtop, Htop, Dbot, Hbot, t) Get Dtop, Htop Get Dbot, Hbot Get t Acone: = Cone(Dtop/2, Htop) Acyl := Cylinder(Dbot/2, Hbot) Vol = t * (Acone + Acyl) Give Vol Funnel Algorithm
Get r (from a SUB) Get h (from a SUB) A = 2r(r + h) Give A Sub Cylinder (ByVal r as Single, ByVal h as Single Cylinder
Get r (from a SUB) Get h (from a SUB) A = 2r(r + h) Give A (to a SUB) Sub Cylinder (ByVal r as Single, ByVal h as Single, ByRef A as Single) Const Pi = 3.14159 A = 2*Pi*r * (r + h) End Sub Cylinder
Get r (from SUB) Get h (from SUB) s = sqrt(r^2 + h^2) A = r (r + s) Give A SUB Cone (ByVal r as Single, ByVal h as Single, Cone
Get r (from SUB) Get h (from SUB) s = sqrt(r^2 + h^2) A = r (r + s) Give A (to SUB) Sub Cone (ByVal r as Single, ByVal h as Single, ByRef A as Single) Const Pi = 3.14159 Dim s as Single s = (r^2 + h^2)^(1/2) A = pi * r * (r + s) End Sub Cone
Get Dtop, Htop Get Dbot, Hbot Get t Acone: = Cone(Dtop/2, Htop) Acyl := Cylinder(Dbot/2, Hbot) Vol = t * (Acone + Acyl) Give Vol Sub Funnel ( ) Dim Dtop as Single Dim Htop as Single Dim Dbot as Single Dim Hbot as Single Dim t as Single Dim Acone as Single Dim Acyl as Single Dim Vol as Single Dtop = InputBox(“Dtop”) Htop = InputBox (“Htop”) Dbot = InputBox(“Dbot”) Htop = InputBox (“Hbot”) t = InputBox(“t”) Call Cone(Dtop/2, Htop, Acone) Call Cylinder(Dbot/2, Hbot, Acyl) Vol = t * (Acone + Acyl) MsgBox (“Vol = “ & Format(Vol,0)) End Sub Funnel
Tools Macro Run Funnel • View • ToolBars • Command • Add Button • do a Call Funnel() • Change caption to Run Macro Funnel • This is not as direct as Excel