Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. Maximum number in an array?

Maximum number in an array?

Scheduled Pinned Locked Moved Visual Basic
data-structuresquestionlounge
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Adam Wike
    wrote on last edited by
    #1

    I need to make a program that generates 15 random numbers as an array and then lists them in a listbox. I also need to be able to display the maximum and minimum in a label. Heres what I've got so far...

    Public Class Form1
    Dim strNumbers(14) As String

    Private Sub btnGenerate\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGenerate.Click
        Dim intRandom As New Random()
        Dim intLoop As Integer
    
        lstOutcome.Items.Clear()
    
        For intLoop = 0 To strNumbers.Length - 1
            lstOutcome.Items.Add(intRandom.Next(1, 100))
        Next intLoop
    
    End Sub
    
    Private Sub btnMaximum\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMaximum.Click
        Dim intLoop As Integer
        Dim intMax As Integer
    
    
        For intLoop = 0 To strNumbers.Length - 1
            intMax = strNumbers(intLoop)
            If intMax < strNumbers(intLoop) Then
                intMax = strNumbers(intLoop)
            End If
        Next intLoop
    
        lblMinMax.Text = intMax
    End Sub
    

    End Class

    I've got the 15 random numbers in the listbox. I just cant figure out why my maximum code isnt working. Any tips would be awesome right now...

    L D A 4 Replies Last reply
    0
    • A Adam Wike

      I need to make a program that generates 15 random numbers as an array and then lists them in a listbox. I also need to be able to display the maximum and minimum in a label. Heres what I've got so far...

      Public Class Form1
      Dim strNumbers(14) As String

      Private Sub btnGenerate\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGenerate.Click
          Dim intRandom As New Random()
          Dim intLoop As Integer
      
          lstOutcome.Items.Clear()
      
          For intLoop = 0 To strNumbers.Length - 1
              lstOutcome.Items.Add(intRandom.Next(1, 100))
          Next intLoop
      
      End Sub
      
      Private Sub btnMaximum\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMaximum.Click
          Dim intLoop As Integer
          Dim intMax As Integer
      
      
          For intLoop = 0 To strNumbers.Length - 1
              intMax = strNumbers(intLoop)
              If intMax < strNumbers(intLoop) Then
                  intMax = strNumbers(intLoop)
              End If
          Next intLoop
      
          lblMinMax.Text = intMax
      End Sub
      

      End Class

      I've got the 15 random numbers in the listbox. I just cant figure out why my maximum code isnt working. Any tips would be awesome right now...

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      look again right here:

      intMax = strNumbers(intLoop)
      If intMax < strNumbers(intLoop) Then

      :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


      Prolific encyclopedia fixture proof-reader browser patron addict?
      We all depend on the beast below.


      1 Reply Last reply
      0
      • A Adam Wike

        I need to make a program that generates 15 random numbers as an array and then lists them in a listbox. I also need to be able to display the maximum and minimum in a label. Heres what I've got so far...

        Public Class Form1
        Dim strNumbers(14) As String

        Private Sub btnGenerate\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGenerate.Click
            Dim intRandom As New Random()
            Dim intLoop As Integer
        
            lstOutcome.Items.Clear()
        
            For intLoop = 0 To strNumbers.Length - 1
                lstOutcome.Items.Add(intRandom.Next(1, 100))
            Next intLoop
        
        End Sub
        
        Private Sub btnMaximum\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMaximum.Click
            Dim intLoop As Integer
            Dim intMax As Integer
        
        
            For intLoop = 0 To strNumbers.Length - 1
                intMax = strNumbers(intLoop)
                If intMax < strNumbers(intLoop) Then
                    intMax = strNumbers(intLoop)
                End If
            Next intLoop
        
            lblMinMax.Text = intMax
        End Sub
        

        End Class

        I've got the 15 random numbers in the listbox. I just cant figure out why my maximum code isnt working. Any tips would be awesome right now...

        D Offline
        D Offline
        dan sh
        wrote on last edited by
        #3

        Put intMax initialization outside for loop. BTW, you can use a List instead of array since it has a sort method. Then all you would need is to get first and last element.

        1 Reply Last reply
        0
        • A Adam Wike

          I need to make a program that generates 15 random numbers as an array and then lists them in a listbox. I also need to be able to display the maximum and minimum in a label. Heres what I've got so far...

          Public Class Form1
          Dim strNumbers(14) As String

          Private Sub btnGenerate\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGenerate.Click
              Dim intRandom As New Random()
              Dim intLoop As Integer
          
              lstOutcome.Items.Clear()
          
              For intLoop = 0 To strNumbers.Length - 1
                  lstOutcome.Items.Add(intRandom.Next(1, 100))
              Next intLoop
          
          End Sub
          
          Private Sub btnMaximum\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMaximum.Click
              Dim intLoop As Integer
              Dim intMax As Integer
          
          
              For intLoop = 0 To strNumbers.Length - 1
                  intMax = strNumbers(intLoop)
                  If intMax < strNumbers(intLoop) Then
                      intMax = strNumbers(intLoop)
                  End If
              Next intLoop
          
              lblMinMax.Text = intMax
          End Sub
          

          End Class

          I've got the 15 random numbers in the listbox. I just cant figure out why my maximum code isnt working. Any tips would be awesome right now...

          A Offline
          A Offline
          Adam Wike
          wrote on last edited by
          #4

          It worked...to do the minimum I should only have to change the sign I guess. Thanks a lot :)

          1 Reply Last reply
          0
          • A Adam Wike

            I need to make a program that generates 15 random numbers as an array and then lists them in a listbox. I also need to be able to display the maximum and minimum in a label. Heres what I've got so far...

            Public Class Form1
            Dim strNumbers(14) As String

            Private Sub btnGenerate\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGenerate.Click
                Dim intRandom As New Random()
                Dim intLoop As Integer
            
                lstOutcome.Items.Clear()
            
                For intLoop = 0 To strNumbers.Length - 1
                    lstOutcome.Items.Add(intRandom.Next(1, 100))
                Next intLoop
            
            End Sub
            
            Private Sub btnMaximum\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMaximum.Click
                Dim intLoop As Integer
                Dim intMax As Integer
            
            
                For intLoop = 0 To strNumbers.Length - 1
                    intMax = strNumbers(intLoop)
                    If intMax < strNumbers(intLoop) Then
                        intMax = strNumbers(intLoop)
                    End If
                Next intLoop
            
                lblMinMax.Text = intMax
            End Sub
            

            End Class

            I've got the 15 random numbers in the listbox. I just cant figure out why my maximum code isnt working. Any tips would be awesome right now...

            A Offline
            A Offline
            Adam Wike
            wrote on last edited by
            #5

            I also figured out that the way I generated the random numbers made the code mess up. I ended up having to use

            strNumbers(intLoop) = Int((100 - 1 + 1) * Rnd()) + 1

            to get the random numbers.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups