VBScript Programs PART - III
'****************************************************
'21 Print all values from an Array
Dim oArray
Dim oCounter
oArray=array(1,2,3,4,"qtp","Testing")
For oCounter=lbound(oArray) to ubound(oArray)
print oArray(oCounter)
Next
'****************************************************
'****************************************************
'22 Sort Array elements
Dim oArray
Dim oCounter1
Dim oCounter2
Dim tmp
oArray=array(8,3,4,2,7,1,6,9,5,0)
For oCounter1=lbound(oArray) to ubound(oArray)
For oCounter2=lbound(oArray) to ubound(oArray)-1
If oArray(oCounter2)>oArray(oCounter2+1) Then
tmp=oArray(oCounter2)
oArray(oCounter2)=oArray(oCounter2+1)
oArray(oCounter2+1)=tmp
End If
Next
Next
For oCounter1=lbound(oArray) to ubound(oArray)
print oArray(oCounter1)
Next
'****************************************************
'****************************************************
'23 Add two 2X2 matrices
Dim oArray1(1,1)
Dim oArray2(1,1)
Dim tArray(1,1)
oArray1(0,0)=8
oArray1(0,1)=9
oArray1(1,0)=5
oArray1(1,1)=-1
oArray2(0,0)=-2
oArray2(0,1)=3
oArray2(1,0)=4
oArray2(1,1)=0
tArray(0,0)=oArray1(0,0)+ oArray2(0,0)
tArray(0,1)=oArray1(0,1)+oArray2(0,1)
tArray(1,0)=oArray1(1,0)+oArray2(1,0)
tArray(1,1)=oArray1(1,1)+oArray2(1,1)
'****************************************************
'****************************************************
'24 Multiply Two Matrices of size 2X2
Dim oArray1(1,1)
Dim oArray2(1,1)
Dim tArray(1,1)
oArray1(0,0)=8
oArray1(0,1)=9
oArray1(1,0)=5
oArray1(1,1)=-1
oArray2(0,0)=-2
oArray2(0,1)=3
oArray2(1,0)=4
oArray2(1,1)=0
tArray(0,0)=oArray1(0,0)* oArray2(0,0)+ oArray1(0,1)* oArray2(1,0)
tArray(0,1)=oArray1(0,0)* oArray2(0,1)+ oArray1(0,1)* oArray2(1,1)
tArray(1,0)=oArray1(1,0)* oArray2(0,0)+ oArray1(1,1)* oArray2(1,0)
tArray(1,1)=oArray1(1,0)* oArray2(0,1)+ oArray1(1,1)* oArray2(1,1)
'****************************************************
'****************************************************
'25 Convert a String in to an array
Dim oStr
Dim iLoop
oStr="Quick Test Professional"
StrArray=split(oStr)
For iLoop=0 to ubound(StrArray)
print StrArray(iLoop)
Next
'****************************************************
'****************************************************
'26 Convert a String in to an array using ‘i‘ as delimiter
Dim oStr
Dim iLoop
oStr="Quick Test Professional"
StrArray=split(oStr,"i")
For iLoop=0 to ubound(StrArray)
print StrArray(iLoop)
Next
'****************************************************
'****************************************************
'27 Find number of words in string
Dim oStr
Dim iLoop
oStr="Quick Test Professional"
StrArray=split(oStr," ")
print "Theere are "&ubound(StrArray)+1&" words in the string"
'****************************************************
'****************************************************
'28 Write a program to reverse the words of a given string.
Dim oStr
Dim iLoop
oStr="Quick Test Professional"
StrArray=split(oStr," ")
For iLoop=0 to ubound(StrArray)
print strreverse(StrArray(iLoop))
Next
'****************************************************
'****************************************************
'29 Print the data as a Pascal triangle
'The formulae for pascal triangle is nCr=n!/(n-r)!*r!
Dim PascalTriangleRows
Dim nCr
Dim NumCount
Dim RowCount
PascalTriangleRows = 10
For NumCount = 0 To PascalTriangleRows
toPrint= Space(PascalTriangleRows - NumCount)
For RowCount = 0 To NumCount
If (NumCount = RowCount) Then
nCr = 1
Else
nCr = Factorial(NumCount) / (Factorial(NumCount - RowCount) * Factorial(RowCount))
End If
toPrint=toPrint&nCr&" "
Next
print toPrint
Next
Function Factorial(num)
Dim iLoop
Factorial = 1
If num <> 0 Then
For iLoop = 2 To num
Factorial = Factorial * iLoop
Next
End If
End Function
'****************************************************
'****************************************************
'30 Join elements of an array as a string
Dim oStr
Dim iLoop
oStr="Quick Test Professional"
StrArray=split(oStr," ")
print join(StrArray," ")
'****************************************************
No comments:
Post a Comment