'#####################################################
'Program to illustrate
sorting of elements of an array
'This program takes
the array boundary from the user
'Validate if it is a
valid number <=10
'Get the list of
elements from the user
' Validate if it is a
number
' Finally Sorts and
displays the contents of the array
'#####################################################
'Variable Declarations
Dim
nArray(),nArrayBound
Dim nCounter,
nCtr1,nCtr2
Dim nTempVar,sTempStr
Dim bFlag
'Get the number of
elements as input from the user nArrayBound=inputbox("Enter a number
between 5 and 10"
'Validate the input
(if it is a valid number)
bFlag=False
Do While
bFlag=False
If isNumeric(nArrayBound) Then 'Valid
number
If nArrayBound<=10 Then 'Lesser than
10
If nArrayBound>=5 Then 'Greater than 5
bFlag=True
Else
bFlag=False
nArrayBound=inputbox("Enter a
number greater than 5")
End If
Else
bFlag=False
nArrayBound=inputbox("Enter a
number Less than or equal to 10")
End If
Else
bFlag=False
nArrayBound=inputbox("Enter a valid
number")
End If
Loop
'Redim the array
decalred with the number of elements
ReDim nArray(nArrayBound-1)
' -1 because the array index starts with 0
nArrayBound=cint(ubound(nArray))
'moving the upper boundary of the array to a variable
For nCounter=0 to
nArrayBound
'Get the elements of the array from the user
nArray(nCounter)=inputbox("Enter the
Array Element #"&nCounter)
'Validate the input (if it is a valid
number)
bFlag=False
Do While bFlag=False
If isNumeric(nArray(nCounter)) Then '
Checking if the input is a number
bFlag=True
Else
bFlag=False
nArray(nCounter)=inputbox("Enter
a valid number")
End If
Loop
Next
For nCtr1=0 to
cint(ubound(nArray)) 'To track the current elemtent
For nCtr2 = nCtr1+1 to
cint(ubound(nArray)) ' The other
elements in the Array
If cint(nArray(nCtr2))<
cint(nArray(nCtr1)) Then ' Check which one is smaller
'Swap the elements
nTempVar = nArray(nCtr1)
nArray(nCtr1)=nArray(nCtr2)
nArray(nCtr2)=nTempVar
End If
Next
Next
sTempStr="Sorted
list : "
For nCounter=0 to
ubound(nArray)
'Putting the sorted elements in a string
sTempStr=sTempStr&nArray(nCounter)&", "
Next
Msgbox(sTempStr)
'#######################
'#### Sample
Result #####
'#######################
' Sorted list: -8,
-2,0,1,4,4,7,
'##### End of
Script######
Comments
Post a Comment