How can I Calculate the Average of the Array List - arrays

Ok I need to get size of the array and the array inputs through a msgbox and display the array list in a list box and then get the average of the array list. here is the code I have so far:
Private Sub btnCalculate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
Dim i, size As Integer
size = Val(InputBox("Please enter array size"))
Dim sequence(size) As Integer
'get array values
i = 0
Do While i < size
sequence(i) = Val(InputBox("Please enter element of array"))
i = i + 1
Loop
i = 0
Do While i < size
lstoutArray.Items.Add(sequence(i))
i = i + 1
Loop
End Sub

While something like this will work:
Dim lstoutArray As New ArrayList
Dim lstoutCount As Double = 0
Dim size As Double
size = Val(InputBox("Please enter array size"))
For i = 1 To size
lstoutArray.Add(Val(InputBox("Please enter element of array")))
lstoutCount += DirectCast(lstoutArray(lstoutArray.Count - 1), Double)
Next
Dim lstoutAverage As Double = lstoutCount / lstoutArray.Count
You can see from this example that one of the main drawbacks to using an arraylist, is that it isn't strongly typed. Therefore to use the values in the arraylist you have to cast them as the type you need.
A List(Of) is much easier to use as it's strongly typed already and has the Average extension:
Dim lstoutArray As New List(Of Double)
Dim size As Double
size = Val(InputBox("Please enter array size"))
For i = 1 To size
lstoutArray.Add(Val(InputBox("Please enter element of array")))
Next
Dim lstoutAverage = lstoutArray.Average

Related

Making a binary to decimal converter, but need to convert a char array to a integer array

