5 Replies - 1840 Views - Last Post: 19 October 2013 - 11:12 AM Rate Topic: -----

#1 lennie_k  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 14-October 13

largest/smallest array value

Posted 18 October 2013 - 09:48 AM

Good afternoon.

So I have this homework assignment. I know you've seen this code before, any time I try to google for help I get a million hits for this program. And funnily enough, they seem to be all the same code.... with the same problem. Truth be told, mine is probably very similar in code, but I can't seem to find a solution that works.

I need to find the largest and smallest array values and display them. I've used two different codes and come up with two different problems... with the first code I will show you, I am able to get the lblLargest value to display, but the lblSmallest value remains blank. My second attempt gets me a display of either: smallest = 0 largest = 9 or smallest = 1 largest = 10. Obviously neither of these will work unless those are the numbers chosen by the user. Any help/suggestions are appreciated, including those that I did not specifically ask for!

Again, this first code portion shows the largest value only. I need both largest and smallest. To fit this into the code follwing, just replace the 'processing portion.

'Processing
        For i As Integer = 1 To 10
            strInput(i) = CInt(rtbInput.Lines(i))
        Next

        Array.Sort(strInput)
        lblSmallest.Text = strInput(0)
        lblLargest.Text = strInput(10)




This second code gives me 0,9 or 1,10 depending on what numbers I input.

Public Class frmLgSm

    Dim strInput(10) As String

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        End
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        'Clear form
        rtbInput.Clear()
        lblSmText.Text = ""
        lblLgText.Text = ""
    End Sub

    Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
        'Declarations
        Dim strLargest As String = 0            'to hold largest value
        Dim strSmallest As String = 0           'to hold smallest value
        Dim strHold As String                  'temporary hold
        Dim i, j As Integer

        'Find largest
        For i = 1 To strInput.GetUpperBound(0) - 1
            strLargest = i
            For j = i + 1 To strInput.GetUpperBound(0)
                If strInput(strLargest) < strInput(j) Then
                    strLargest = j
                End If
            Next j

            strHold = strInput(i)
            strInput(i) = strInput(strLargest)
            strInput(strLargest) = strHold
        Next i

        'Find smallest
        For i = 1 To strInput.GetLowerBound(0) + 1
            strSmallest = i
            For j = i - 1 To strInput.GetLowerBound(0)
                If strInput(strSmallest) > strInput(j) Then
                    strSmallest = j
                End If
            Next j

            strHold = strInput(i)
            strInput(i) = strInput(strSmallest)
            strInput(strSmallest) = strHold
        Next i

        'Output
        lblLargest.Text = strLargest.ToString
        lblSmallest.Text = strSmallest.ToString

    End Sub

    Private Sub btnInput_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInput.Click
        'Declarations
        Dim i As Integer

        'Input
        For i = 1 To 10
            strInput(i) = InputBox("Please enter a number")
        Next i

        'Output
        rtbInput.Clear()
        For i = 0 To 0
            rtbInput.AppendText("Input Values" & vbNewLine)
        Next i
        For i = 1 To 10
            rtbInput.AppendText(strInput(i).ToString & vbNewLine)
        Next i
    End Sub

End Class




Is This A Good Question/Topic? 0
  • +

Replies To: largest/smallest array value

#2 astonecipher  Icon User is offline

  • D.I.C.
  • member icon

Reputation: 768
  • View blog
  • Posts: 3,358
  • Joined: 03-December 12

Re: largest/smallest array value

Posted 18 October 2013 - 10:53 AM

Get rid of the "strInput(0)" and "strInput(10)"


        lblSmallest.Text = strInput.min.tostring
        lblLargest.Text = strInput.max.tostring



Was This Post Helpful? 1
  • +
  • -

#3 dbasnett  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 109
  • View blog
  • Posts: 608
  • Joined: 01-October 08

Re: largest/smallest array value

Posted 18 October 2013 - 10:59 AM

If you can get the numbers into an array or list then

        'foo is an array or list of numbers
        Dim small As Integer = foo.Min
        Dim large As Integer = foo.Max


Was This Post Helpful? 1
  • +
  • -

#4 lennie_k  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 14-October 13

Re: largest/smallest array value

Posted 18 October 2013 - 03:21 PM

astonecipher THANKYOU!!

View Postastonecipher, on 18 October 2013 - 10:53 AM, said:

Get rid of the "strInput(0)" and "strInput(10)"


        lblSmallest.Text = strInput.min.tostring
        lblLargest.Text = strInput.max.tostring





Awesome! Thanks. I turned it in because I have 2 more programs to write by Sunday (it's finals week). I do have a question though, just for my benefit since I've already turned in the assignment.... While I was testing the code with the min.tostring added, the app would give me the correct answer up to a point. It refused to recognize anything higher than 999, and twice it ignored my smallest number. Normally, I won't turn something in that isn't perfect, but as I said it's finals week AND it's only a 5 day week vs the normal 7.

Thank you as well dbasnett. The numbers were already in an array... does that mean I could have simply changed my variables from
Dim strLargest As String = 0 

to
Dim strLargest As String = strInput.max
?

Thanks for your help!
Was This Post Helpful? 0
  • +
  • -

#5 dbasnett  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 109
  • View blog
  • Posts: 608
  • Joined: 01-October 08

Re: largest/smallest array value

Posted 19 October 2013 - 06:34 AM

@lennie - if you are interested...

Create a new project and add:

one button
two labels
one textbox

using the default names.

The copy and paste the following code

Public Class Form1

    Dim nums() As Long = New Long() {}

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        TextBox1.Width = TextBox1.Width * 2
    End Sub

    Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        Button1.PerformClick()
    End Sub

    Private Sub TextBox1_Enter(sender As Object, e As EventArgs) Handles TextBox1.Enter
        TextBox1.Text = "Enter a number, press tab"
        TextBox1.SelectAll()
    End Sub

    Private Sub TextBox1_Leave(sender As Object, e As EventArgs) Handles TextBox1.Leave
        Dim somenum As Long
        If Long.TryParse(TextBox1.Text, somenum) Then
            'legal number
            Array.Resize(nums, nums.Length + 1)
            nums(nums.Length - 1) = somenum
            Label1.Text = nums.Min.ToString
            Label2.Text = nums.Max.ToString
        End If
        TextBox1.Select()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        'start over
        Label1.Text = ""
        Label2.Text = ""
        nums = New Long() {}
        TextBox1.Select()
    End Sub
End Class


Was This Post Helpful? 1
  • +
  • -

#6 lennie_k  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 14-October 13

Re: largest/smallest array value

Posted 19 October 2013 - 11:12 AM

Thank you! I am interested. Since I'm a computer programming major and I'm taking online classes, I'm not actually getting as much out of it as I do out of in-school classes. I figure any extra coding I can do will only help me, so I will definitely do this.
I love how you can code things so many different ways to get the same result. :)
Thanks again!

This post has been edited by andrewsw: 19 October 2013 - 11:15 AM
Reason for edit:: Removed large, previous quote

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1