' ceiling of N = the smallest element in an array greater than or equal to N
Imports System
Public Class Program
Public Shared Function find_the_ceiling(ByVal arr As Integer(), ByVal N As Integer) As Integer
Dim size As Integer = arr.Length
If N <= arr(0) Then
Return 0
End If
For i As Integer = 0 To size - 1
If arr(i) = N Then
Return i
End If
If arr(i) < N AndAlso arr(i + 1) >= N Then
Return i + 1
End If
Next
Return -1
End Function
Public Shared Sub Main(ByVal args As String())
Dim arr As Integer() = New Integer() {1, 2, 7, 8, 14, 19, 20, 24, 28}
Dim N As Integer = 9
Dim index As Integer = find_the_ceiling(arr, N)
If index = -1 Then
Console.Write("The ceiling doesn't exist in array")
Else
Console.Write("The ceiling of " & N & " is " & arr(index))
End If
End Sub
End Class
' run:
'
' The ceiling of 9 is 14
'