Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,906 questions

51,838 answers

573 users

How to find the median of a list of integers in VB.NET

1 Answer

0 votes
Imports System
Imports System.Collections.Generic

Public Class MedianOfIntArray_CSharp
    Private Shared Function FindMedianOfIntArray(ByVal list As List(Of Integer)) As Double
        list.Sort()

        For Each num As Integer In list
            Console.Write(num)
            Console.Write(" ")
        Next

        Dim median As Double
        Dim size As Integer = list.Count

        If size Mod 2 = 0 Then
            median = (list(size / 2 - 1) + list(size / 2)) / 2.0
        Else
            median = list(size / 2)
        End If

        Return median
    End Function

    Public Shared Sub Main(ByVal args As String())
        Dim list As List(Of Integer) = New List(Of Integer) From {
            40, 70, 60, 55, 90, 45, 100, 80, 65, 50, 82, 58
		}
	
		' Dim list As List(Of Integer) = { 24, 25, 26, 27, 28, 30, 32, 51, 34, 35, 36, 40, 60, 42, 49 };
        ' 24 25 26 27 28 30 32 34 35 36 40 42 49 51 60
        ' median = 34.00
	
        Dim median As Double = FindMedianOfIntArray(list)
		
		Console.WriteLine(Environment.NewLine & "median = " & median)
    End Sub
End Class



' run:
'
' 40 45 50 55 58 60 65 70 80 82 90 100 
' median = 62.5
'

 



answered Jul 18, 2024 by avibootz

Related questions

...