Even numbers listbox display

Program won't show even numbers if one of the two numbers is negat

Page 1 of 1

6 Replies - 8429 Views - Last Post: 01 December 2010 - 04:15 PM Rate Topic: -----

#1 ravencode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 01-December 10

Even numbers listbox display

Posted 01 December 2010 - 11:22 AM

The program must display numbers 0 to the highest number entered into one of the textboxes which are even division of bigger number by smallest number.
The program isn't quiet done, but I have the rest of it figured out.
The problem is that for some reason the program won't change the label status text and display numbers in the listbox if number1 is negative and number2 is positive. But if number2 is -10 and number2 is 3 it works. Also it ignores any other If statements I made.


Public Class Form1
    Dim number1, number2 As Integer
    Private Sub btnExecute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExecute.Click
        If (Not (IsNumeric(txtFirst.Text))) Or (Not (IsNumeric(txtSecond.Text))) Or (txtFirst.Text = Nothing) Or (txtSecond.Text = Nothing) Then
            lblStatus.Text = "You must enter two numbers"
        Else
            number1 = CInt(txtFirst.Text)
            number2 = CInt(txtSecond.Text)
        End If
        If number1 = number2 Then
            listBox.Text = "The numbers are equal"
            txtFirst.Clear()
            txtSecond.Clear()
        End If
        If number1 = 0 Or number2 = 0 Then
            listBox.Text = "One of the numbers is zero"
            txtFirst.Clear()
            txtSecond.Clear()
        End If
        If number1 > number2 Then
            If number1 > 0 Then
                For i = 0 To number1
                    If ((i Mod number2) = 0) Then
                        listBox.Items.Add(i)
                    End If
                Next i
            ElseIf number1 < 0 Then
                For i = 0 To number1 Step -1
                    If ((i Mod number2) = 0) Then
                        listBox.Items.Add(i)
                    End If
                Next i
            End If
        End If
            If number2 > number1 Then
                If number2 > 0 Then
                    For i = 0 To number2
                    If ((i Mod number1) = 0) Then
                        listBox.Items.Add(i)
                    End If
                    Next i
                End If
            ElseIf number2 < 0 Then
                For i = 0 To number2 Step -1
                If ((i Mod number1) = 0) Then
                    listBox.Items.Add(i)
                End If
                Next i
            End If
    End Sub
End Class

This post has been edited by ravencode: 01 December 2010 - 11:25 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Even numbers listbox display

#2 lordofduct  Icon User is offline

  • I'm a cheeseburger
  • member icon


Reputation: 2538
  • View blog
  • Posts: 4,641
  • Joined: 24-September 10

Re: Even numbers listbox display

Posted 01 December 2010 - 12:33 PM

What is it supposed to do exactly?
Was This Post Helpful? 0
  • +
  • -

#3 ravencode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 01-December 10

Re: Even numbers listbox display

Posted 01 December 2010 - 12:42 PM

View Postlordofduct, on 01 December 2010 - 11:33 AM, said:

What is it supposed to do exactly?


The program asks user for two numbers. After user typed those numbers in and clicked Submit a list box must be populated with numbers from 0 to the highest number user inputted, that give even number from diving the each of the numbers from 0 to the highest number by the smallest number user inputted.

So if user inputted in the first text-box 12 and 3 in the second, then the highest number is 12 and all the numbers from 0 to that 12 that are even, by diving each number from 0 to 12 by the smallest number inputted by user, the 3, must be placed in the listbox.

This post has been edited by ravencode: 01 December 2010 - 12:43 PM

Was This Post Helpful? 0
  • +
  • -

#4 lordofduct  Icon User is offline

  • I'm a cheeseburger
  • member icon


Reputation: 2538
  • View blog
  • Posts: 4,641
  • Joined: 24-September 10

Re: Even numbers listbox display

Posted 01 December 2010 - 01:22 PM

Quote

by diving each number from 0 to 12 by the smallest number inputted by user, the 3, must be placed in the listbox.


huh? Your code doesn't do any of that...

