Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Sub FIBSub()
' Finds Nth Fibonacci number N = 1,2,3,...
' From the series Fib(1)=0, Fib(2)=1, Fib(3)= 1,....
' N is the input contained in the cell named ValueofN
' Function FIB is called to find the value
' The result is printed in the cell named ValueofFIBN
Dim M As Range, FIBN As Range
Set M = Range("ValueofN")
Set FIBN = Range("ValueofFibN")
'FIBN.Value = FIB(M.Value)
FIBN.Value = FIB(M.Value)
End Sub
'Recursive computation
' To find Nth Fibonacci Number, N = 1,2,3,4,5,...
' The series is: 0,1,1,2,3,5,8,...
' For example: FIB(1) is 0
' FIB(2) is 1
' FIB(3) is 1
' FIB (4) Is 2
' The rule is FIB(N) = FIB(N-1)+ FIB(N-2) for n = 3,4,5,...
' This function uses recursion
If N = 1 Then
FIBR = 0
Else
If N = 2 Then
FIBR = 1
Else
FIBR = FIBR(N - 2) + FIBR(N - 1)
End If
End If
End Function
Function FIBW(N As Integer) As Long
' Same as FIB, but uses DO WHILE... LOOP
' To find Nth Fibonacci Number, N = 1,2,3,4,5,...
' The series is: 0,1,1,2,3,5,8,...
' For example: FIB(1) is 0
' FIB(2) is 1
' FIB(3) is 1
' FIB (4) Is 2
' The rule is FIB(N) = FIB(N-1)+ FIB(N-2) for n = 3,4,5,...
Dim F As Long, FP As Long, j As Integer
F = 1
FP = 0
If N = 1 Then
FIBW = FP
Else
If N = 2 Then
FIBW = F
Else
j = 3
Do While j <= N
F = F + FP
FP = F - FP
j = j + 1
Loop
FIBW = F
End If
End If
End Function
Function FIBU(N As Integer) As Long
' Same as FIB, but uses Do Until... LOOP
'
'
'
'
'
'
'
Validation of a
of nine digits,
Modulo 9 method
The multipliers
Option Base 1
Function mustselect(am As Range)
' to find which centres must be selected
' am is the accessibilty matrix
'ICOV(I)
'
'MS(J)
'
'MS
'NV ...
'NC...
=
=
=
=
ms3 = mustselect(am2)
'xyrc(1) = Selection.Rows.Count - 1
'xyrc(2) = Selection.Columns.Count - 1
ReDim xyrc(1 To 2)
xyrc = redurc(am2)
ReDim v(1 To xyrc(1))
ReDim c(1 To xyrc(2))
ReDim lines(1 To xyrc(1) + 1, 1 To xyrc(2) + 1)
' fill the vector c
k = 0
For j = 1 To ncntr
If ms3(j) = 0 Then
k = k + 1
c(k) = j
lines(1, k + 1) = j
End If
Next j
' fill vector v
k = 0
For i = 1 To nvil
flag = 0
For j = 1 To ncntr
If ms3(j) = 1 And am2.Cells(i, j).Value = 1 Then
flag = flag + 1
End If
Next j
If flag = 0 Then
k = k + 1
v(k) = i
lines(k + 1, 1) = i
End If
Next i
lines(1, 1) = ""
For i = 1 To xyrc(1)
For j = 1 To xyrc(2)
lines(i + 1, j + 1) = am2.Cells(v(i), c(j)).Value
Next j
Next i
reducedrange = lines
End Function
Precision:=0.01
IntTolerance:=8
AssumeNonNeg:=True
AssumeLinear:=True
Next j
If sum < i Then ' the ith village is not serviced
Exit For
End If
Next i
If sum = 50 Then 'feasible
'---- Find C, the cost of S vector
C = 0
For i = 1 To 15
C = C + Cost(i) * S(i)
Next i
'---------------- update cmin and smin() if necessary
If C < CMin Then
CMin = C
For i = 1 To nc
SMin(i) = S(i)
Next i
End If
End If
Next m
ftime = Timer
sf1 = Now
durn = ftime - stime
sd1 = sf1 - st1
'---------------- write output
If CMin < 29999 Then ' write output if solution is feasible
Orng(k, 2) = CMin
For i = 1 To 15
Orng(k, i + 2) = SMin(i)
Next i
Orng(k, 18) = durn
Orng(k, 19) = sd1 * 24 * 60 * 60
End If
Next k
End Sub
MULTIPLEX MOVIES
Option Explicit
Option Base 1
Sub findmovies()
' Multiplex Movies
' To list the feasible shows
' starting from the Outrng range
'inputrng ... starting timings of shows in 24 hr format,
'
include title row
'outrng.. ...range in which output needs to be written
'ws......... starting time of desirable time window
'we......... ending time of desirable time window
'solnno..... index of the solution,
'
same as the row of the output range
Dim irng As Range, orng As Range
orng(solnno, 5) = tkts
orng(solnno, 6) = tktg
If tkts < mintkts Then
mintkts = tkts
End If
If tktg < mintktg Then
mintktg = tktg
End If
End If
Next j
Next i
' Summary output
If solnno > 0 Then
solnno = solnno + 1
orng(solnno, 1) = "No. of Feasible Solutions"
orng(solnno, 2) = solnno - 1
orng(solnno + 1, 1) = "MinSilverChrg"
orng(solnno + 1, 2) = mintkts
orng(solnno + 2, 1) = "MinGoldChrg"
orng(solnno + 2, 2) = mintktg
End If
End Sub
Function listshows(irng As Range, ptab As Range, prmtr As Range)
'---------------------------------------------------'An array function to be given in an array of size:
'rows in data list (w/o headings)*4, six columns
' Each output row gives the following details on a feasible show:
' Multiplex code, movie name, starting time, Language
' t1 , t2... begin and end times of desired time window
' inputrng... range name of data base including field titles
' otpt() is output array
'--------------------------------------------------Dim
Dim
Dim
Dim
r = 4 * (irng.Rows.Count - 1)
c = irng.Columns.Count
ReDim otpt(1 To 4 * (r - 1), 1 To 6)
'Initialize otpt to null values
For i = 1 To r
For j = 1 To 6
otpt(i, j) = ""
Next j
Next i
' Check each of the four shows in each row to verify their feasibilty
' and if feasible write six output values in the corresponding row of OT
PT
solnno = 0
For i = 2 To irng.Rows.Count
For j = 4 To 7
If irng(i, j) >= prmtr(1, 1) And irng(i, j) <= prmtr(1, 2) Then
solnno = solnno + 1
otpt(solnno, 1) = irng(i, 1)
otpt(solnno, 2) = irng(i, 2)
otpt(solnno, 3) = irng(i, j)
otpt(solnno, 4) = irng(i, 3)
tkts = WorksheetFunction.VLookup(irng(i, 1), ptab, 3, False) * prmtr
(1, 3)
tktg = WorksheetFunction.VLookup(irng(i, 1), ptab, 4, False) * prmtr
(1, 3)
If irng(i, 3) = "E" Then
tkts = tkts * (1 - WorksheetFunction.VLookup(irng(i, 1), ptab, 5, Fa
lse) / 100)
tktg = tktg * (1 - WorksheetFunction.VLookup(irng(i, 1), ptab, 5, Fa
lse) / 100)
End If
otpt(solnno, 5) = tkts
otpt(solnno, 6) = tktg
End If
Next j
Next i
listshows = otpt
End Function
Sub findmovies1()
' Multiplex Movies
' To list the feasible shows starting from the active cell
'inputng ... starting timings of shows in 24 hr format,
'
include title row
'activecell.. the location of the highlight when the button is pressed
'ws......... starting time of desirable time window
'we......... ending time of desirable time window
'solnno..... index of the solution,
'
same as the row of the output range
Dim solnno As Integer, j As Integer, i As Integer
Dim sttime As Single
'clear the output range
j = 1
Do While (ActiveCell(j, 1) <> "")
For i = 1 To 4
ActiveCell(j, i) = ""
Next i
j = j + 1
Loop
'Put the four column headings for output
ActiveCell(1, 1) = "Mltplex"
ActiveCell(1, 2) = "Movie"
ActiveCell(1, 3) = "Sttime"
ActiveCell(1, 4) = "ShowNo."
' To scan the input range
' for feasible solutions
solnno = 1
For i = 2 To Range("inputrng").Rows.Count
For j = 4 To 7
sttime = Range("inputrng").Cells(i, j).Value
If (sttime >= Range("ws").Value And sttime <= Range("we").Value) Then
ActiveCell(solnno
ActiveCell(solnno
ActiveCell(solnno
ActiveCell(solnno
+
+
+
+
1,
1,
1,
1,
1)
2)
3)
4)
=
=
=
=
Range("inputrng").Cells(i, 1).Value
Range("inputrng").Cells(i, 2).Value
sttime
j - 3
solnno = solnno + 1
End If
Next j
Next i
End Sub
mtype = -1 * Intrvl
MR = WorksheetFunction.Match(LV, WorksheetFunction.Index(LR, 0, LCNO), m
type)
GVlookup = WorksheetFunction.Index(LR, MR, RCNo)
End Function
MINIMUM Search
Function Msearch(X As Integer, M As Range)
Dim result(1 To 1, 1 To 2) As Integer
'Dim result(1 To 2) As Integer
Dim i As Integer, j As Integer, NR As Integer, NC As Integer
result(1, 1)
result(1, 2)
'result(1) =
'result(2) =
= 0
= 0
0
0
NR = M.Rows.Count
NC = M.Columns.Count
For i = 1 To NR
For j = 1 To NC
If M(i, j) = X Then
result(1, 1) = i
result(1, 2) = j
'result(1) = i
'result(2) = j
Exit For
End If
Next j
If result(1, 1) > 0 Then
'If result(1) > 0 Then
Exit For
End If
Next i
Msearch = result
'Msearch = WorksheetFunction.Transpose(result)
End Function
Function minnmin(given As Range)
Dim r As Integer, c As Integer, i As Integer, j As Integer
Dim result(1 To 2) As Integer
r = given.Rows.Count
c = given.Columns.Count
result(1) = given(1, 1)
'results(1) = given.cells(1,1).value
For i = 1 To r
For j = 1 To c
If given(i, j) < result(1) Then
result(1) = given(i, j)
End If
Next j
Next i
result(2) = 0
For i = 1 To r
For j = 1 To c
If given(i, j) = result(1) Then
result(2) = result(2) + 1
End If
Next j
Next i
minnmin = result
End Function
Function minnmin3(given As Range)
'Single pass solution
Dim r As Integer, c As Integer, i As Integer, j As Integer
Dim result(1 To 2) As Integer
r = given.Rows.Count
c = given.Columns.Count
result(1) = given(1, 1)
result(2) = 0
For i = 1 To r
For j = 1 To c
If given(i, j) < result(1) Then
result(1) = given(i, j)
result(2) = 1
Else
If given(i, j) = result(1) Then
result(2) = result(2) + 1
End If
End If
Next j
Next i
minnmin3 = result
End Function
Function minnmin2(given As Range)
'Solution using "For each"
Dim r As Integer, c As Integer, i As Integer, j As Integer
Dim result(1 To 2) As Integer
r = given.Rows.Count
c = given.Columns.Count
For i = 1 To n
mincol(i) = 0
minval(i) = 9999
costmat(i, 1) = 9999
For j = 2 To n
costmat(i, j) = tspmat(i, j)
Next j
Next i
tcost = 0
'... Start with city-1 as the current city
'... Find in the row of the current city, min val, and mincol
'....to decide the next city to be visited
'... update cost matrix to ensure that the next city is not visited from
anywhere else
'... update tcost
'... next city becomes current city
row = 1
For i = 1 To n - 1
For j = 2 To n
If costmat(row, j) < minval(row) Then
minval(row) = costmat(row, j)
mincol(row) = j
End If
Next j
tcost = tcost + minval(row)
VENDER Selection
Option Explicit
Option Base 1
Function Composite(Wts As Range, VScore As Range, PScore As Range)
Dim r As Integer, c As Integer, i As Integer, j As Integer
Dim w1 As Single, w2 As Single, w3 As Single
' This is an array function
' To compute the composite scores in vendor selection problem
Dim Comp() As Single
r = PScore.Rows.Count
c = PScore.Columns.Count
ReDim Comp(1 To r, 1 To c)
w1 = Wts(1)
w2 = Wts(2)
w3 = 1 - w1 - w2
For i = 1 To r
For j = 1 To c
Comp(i, j) = w1 * VScore(i, 1) + w2 * VScore(i, 2) + w3 * PScore(i
, j)
Next j
Next i
Composite = Comp
End Function
Function ColMax(Comp As Range)
' array function to compute
' the index of max rated vendor in each column
Dim c As Integer, r As Integer, i As Integer, j As Integer
Dim max As Single, maxrow As Integer
Dim OTRow() As Integer
c = Comp.Columns.Count
r = Comp.Rows.Count
ReDim OTRow(1 To c)
For j = 1 To c
max = -9999
maxrow = 0
For i = 1 To r
If Comp(i, j) > max Then
max = Comp(i, j)
maxrow = i
End If
Next i
OTRow(j) = maxrow
Next j
ColMax = OTRow
End Function