5 Replies - 13145 Views - Last Post: 26 October 2012 - 01:41 AM Rate Topic: -----

#1 Rmclayton  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 20-June 12

Finding Maximum value in an array.

Posted 25 October 2012 - 11:59 PM

Hey guys!

Basically, the user will enter a name and a number. they enter the array sizes(i)

After the user enters 7 names and numbers they have the opportunity to see the array.

I have added a button that will produce the Maximum number in the array. I have set the first array element the maximum.

Heres all my code:

Public Class frmCreateArray
    Dim Names(7) As String
    Dim Sizes(7) As String
    Dim NamesCount As Integer
    Dim SizeCount As Integer
    Dim Index1 As Integer
    Dim Index2 As Integer
    Dim TempNamez As String
    Dim TempIndex2 As String
    Dim TempSizez As String
    Dim Max As Integer
    Dim MaxIndex As Integer
    Private Sub lblCounter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblCounter.Click

    End Sub

    Private Sub cmdEnterThisName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEnterThisName.Click
        NamesCount = NamesCount + 1                 'increments the index
        Names(NamesCount) = txtNamesGoHere.Text             'inputs each name
        SizeCount = SizeCount + 1
        Sizes(SizeCount) = txtSizesGohere.Text
        lblCounter.Text = Str(NamesCount) + " Names enterred"
        txtNamesGoHere.Clear()
        txtNamesGoHere.Focus()
        txtSizesGohere.Clear()
    End Sub

    Private Sub frmCreateArray_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        NamesCount = 0 ' initialises the index to 0
        SizeCount = 0
    End Sub

    Private Sub cmdShowListArray_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdShowListArray.Click
        lstListNames.Items.Clear()
        lstListSizes.Items.Clear()
        For Me.NamesCount = 1 To 7            ' starts loop counting thru the names
            lstListNames.Items.Add(Names(NamesCount))      'adds each name to list
        Next
        For Me.SizeCount = 1 To 7
            lstListSizes.Items.Add(Sizes(SizeCount))
        Next
    End Sub

    Private Sub Swap_Btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SwapBtn.Click
        Index1 = Index1Txt.Text
        Index2 = Index2Txt.Text
        TempNamez = Names(Index1)
        Names(Index1) = Names(Index2)
        Names(Index2) = TempNamez
        TempSizez = Sizes(Index2)
        Sizes(Index1) = Sizes(Index2)
        Sizes(Index2) = TempSizez
        lstListNames.Items.Clear()
        lstListSizes.Items.Clear()
        For Me.NamesCount = 1 To 7            ' starts loop counting thru the names
            lstListNames.Items.Add(Names(NamesCount))      'adds each name to list
        Next
        For Me.SizeCount = 1 To 7
            lstListSizes.Items.Add(Sizes(SizeCount))
        Next
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    End Sub
End Class



Here's what it looks like:

http://puu.sh/1iQoK

This post has been edited by Rmclayton: 26 October 2012 - 12:05 AM


Is This A Good Question/Topic? 1
  • +

Replies To: Finding Maximum value in an array.

#2 manos364  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 42
  • Joined: 09-October 12

Re: Finding Maximum value in an array.

Posted 26 October 2012 - 12:30 AM

I created this function for you.
    Private Function GetMaximumValue() As String
        ' Create the result.
        Dim Result As String
        ' Create the integer to store the maximum value in.
        Dim Max As Integer = 0
        ' Create the integer to pass the maximum value index from
        ' the loop to the result.
        Dim MaxIndex As Integer = 0
        ' Loop through items to get maximum value.
        For Index As Integer = 0 To lstListSize.Items.Count - 1
            ' Check whether the current value is greater than the
            ' maximum found by the time.
            If Max < lstListSize.Items.Item(Index) Then
                ' Store the maximum value.
                Max = lstListSize.Items.Item(Index)
                ' Store the index of the maximum index.
                MaxIndex = Index
            End If
            ' Loop for the other items.
        Next
        ' Set the maximum value.
        ' This will look like this.
        ' "Name   Size".
        ' Change it how you want.
        Result = lstListName.Items.Item(MaxIndex) & "   " & Max
        ' Finally return the result.
        Return Result

        ' {***WARNING***}
        ' If all values are the same, the result will be the first.
    End Function



Use it and don't forget to change the output format.
Was This Post Helpful? 0
  • +
  • -

#3 Rmclayton  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 20-June 12

Re: Finding Maximum value in an array.

Posted 26 October 2012 - 12:49 AM

So i paste this into the button click event to find the maximum?

And how do i change the output? :)

THANK YOU FOR YOUR HELP BY THE WAY
Was This Post Helpful? 0
  • +
  • -

#4 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 769
  • Joined: 19-October 11

Re: Finding Maximum value in an array.

Posted 26 October 2012 - 12:59 AM

Rmclayton .NET collections are zero-based, so when you define Names(7) for example, it can actually hold 8 values, on indexes 0,1,2,3,4,5,6,7.

If you want to find maximum value in array, you can use its Max method:
Module Module1
    Sub Main()
        Dim numbers() As Integer = {1, 234, 56, 78, 910, 11, 234, 56}
        Console.WriteLine(numbers.Max)

        Console.ReadKey()
    End Sub
End Module


Was This Post Helpful? 0
  • +
  • -

#5 manos364  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 42
  • Joined: 09-October 12

Re: Finding Maximum value in an array.

Posted 26 October 2012 - 01:31 AM

No, you paste it as a new function and the into the the button code you type:
label_max.Text = GetMaximumValue()



Now, as far as the output is conserned, you can change it by changing the
Result = lstListName.Items.Item(MaxIndex) & "   " & Max


in the function to be like you want it to.
Was This Post Helpful? 0
  • +
  • -

#6 Rmclayton  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 20-June 12

Re: Finding Maximum value in an array.

Posted 26 October 2012 - 01:41 AM

YYYYYYYYYEEEEEEEEEEESSSSSSSSSSS IIIIIIIIIIITTTTTTTTT WWWWWWWWWWOOOOOOOOOORRRRRRRRRKKKKKKKKKKKKKSSSSSSSSSSS

~.^ ~.^ ~.^ ~.^ ~.^ ~.^ ~.^ ~.^ ~.^ ~.^ ~.^ ~.^ ~.^ ~.^ ~.^ ~.^ ~.^ ~.^ ~.^ ~.^ ~.^ ~.^ ~.^ ~.^ ~.^ ~.^ ~.^ ~.^ ~.^
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1