Implement function handmadeSAMPLE in Visual Basic for Applications that calculates the following formula. 

 Parameter P2 depends on parameter P1 in the following way: 

P1<=100.00

P2

P1<=1060.00

P2 + 20.8

P1<=10000.00

P2 – 10.7

P1>10000.00

P2 – 40.4

 

It is required that parameter P2 does not drop below 0.  This means that the minimum value for parameter P2 is 40.4

 


The flowchart for this solution is given below (NOTE: for assignment you must show declaration of all variables in a flowchart, e.g. applying to this example: P1 as Currency, P2 as Single, n as Integer).

 


The program code is below.  It is obvious that there are three parameters in this function and their data types can be guessed by looking at the numbers that those parameters get. When copying this code be careful to not copy any additional characters including spaces, tabs, and newlines.

 

Option Explicit
Function handmadeSample(P1 As Currency, P2 As Single, n As Integer) As Currency
' Declare local variables
Dim offset As Single
Dim i As Integer
' Determine the offset for parameter P2
If P1 <= 100 Then
    offset = 0
ElseIf P1 <= 1060 Then
    offset = 20.8
ElseIf P1 <= 10000 Then
    offset = -10.7
Else
    offset = -40.4
End If
' Calculate the actual formula
handmadeSample = 0
For i = 1 To n
    handmadeSample = handmadeSample + (100 + P2 + offset) ^ (i - 1)
Next i
handmadeSample = handmadeSample * P1
End Function

 


Input data can be fed as show in this example.  The numbers in the table should be reasonably selected to fit the parameters of the function.  For example parameter P1 cannot get value -0.000265, parameter n cannot get value 100.98