[RESOLVED] VB6 Program; gets the mode of 10 numbers (HELP)
The program accepts 10 numbers then it will arrange the numbers in ascending order then it will count the mode of the 10 numbers inputted. When we say mode, it is the number most frequently used. For example there are 10 numbers: 1,2,1,1,1,4,5,2,1,9 --> the most frequent number is 1 because its number of occurrence is 5, so the mode is 1.
This is where the code of the getting the mode is inserted:
for x = 0 to 9
list1.additem num(x) --> This is where the 10 numbers are displayed in ascending order
~~~~CODES TO GET THE MODE/COUNT THE NUMBER OF OCCURRENCE OF EACH NUMBER~~~~
next x
~~~~CODES TO DISPLAY THE MODE~~~~
I don't know the codes for counting the number of occurrence of every number being entered. :( Please help me guys! :thumb::bigyello::thumb:
THANK YOU IN ADVANCE :)
Re: VB6 Program; gets the mode of 10 numbers (HELP)
Looks like homework. We won't do your homework but will help point you in the right direction.
- You can loop thru your listbox (after it is all filled out)
- Maybe use the .ItemData property to store counts
- Start from the 1st/current item and search for any matches from the next item til the end
:: If its .ItemData property is non-negative, give it a count of 1 (keep reading below, it should make sense)
-- if match found
:: update the .ItemData property of the current item with the total count
:: tag the found item's .ItemData with a negative value
:: continue looping til end of list
Now continue from the next list item. If the .ItemData value for that item is negative, you've already processed it, so skip it and go to the next item
When all done, your listbox's .ItemData properties will either be a positive number or a negative number. Find the one with the largest positive number
Edited:
If the list is sorted, then it is much easier
- keep track of which is the current number
- loop til end of listbox
- for each item that matches the current number, tally 1 more match.
- when you hit an item that doesn't match the current number
:: change the current number
:: start a new tally for it
:: when done with that number, if its tally is greater than the previously largest tally, cache it
Re: VB6 Program; gets the mode of 10 numbers (HELP)