what would result from 3 and 12?

0,2,4,8,10,12?



Anyways, you're going about this one long way around. Why not shortening it up a little to reduce error caused by convolution... let's consider we want to parse out some values. Try parsing instead...

        Dim iH As Integer
        Dim iL As Integer

        ''parse values
        Try
            ''I just use strings, get your text from whereever
            iH = Integer.Parse("3")
            iL = Integer.Parse("12")
        Catch ex As Exception
            MsgBox("Value must be an integer value.")
            Exit Sub
        End Try



now we need to know which is high and which is low (notice in that code iH is the smaller). We need to flip those if iL is actually greater.

        ''ensure that iH is the high, and iL is the low
        If iL > iH Then
            Dim iT As Integer = iL
            iL = iH
            iH = iT
        End If



now we know that iH is > iL

but from your description of allowing negative values... is -12 supposed to be considered greater than 3 or less than? If we need it by absolute value... than use absolute:

        ''ensure that abs iH is the high, and abs iL is the low
        If Math.Abs(iL) > Math.Abs(iH) Then
            Dim iT As Integer = iL
            iL = iH
            iH = iT
        End If



now we need to get the even values from 0 -> iH... counting up for positive iH, and counting down for negative iH. (as far as I understand from your explanation).

        ''now list even values from 0 -> iH
        Dim lst As New List(Of Integer)()
        Dim sgn As Integer = Math.Sign(iH)

        For i As Integer = 0 To Math.Abs(iH) Step 2
            lst.Add(i * sgn)
        Next



note I count up to the abs(iH), with a step of two, because evens are every OTHER number. Why step by 1, when we can step by 2? We then just multiply by the sign of iH to make sure we're counting in the right direction.



from there I have no idea what you want to do... what is this division by iL you speak of?

This post has been edited by lordofduct: 01 December 2010 - 01:31 PM

Was This Post Helpful? 0
  • +
  • -

#5 ravencode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 01-December 10

Re: Even numbers listbox display

Posted 01 December 2010 - 02:29 PM

Quote

(i Mod number2) = 0


If the current i modulus 2 gives an even number place it inside the listbox.
Was This Post Helpful? 0
  • +
  • -

#6 lordofduct  Icon User is offline

  • I'm a cheeseburger
  • member icon


Reputation: 2538
  • View blog
  • Posts: 4,641
  • Joined: 24-September 10

Re: Even numbers listbox display

Posted 01 December 2010 - 03:06 PM

Oh I get that... you're checking if i is even. In my code we just 'Step' over every odd, so no reason to check if even... it's going to be even.


It's this I don't understand:

Quote

by diving each number from 0 to 12 by the smallest number inputted by user, the 3, must be placed in the listbox.


I'm assuming you don't speak English... how about you post the question in your native language and I'll try to translate that instead.

This post has been edited by lordofduct: 01 December 2010 - 03:07 PM

Was This Post Helpful? 0
  • +
  • -

#7 ravencode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 01-December 10

Re: Even numbers listbox display

Posted 01 December 2010 - 04:15 PM

View Postlordofduct, on 01 December 2010 - 02:06 PM, said:

Oh I get that... you're checking if i is even. In my code we just 'Step' over every odd, so no reason to check if even... it's going to be even.


It's this I don't understand:

Quote

by diving each number from 0 to 12 by the smallest number inputted by user, the 3, must be placed in the listbox.


I'm assuming you don't speak English... how about you post the question in your native language and I'll try to translate that instead.


Wtf an insult =)
Anyways, on that particular sentence I was already confused myself what was the program suppose to do.
But yes, I'm checking if i is even compared against the smallest of the two numbers the user inputted.
And my program does work properly. It's just that if I put a negative number in the first text-box(first number) it won't work, but if I put a negative number into the second text-box(second number) it works. Besides all of that the program must not compare the numbers if: one of the numbers is zero, both numbers equal to each other or if there's no numbers provided by the user what so ever.

This post has been edited by ravencode: 01 December 2010 - 04:23 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1