Home > Net >  Problem is finding second highest values in vb.net
Problem is finding second highest values in vb.net

Time:06-22

Aim: To find the second smallest value of the sorted array in VB.NET. Procedure: Step 1: Start the program. Step 2: Assign input values for an Array. Step 3: sort the input of an array by using Sort method. Step 4: Find the second smallest value of the sorted array . Step5: show the sorted array and second smallest value of an array. Step6: stop the program '''Module Module1

Sub Main()
    Dim n(10) As Integer 'Declaration n in array in integer format'
    Dim i, j As Integer  'Declaration i,j in integer format'
    Dim second_small As Integer
    second_small = 0
    Dim small As Integer
    small = 0
    For i = 0 To 10 'first condition check the statement looping for the 10 time'
        Console.WriteLine(" Enter the value for array value[{0}] : ", i)  'user input array values'
        n(i) = Console.ReadLine()  ' store the values in n of arry' 
    Next i
    For j = 0 To 10 'for array number'
        Console.WriteLine("Array value({0}) = {1}", j, n(j))
    Next j
    Console.WriteLine("Lowest Number To Higher Number")
    System.Array.Sort(n)
    For Each str As Integer In n
        Console.WriteLine(str)
    Next
    small = n(-1)
    For i = 1 To 10 Step 1
        If (small > n(i)) Then
            second_small = small
            small = n(i)
        End If
    Next
    Console.WriteLine("Second smallest element in array is: {0}", second_small)
    Console.ReadKey()
End Sub

CodePudding user response:

If you sort the array, you can select the first two elements after sorting. A more efficient way (arguably) is what you're trying to do with the last loop, which is not quite correct as it stands now.

CodePudding user response:

the answer is Console.WriteLine("Second smallest element in array is: {0}", n(1)) array values are stored in n values so the second smallest values are stored in 1 area

  • Related