I need to create a code that converts any binary number (up to 8 digits) into the decimal counterpart.
So i have created most of the program but i have one problem, this is that i have used a ToCharArray to split the string of numbers entered into the individual elements of an array. But then i have to use them number for arithmetic, - but for that they need to be in an integer array.
Dim array(7) As Integer
Dim num As String
Dim i As Integer = 0
Dim x As Integer = 0
Dim y As Integer = 1
Dim dec As Integer = 0
console.writeline("Enter an 8-Digit binary string")
num = console.readline()
num.ToCharArray(array)
array.Reverse(array)
For i = 0 To 7
dec = dec + array(x) * 1 * y
x = x + 1
y = y * 2
Next
console.write(dec)
console.read()
(Sorry i didn't know which parts would be helpful or not, so heres what ive got so far)
Make your life easier and take advantage of the convert in vb.net so be something very simple like this
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim binary As String = "11010110"
Console.WriteLine(ToDecimal(binary))
End Sub
Function ToDecimal(input As String) As Integer
Dim i As Integer = Convert.ToInt32(input, 2)
Return i
End Function

VBA Trend returned value

I have two array defined inside of VBA, and I want to find by linear interpolation points in between based on known values.
Sub example()
Dim boundpoint(1 To 2) As Integer
Dim boundeleva(1 To 2) As Double
Dim totELEV as Integer
ReDim plvl(201) as Double
Dim out As Variant
totELEV = 12
boundeleva(1) = -61.90
boundeleva(2) = -260.25
boundpoint(1) = 1
boundpoint(2) = 201
For i = 1 to totELEV
out = Application.WorksheetFunction.Trend(boundPoint,boundEleva,ELEV(i),True)
plvl(i) = Application.WorksheetFunction.RoundUp(out,0)
Next i
End Sub
But of course it is not working due to mismatch. If I understand it right, "trend" is returning an array for each ELEV(i).
Now I just want the returned value to be a Double that can be rounded up to the closest Integer.
Any suggestion?
Thank you!

There is an error in my code, '1- dimensional array of double cannot be converted too boolean'

Sorry i'm new to all this, i don't know how to fix my bubble sort , i have an array but i have 2 errors in my code, so i don't even know if it works or how to fix it help would be very much appreciated.
My first error said '1- dimensional array of double cannot be converted too boolean' and second error said 'identifier expected' Both errors occur on line 23
'Declares the name of the sorted Array
Dim SortedArray(49) As Integer
'Declares the name for the Unsorted Array
Dim UnSortedArray(49) As Double
Private Sub BtnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnStart.Click
'Declare Random Numbers, NumberRange and the Name of the array
Dim RandomNumber As New Random
Dim NumberRange As Integer
Dim swap As Boolean
'Declares the Values of the numbers only be between 0 and 100
Randomize(NumberRange)
For NumberRange = 0 To 49
Next
'Creates and Array with 100 RandomNumbers with values inbetween 0 and 100
SortedArray(NumberRange) = RandomNumber.Next(0, 100)
UnSortedArray(NumberRange) = UnSortedArray(NumberRange)
'Adds the items onto the ListBox
LstUnSorted.Items.Add(UnSortedArray(NumberRange).ToString)
swap = False
For i = 1 To UnSortedArray(49) - 1
If UnSortedArray ([i]-1) > UnSortedArray[i] Then
UnSortedArray(i - 1) = UnSortedArray([i])
swap = True
End If
Next
End Sub
End Class

Will not display in list box VB

So, I'm trying to display the statistics for a soccer teams points scored.
At the time this is being executed, the arrays have been already filled and what not.
When this form opens, I'd like it to display the maximum, minimum, and average scores....
I want it to get the players name and score for max and min. For example:
Maximum: John scored 9
Minimum: Joe scored 2
Like, i'd be getting the value at strPlayers(i) for the name and intScores(i) for score.
I'm pretty sure I had the functions correct, but, for whatever reason, I can not get it to display anything in the list box upon loading the form!
Public Class frmDisplayStatistics
Function FindMaximum() As String
Dim max As Integer
Dim i As Integer = 0
ReDim intScores(intNumberOfPlayers)
max = CInt(intScores(0))
For i = 0 To intNumberOfPlayers
If max < intScores(i) Then
max = CInt(intScores(i))
End If
Next
max = strPlayers(i) & " scored maximum points of " & intScores(i)
Return max
End Function
Function FindMinimum() As Integer
Dim min As Integer
Dim i As Integer = 0
ReDim intScores(intNumberOfPlayers)
min = CInt(intScores(0))
For i = 0 To intNumberOfPlayers
If min > intScores(i) Then
min = CInt(intScores(i))
End If
Next
Return min
End Function
Function FindAverage() As Double
Dim average As Double
Dim i As Integer = 0
average = total / intNumberOfPlayers
Return average
End Function
Private Sub frmDisplayStatistics_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim max As String
max = FindMaximum()
lstStatistics.Items.Add(max)
lstStatistics.Items.Add("Minimum: " & FindMinimum())
lstStatistics.Items.Add("Average: " & FindAverage())
End Sub
Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
Me.Close()
End Sub
End Class
The reason maximum returns a string and minimum and average return a number is because I was trying a different approach, that also did not work. :/
Assuming you are getting array in the variable max in the form load event. Then you should loop the array. Like below
for i = 0 to max.count -1
listbox.item.add(i)
next
Also you need to declare variable max as array. Hope you got my point

Converting a System.Array to Double() in VB.NET 2012

I want to convert an array of type System.Array to Double() or Double(,) (I already know which one to convert to). The problematic lines of code follow
Dim kernel As Double() = CType(Array.CreateInstance(GetType(Double), _
{2 * limit + 1}, {-limit}), Double())
Where limit is predefined as a valid, positive Integer. I get the InvalidCastException. How do I go about doing this? Or another way of creating a Double array with < 0 starting index?
You can use the Enumerable.Range method to create a single-dimension array like so:
Dim start = 0
Dim count = 10
Dim singleArray = Enumerable.Range(start, count).ToArray()
To create a multidimensional array you will have to create your own extension method to modify the collection as I have done below
Public Module Extensions
<Runtime.CompilerServices.Extension()>
Function SelectMultiDimension(Of T)(collection As IEnumerable(Of T), rows As Integer, cols As Integer) As T(,)
Dim multiDimArray(rows - 1, cols - 1) As T
Dim i As Integer = 0
For Each item In collection
If i >= multiDimArray.Length Then Exit For
multiDimArray(i \ cols, i Mod cols) = item
i += 1
Next
Return multiDimArray
End Function
End Module
Then you can use it this way:
Dim mArray = Enumerable.Range(start, count).SelectMultiDimension(3, 4)

Resources