Assignment
1. Implement in Visual Basic the function according to the following flowchart. Call the function NestedIf. Use inline and block IF statements.
Apply this function on an Excel worksheet to the following data set.
P1 | P2 |
11 | 6 |
22 | 3 |
-22 | 6 |
-57.3 | 4 |
6 | 6 |
-5 | 7 |
4 | 3 |
2. Implement in Visual Basic the function according to the following flowchart. Use Select Case statements.
The syntax of the select statement is the following:
Select Case variable |
Case choice1, choice2 |
Case choice3 |
Case Else |
End Select |
Apply the function to the following dataset
Parameter |
0 |
1 |
2 |
3 |
3.5 |
4 |
5 |
6 |
3. Implement the combination function, which gives the number of ways of choosing m elements from among n elements, in Visual Basic. The function is given as:
The factorial function is the following n! = 1*2* ... *(n - 1)*n.
Implement the factorial function separately first using looping statements, then using recursion. Use the factorial function in building of the combination function.
The flowcharts of the factorial are shown here:
Simple | Recursive |
Display the error message #VALUE! if the factorial argument is fractional, display #NUM! value if the factorial argument is negative. In the combination function display #VALUE! if n is negative, display #NUM! if m > n, or or m < 0.
To display error messages just return the values CVErr(xlErrValue), and CVErr(xlErrNum).
Draw the flowcharts for these functions.
4. A bank offers different yearly interest rates to its customers based on the deposit in the following way:
For deposits up to $1,000 the interest rate is 5.5%
For deposits from $1,000 up to $10,000 the interest rate is 6.3%
For deposits from $10,000 up to $100,000 the interest rate is 7.3%
For all other deposits the interest rate is 7.8%
Implement the function Interest(Dposit) in VBA. Can use Block If structure or Select Case structure.
5. Using the function from the previous exercise, implement a function NewDFV(deposit,Years). The function will return the future value of a deposit with the bank assuming that the deposit and accrued interest are reinvested for a given number of years. Thus, for exampleNewDFV(10000, 10) will return 10000*(1.063)^10.
6. Fibonacci numbers (named after Leonardo Fibonacci, 1170-1230, an outstanding European mathematician of the medieval period) are defined as follows:
F(0) = 0
F(1) = 1
F(2) = F(0) + F(1) = 1
F(3) = F(1) + F(2) = 2
F(4) = F(2) + F(3) = 3
In general, F(n) = F(n-2)+F(n-1)
Write a VBA function that computes the nth number in the series. Recursion is necessary.
This is the flowchart for the fibonacci function: