Note that you should delcare variables with meaning names, and indent your programme statements to improve readability.
The symbol (*) before a question indicates that the question is deemed too difficult for ISOM 3230. These, however, are foundation problems in learning general programming. If you cannot finish these questions, you may skip it for the final exam and ask a firend who have learnt programming later.
Convert the following algebraic equations into VBA statements. Assume all variables have been probably declared.
a. $s=ut+\frac{1}{2}gt^2$
b. $x=\frac{1+b+c}{d-e}$
c. $y=(A+B)(A^2-AB+B^2)$
Write down the data type and the result for each of the following expressions.
Expression | Data Type | Result |
---|---|---|
84+3+2 |
||
4+12/5 |
||
5-10 Mod 3 |
||
3+4+6.0 |
||
1+2*3/4 |
||
384 mod 10 |
||
384/10 mod 10 |
||
0 mod 8 |
Write a VBA programme to produce the following: the user is prompted to enter an integer twice, and then show the following. Below is a smaple output.
18 + 7 = 25
18 - 7 = 11
18 * 7 = 126
18 \ 7 = 2
18 Mod 7 = 4
Fill in the blanks.
Sub Q1_3()
Dim a As Integer, b As Integer
a = InputBox("Input Integer a")
b = InputBox("Input Integer b")
Debug.Print a & " + " & b & " = " & ______________
Debug.Print a & " - " & b & " = " & ______________
Debug.Print a & " * " & b & " = " & ______________
Debug.Print a & " \ " & b & " = " & ______________
Debug.Print a & " Mod " & b & " = " & ______________
End Sub
You are 5 feet 11 inch(es) tall.
You can use an input box to prompt the user to input a value.
Sub Q2_1()
Dim i As Integer
Dim sum As Intger
i = 0
sum = 0
Do While i > 100
sum = sum + i
i = i + 1
Loop
Debug.Print "The sum is " & sum & "."
End Sub
Debug and state the types of errors in the above programme. (Hint: There are two errors.)
A
” is inputted into the following VBA programme?Sub Q2_2()
Dim mark As Integer
mark = InputBox("Need Integer")
Debug.Print mark
End Sub
Sub Q3_1()
Dim x As Integer
Dim y As Integer
Dim z As Integer
x = 3
y = 2
z = x - y
If (x < 0) Or (z > 0) Then
Debug.Print x & y & z
Else
Debug.Print z & y & z
End If
End Sub
a
, b
, and c
are 1
, 4
, and 6
, respectively, what is the expected output of the excrept? If Not (a > 3) And (b + c > a) Then
Debug.Print c & b & a
Else
Debug.Print a & b & c
End If
If (x > 10) And (x < 20) Then
y = 1
ElseIf x = 10 Then
y = 2
ElseIf (x >= 0) And (x < 10) Then
y = 3
Else
y = 4
End If
Replace the segment with a select ... case
statement.
double
) and displays the greatest number amongst them, assuming that the three integers are not in any order.Sub Q4_1()
Dim i As Integer, j As Integer
For i = 1 To 3
For j = 1 To 3
Debug.Print i & j
Next j
Next i
End Sub
Sub Q4_2()
Dim n As Integer, s As Integer
s = 50
n = 1
Do
n = n * 2
Loop Until (n > s)
Debug.Print n
End Sub
*
’ in the immediate window.Sub Q4_3()
Dim i As Integer
Do
i = i + 1
Debug.Print "*"
Loop Until <condition>
End Sub
Write a Boolean expression to replace <condition>
.
for
loop, write a VBA Programme which sums up all the integers between two input numbers inclusely, and display the result in a message box. For example, if the user inputted 5
and 10
, the result should be $5+6+\dots+10=45$. Q5_1_main
is run, what is the output?Option Explicit
Dim num As Integer
Sub Q5_1_main()
num = 10
Q5_1_side num
Debug.Print num
End Sub
Sub Q5_1_side(ByRef x As Integer)
x = x + 1
Debug.Print x
End Sub
Q5_2_main
is run, what is the output?Option Explicit
Dim n As Integer
Sub Q5_2_main()
n = 3
Q5_2_cal n
Debug.Print n
End Sub
Sub Q5_2_cal(ByRef x As Integer)
Dim y As Integer
y = x
x = y * 2 + 1
x = x + y
End Sub
Refer to question 2. Rewrite Q5_2_cal
as a function, and make subsequent changes in Q5_2_main
.
(*) Consider the following VBA programme. If Q5_4_main
is run, what is the output?
Sub Q5_4_main()
Debug.Print F(6)
End Sub
Function F(ByVal n As Integer) As Integer
If n = 1 Then
F = 1
Else
F = F(n - 1) * 2
End If
End Function
checkPrime
. The function accepts an integer paramater n
which is greater than one and returns a Boolean result. If n
is a prime number, the function returns TRUE
; otherwise, it returns FALSE
.checkPrime
to print all the prime number between 2 and 100.Sub Q6_1()
Dim Num(1 To 10) As Integer
Num(1) = 7
Num(2) = 6
Num(3) = 4
Num(4) = 3
Num(5) = 5
Num(6) = 2
Num(7) = 14
Num(8) = 11
Num(9) = 9
Num(10) = 1
Debug.Print Num(Num(7) \ Num(5))
End Sub
Sub Q6_2()
Dim Num(1 To 10) As Integer, i As Integer
Num(1) = 2
Num(2) = 3
For i = 3 To 10
Num(i) = Num(i - 1) + Num(i - 2)
Next
Debug.Print Num(6)
End Sub
Option Explicit
Option Base 0
Sub Q6_3()
Dim num(5) As Integer, i As Integer, max As Integer
For i = LBound(num) To UBound(num)
num(i) = (i - 1) ^ 2
Next
max = num(LBound(num))
For i = LBound(num) + 1 To UBound(num)
If num(i) > max Then
max = num(i)
End If
Debug.Print max
Next
End Sub
90-100: 9
80-89: 9
70-79: 1
60-69: 1
0-59: 0
Complete the following programme by filling in the blanks.
Sub Q6_4()
Const n = 20
Dim sRange(1 To 5) As Integer
Dim sName(1 To 5) As String
Dim score(1 To n) As Integer
Dim i As Integer, k As Integer
For i = 1 To _____________
sRange(i) = 0
Next
sName(1) = "90-100"
sName(2) = "80-89"
sName(3) = "70-79"
sName(4) = "60-69"
sName(5) = " 0-59"
For i = 1 To _____________
score(____) = InputBox("Enter Score")
Select Case score(i)
Case Is >= 90
k = 1
Case Is >= 80
k = 2
Case Is >= _____________
k = 3
Case Is >= 60
k = 4
Case Else
k = 5
End Select
sRange(k) = __________________________
Next
For i = 1 To 5
Debug.Print _______________________________________
Next
End Sub
Write a subroutine that takes an integer array. The subrouting will update the array by multiplying each element by 2.
(*) A Fibonacci seqence is given with the first two terms as 1, and the other terms as the sum of the previous two terms: $F_1 = 1$, $F_2 = 1$, $F_3 = F_1 + F_2 = 2$.
(a) Write a function nthFibonacci
. The function accepts an integer parameter n
which is greater than 0 and returns the n-th Fibonacci number $F_n$.
(b) Using nthFibonacci
, write a function sumOfFibonacci
. The function accepts an integer parameter n
which is greater than 0 and returns the sum of the Fibonacci sequence up to the n-th fibonacci number.
(c) Optimize the nthFibonacci
function by doing the following: store the n-th fibonnaci number into a global array so that the programme needs not to re-calculate any fibonnaci number.
Option Explicit
Option Base 1
Global fibonacciNumber() As Long
Sub Q6_6()
ReDim fibonacciNumber(1)
Debug.Print sumOfFibonacci(40)
End Sub
Function nthFibonacci(ByVal n As Integer) As Long
If UBound(fibonacciNumber) >= n Then
nthFibonacci = fibonacciNumber(n)
Exit Function
End If
ReDim Preserve fibonacciNumber(1 To n)
If n = 1 Or n = 2 Then
nthFibonacci = 1
fibonacciNumber(n) = 1
Else
nthFibonacci = nthFibonacci(n - 1) + nthFibonacci(n - 2)
fibonacciNumber(n) = nthFibonacci
End If
End Function
Function sumOfFibonacci(ByVal n As Integer) As Long
Dim i As Integer
sumOfFibonacci = 0
For i = 1 To n
sumOfFibonacci = sumOfFibonacci + nthFibonacci(i)
Next
End Function
closingTitle
. Also, there is a chance that your downloaded file is corrupted, and the word “Close” is not in row 1. Write a statement to checkt if the target has been found.closingTitle
, store the closing price of the stock into a dynamic array data
. Note that you do not know how many records are there.dataStatic
instead.Range("A1").offset(3,7)
Cells(5,6).offset(2,-5)
Range("C5:D9").cells(3)
Range("B2").Offset(2,3).Cells(5,4)