Results 1 to 10 of 10

Thread: recieve upper bound and lower bound of a range

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2006
    Posts
    63

    recieve upper bound and lower bound of a range

    Is there anyone who can tell me the code for the below?

    Receive a lower bound and an upper bound of a range. Also receive a number of random numbers to generate.
    You must validate
    1) the numbers are integers
    2) the lower bound is less than or equal to the upper bound
    3) the number to generate is positive (i.e., > 0).

    Generate this many numbers in the specified range and keep track of how many of each are generated in an array of counters.
    Once all the numbers have been generated, display in a List Box the number, the raw count data, and the percentage of
    how many times the number was generated displayed with two decimal places of precision.
    test1

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: recieve upper bound and lower bound of a range

    Moved From The CodeBank

    There are a great many of us who "can" tell you what the code is, but we won't.

    You try...and if you have problem, post your problem and we will help you solve it.

    Here is a starting point I would suggest...look at the textbooks or notes you have taken for the class you are taking.

  3. #3
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: recieve upper bound and lower bound of a range

    Hi shivani...

    I think this will get you started: http://www.vbforums.com/showpost.php...80&postcount=3

    While you develop the program, if you got stuck at any point feel free to ask us. We're happy to help you(only if you show some effort from your side) .

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  4. #4
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: recieve upper bound and lower bound of a range

    That is one ugly use of a listbox.

  5. #5

    Thread Starter
    Member
    Join Date
    Nov 2006
    Posts
    63

    Re: recieve upper bound and lower bound of a range

    Quote Originally Posted by sha_shivani View Post
    Is there anyone who can tell me the code for the below?

    Receive a lower bound and an upper bound of a range. Also receive a number of random numbers to generate.
    You must validate
    1) the numbers are integers
    2) the lower bound is less than or equal to the upper bound
    3) the number to generate is positive (i.e., > 0).

    Generate this many numbers in the specified range and keep track of how many of each are generated in an array of counters.
    Once all the numbers have been generated, display in a List Box the number, the raw count data, and the percentage of
    how many times the number was generated displayed with two decimal places of precision.


    I already coded this but i am not sure how to Receive a lower bound and an upper bound of a range.Currently i am getting the values from the textbox in which i enter value.As the assignment says receive a lower bound and an upper bound of a range do i need to hardcode values inside code?
    Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'Initialize the random # generator.
    Randomize()
    End Sub

    Public Function Rand(ByVal Low As Decimal, _
    ByVal High As Decimal) As Decimal
    Low = txtLowerBound.Text
    High = txtUpeprBound.Text
    Rand = Int((High - Low + 1) * Rnd()) + Low
    txtGenerate.Text = Rand
    End Function
    Private Sub btnCreateReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateReport.Click
    Dim Low As Decimal, High As Decimal
    Rand(Low, High)
    End Sub
    End Class
    test1

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: recieve upper bound and lower bound of a range

    That is VB.Net code... Thread moved from the 'VB6 and Earlier' forum to the 'VB.Net' (VB2002 and later) forum

  7. #7
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: recieve upper bound and lower bound of a range

    I have rearranged the code:
    vb Code:
    1. Public Class Form1
    2.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
    3.         'Initialize the random # generator.
    4.         Randomize()
    5.     End Sub
    6.  
    7.     Public Function Rand(ByVal Low As Integer, _
    8.     ByVal High As Integer) As Decimal
    9.  
    10.         Return (Int((High - Low + 1) * Rnd()) + Low)
    11.  
    12.     End Function
    13.  
    14.     Private Sub btnCreateReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateReport.Click
    15.  
    16.         Dim Low As Integer, High As Integer
    17.         '~~~ Conversion to Integer
    18.         Integer.TryParse(txtLowerBound.Text, Low)
    19.         Integer.TryParse(txtUpperBound.Text, High)
    20.         '~~~ Call the function, passing LOWER and UPPER bound(range), and display the returned value
    21.         txtGenerate.Text = Rand(Low, High).ToString
    22.  
    23.     End Sub
    24. End Class
    Assignment is not complete. You need to take care of the rest of the things.

    Edit:
    Changed the datatype from Decimal to Integer. (see the below post of jmcilhinney)
    Last edited by akhileshbc; Sep 24th, 2010 at 07:54 AM. Reason: changed datatype from Decimal to Integer

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: recieve upper bound and lower bound of a range

    The assignment specifically states that the numbers are integers so why are you using Decimal? Integer.TryParse will tell you whether the input is an Integer and, if it is, give you the value.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Member
    Join Date
    Nov 2006
    Posts
    63

    Re: recieve upper bound and lower bound of a range

    Quote Originally Posted by akhileshbc View Post
    I have rearranged the code:
    vb Code:
    1. Public Class Form1
    2.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
    3.         'Initialize the random # generator.
    4.         Randomize()
    5.     End Sub
    6.  
    7.     Public Function Rand(ByVal Low As Integer, _
    8.     ByVal High As Integer) As Decimal
    9.  
    10.         Return (Int((High - Low + 1) * Rnd()) + Low)
    11.  
    12.     End Function
    13.  
    14.     Private Sub btnCreateReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateReport.Click
    15.  
    16.         Dim Low As Integer, High As Integer
    17.         '~~~ Conversion to Integer
    18.         Integer.TryParse(txtLowerBound.Text, Low)
    19.         Integer.TryParse(txtUpperBound.Text, High)
    20.         '~~~ Call the function, passing LOWER and UPPER bound(range), and display the returned value
    21.         txtGenerate.Text = Rand(Low, High).ToString
    22.  
    23.     End Sub
    24. End Class
    Assignment is not complete. You need to take care of the rest of the things.

    Edit:
    Changed the datatype from Decimal to Integer. (see the below post of jmcilhinney)

    As the assignement says recieve lower bound of a range do i need to do that through code or do i need to put some numer in textbox to get the lower bound.
    test1

  10. #10
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: recieve upper bound and lower bound of a range

    Quote Originally Posted by sha_shivani View Post
    As the assignement says recieve lower bound of a range do i need to do that through code or do i need to put some numer in textbox to get the lower bound.
    I think, getting the range through textbox is the best option (ie. what you are doing now). Because, we can test it by giving different values at runtime. But if we hardcode the range, then we need to rewrite the code each time (for providing the new range values).

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width