Solved

VB quiz

Posted on 2000-03-07
14
306 Views
Last Modified: 2008-02-26
I am a VB student and I'm 1/2 way through my first semester. I have never had any prior programming experience. If someone could help me with my next quiz, I would greatly appreaciate it.
I learn much better by example.

so here it is:

Test the random # generator by creating 30,000 random integers between 1 and 100 inclusive. Use an array to store the count (frequency) of how many times each # is generated. When a 1 is generated, add 1 to frequency_array(1), when a 2 is generated, add 1 to frequency_array(2), etc. Print the number and the count in 5 columns.

i.e:

1. 323 21. 300  41.275  61. 289  81. 280
2. 288 22. 299  42. 303 62. 295  82. 296
.       .        .       .        .
.       .        .       .        .
20. 29 40. 99  60. 293  80. 300  100. 30

If anyone could help, I would greatly appreciate it. My quiz is due by 7pm Wed. 3-8-00 EST.  Thanks
0
Comment
Question by:derigo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 3
14 Comments
 
LVL 32

Expert Comment

by:Erick37
ID: 2594327
Which part did you need help with?
Generating the numbers, storing the numbers in the array, or printing the results?
0
 

Author Comment

by:derigo
ID: 2594366
I need help with it all to be honest with you.
0
 

Author Comment

by:derigo
ID: 2594381
I know that rnd generates the #s, but how do I make sure they are between 1 -100?  how do I search those #s and how do I store those #s in the array? Is it a 1 or 2 dimentional array, and how do I print it to the form in 5 even columns? If you don't think I've offered enough points for this, let me know and I'll adjust accordingly. I just really need the help.
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 32

Expert Comment

by:Erick37
ID: 2594404
Look up Rnd in Help:

To produce random integers in a given range, use this formula:

Int((upperbound - lowerbound + 1) * Rnd + lowerbound)

To start...

Private Sub Command1_Click()
    Dim lRandom As Long
    Dim i As Long
    For i = 0 To 300 '30000 later...
        'Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
        'Int((100 - 1 + 1) * Rnd + 1)
        'Int(100 * Rnd + 1)
        lRandom = Int(100 * Rnd + 1)
        Debug.Print lRandom
    Next
End Sub

Private Sub Form_Load()
    Randomize
End Sub
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2594419

Dim intCount(100) As Integer
Dim intIndex As Integer
Dim intTest As Integer

Randomize
For intTest = 1 To 30000
  intIndex = CInt((99 * Rnd()) + 1)
  intCount(intIndex) = intCount(intIndex) + 1
Next intTest

intTest = 0
For intIndex = 1 To 100
  Debug.Print intIndex & " " & intCount(intIndex)
  intTest = intTest + intCount(intIndex)
Next intIndex
Debug.Print intTest & " Tests Performed"
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2594433
Next step, store the results in an array.

Private Sub Command1_Click()
    Dim lRandom As Long
    'Declare your array to store 100 numbers
    Dim freq_array(1 To 100) As Long
    Dim i As Long
    'Disable the button until we are done
    Command1.Enabled = False
    For i = 0 To 30000
        lRandom = Int(100 * Rnd + 1)
        'Add 1 to the array number that corresponds with
        'the random number generated.
        'This will count how many times each number is generated.
        freq_array(lRandom) = freq_array(lRandom) + 1
    Next
    Command1.Enabled = True
    'Check the results:
    For i = 1 To 100 'Look at each element of the array
        Debug.Print Format(i, "000") & ": " & CStr(freq_array(i)) & " times"
    Next
End Sub
0
 

Author Comment

by:derigo
ID: 2594435
Eric37,

Thank you for your comment...  I'm not sure what you mean by upperbound and lowerbound.  would those be my values of 1 - 30,000?  If you could actually writ the code for this, it would help greatly and I would give you all the points you want within my power.  I know that when I see the actual code, It'll click in my head.
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2594445
Ooops.. just read the 5 column thing.. (its been a looooong time since I have been in school.. LOL).. Now correcting & simplifying.. Change the above to...

<----- Code Begin ----->

Dim intCount(100) As Integer
Dim intIndex As Integer
Dim intTest As Integer

Randomize
For intTest = 1 To 30000
  intIndex = CInt((99 * Rnd()) + 1)
  intCount(intIndex) = intCount(intIndex) + 1
Next intTest

For intIndex = 1 To 100 Step 5
  Debug.Print _
    intIndex + 0 & " " & intCount(intIndex + 0) & vbTab; _
    intIndex + 1 & " " & intCount(intIndex + 1) & vbTab; _
    intIndex + 2 & " " & intCount(intIndex + 2) & vbTab; _
    intIndex + 3 & " " & intCount(intIndex + 3) & vbTab; _
    intIndex + 4 & " " & intCount(intIndex + 4)
Next intIndex

<----- Code End ----->

0
 
LVL 14

Accepted Solution

by:
wsh2 earned 50 total points
ID: 2594459
derigo.. this section of code defines your variables and loads the array..

Dim intCount(100) As Integer
Dim intIndex As Integer
Dim intTest As Integer

Randomize

For intTest = 1 To 30000
 
  ' Calcualate a random index from 1 to 100
  intIndex = CInt((99 * Rnd()) + 1)

  ' Add 1 to the random Index Counter
  intCount(intIndex) = intCount(intIndex) + 1

Next intTest

---------------------------------
This section prints the array..

For intIndex = 1 To 100 Step 5
  Debug.Print _
    intIndex + 0 & " " & intCount(intIndex + 0) & vbTab; _
    intIndex + 1 & " " & intCount(intIndex + 1) & vbTab; _
    intIndex + 2 & " " & intCount(intIndex + 2) & vbTab; _
    intIndex + 3 & " " & intCount(intIndex + 3) & vbTab; _
    intIndex + 4 & " " & intCount(intIndex + 4)
Next intIndex

---------------------------------

Voile.. all done.. <smile>

0
 

Author Comment

by:derigo
ID: 2594518
wsh2,

Thank you so much!!!

I know this souns stupid, but what does "Randomize" under the dim statements do? I mean you're using rnd later and I thought that was what generated the random #s.
also, what does the C in"intIndex = CInt((99 * Rnd()) + 1) " do?
If you don't feel like answering, it's ok. I'm verifying the answer anyway and you'll get the points.

Oh, and how do I get more space between the columns? I'm actually printing this to the form.

Thanks again.
0
 

Author Comment

by:derigo
ID: 2594528
nevermind about the spacing of the columns, that was a stupid question... I figured that out.
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2594539
Ok.. to use the Random number function you need to seed the randomizer first. The Randomize command uses the System clock to come up with this number.

The Cint function converts an expression to an INTEGER type. As the RND function returns a Single type (ie.. a number with decimal positions), I am using the Cint to strip all the decimals off. To be honest, the Cint is unneccessary as VB will "type" the number automatically, but I'm not sure which VB compiler you are working with, and I thought I would play it safe by explicitly stating the correct type.
0
 

Author Comment

by:derigo
ID: 2594584
thanks wsh2   I get it now.
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2594689
Atta guy.. go get em !!! But I want you to know.. I don't come cheap.. you owe me a slice of pizza and a beer.. LOL. Er.. wait.. Sonny?.. Is that YOU?.. Oh My Gawd !!!.. <arms crossed.. foot methodically tapping>.. GET YOUR HOMEWORK DONE !!!.. <sheesh>.

<smile> and a <wink>

<poof>
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question