# Just a little question

Andy Rayner
Ranch Hand
Posts: 36
Hello again, i just have a little question that you might be able to help me on. It is using this dice example again, i have a row of six dice and at the end is a text box whixh will display the number of same dice.
example
row 1: 1 2 4 2 2 1 text box = 3 (there are 3 2's)
row 2: 1 3 1 3 4 5 text box = 2 (there are 2 1's and 2 3's)

the code i have is this:
When a dice is created:

jPanel1.setLayout(new GridLayout(4, 6, 0, 0));

i then have a method which loops through the array and gets the face value of each dice.

I was wondering how i could get it to compare all the dice and give me the number i want, im not too sure of the logic.

Horatio Westock
Ranch Hand
Posts: 221
Right, I guess this is for an assignment, so I won't provide code, but here are some hints:

1: Create somewhere to store the total count of each face value, perhaps an array.

2: Go through each dice, increment the count for that face value

3: For each of the totals, write out the ones that are > 1

Andy Rayner
Ranch Hand
Posts: 36
how do you do the count? is there a function?
to count all the ones that are say 2..

Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
You'll have to loop through all the dice and keep your own totals. Say you find a "2" and you'd like to count how many 2's you find. Can you think of a data structure where "2" could be the name or address of a counter? Then you could loop through the counters to see which is highest.

BTW: For graduate level CS work, there is an algorithm that will find the number with the most occurrences in one pass. Don't turn THIS in as homework!

Andy Rayner
Ranch Hand
Posts: 36
hello i have had a good go at this and i am not sure if i am getting it right, here is what i have

Horatio Westock
Ranch Hand
Posts: 221
Hi again,

I think you are making things more complicated than they need to be. A simple array rather than an ArrayList can be used, if you aren't planning to use any of the List functionality. Consider this:

This creates an array of ints with 6 entries. Think of these as the 6 possible face values of the dice. Arrays of ints are automatically initialised with the value 0.

Now, when you look at the face value of one of the dice, use that to look up the relevant counter:

In one line, you can also increment that counter. E.g.

With this in mind, try changing your code.

Good luck!

David Harkness
Ranch Hand
Posts: 1646
Originally posted by Stan James:
BTW: For graduate level CS work, there is an algorithm that will find the number with the most occurrences in one pass. Don't turn THIS in as homework!
Was that page serious? Did someone really publish that algorithm? I didn't want to pull up the PDF and read the sordid history of its publication, but if you know, please do tell.

Andy Rayner
Ranch Hand
Posts: 36
Thanks ive got that bit, just a question how would incrementing every number help? i have got.

As i remember what you said earlier you need to return the ones with more then 1, this has just give me a list of numbers that have been incremented by 1, how would i return the count?

Thanks alot for your help, sorry im not picking this up as well :roll:

Horatio Westock
Ranch Hand
Posts: 221
The values in the counters array are the totals, you don't need to assign anything to them, only increment them when you come accross that face value.

I don't know what the variable dices is, so you may need to use something other than size:

[ February 27, 2005: Message edited by: Horatio Westock ]

Horatio Westock
Ranch Hand
Posts: 221
Was that page serious? Did someone really publish that algorithm? I didn't want to pull up the PDF and read the sordid history of its publication, but if you know, please do tell.

I think the confusion is that it doesn't find the item with the highest number of occurances, it finds the majority item, if there is one. i.e. the item that occurs more than half of the time.

David Harkness
Ranch Hand
Posts: 1646
Originally posted by Horatio Westock:
I think the confusion is that it doesn't find the item with the highest number of occurances, it finds the majority item.
Ohhhh, okay. Yes, I wasn't paying close enough attention and assumed it was about the one with the largest number of occurances. Thanks for clarifying.

David Harkness
Ranch Hand
Posts: 1646
Originally posted by Horatio Westock:
Note that this code assumes that getFaceNumber() returns a value in [0,5] instead of [1,6]. I haven't read the rest of the code, so it may do that, but just make sure you match them up correctly.

Horatio Westock
Ranch Hand
Posts: 221
Originally posted by David Harkness:
<hr></blockquote>Note that this code assumes that getFaceNumber() returns a value in [0,5] instead of [1,6]. I haven't read the rest of the code, so it may do that, but just make sure you match them up correctly.

Good point!

Andy Rayner
Ranch Hand
Posts: 36
tempDice.getFaceNumber() returns 1-6 and the variable dices is an array, when i dice is created this is what happens

i put in the code you said and got quite an unusual result, it only looked at the first 5 dice not all 6 of them and i got this result
There were 7 0s
There were 4 1s
There were 2 2s
There were 4 3s

on a line which had 6 3 1 3 2 3

Andy Rayner
Ranch Hand
Posts: 36
hehe nevermind in the end i got it working,
Thanks for all your help, just a quick question
is there a method in an array to get the highest value?

Layne Lund
Ranch Hand
Posts: 3061
Originally posted by Andy Rayner:
tempDice.getFaceNumber() returns 1-6 and the variable dices is an array, when i dice is created this is what happens

i put in the code you said and got quite an unusual result, it only looked at the first 5 dice not all 6 of them and i got this result
There were 7 0s
There were 4 1s
There were 2 2s
There were 4 3s

on a line which had 6 3 1 3 2 3

You say that the variable dices is an array, but in the code above, you do "dices.add(d)". Arrays do NOT have member methods like add(). This indicates to me that dices is probably a Vector or an ArrayList. I just thought I'd point this out because arrays are VERY different from Vectors and ArrayLists. The later two are classes from the Collections Framework, but arrays are built-in types. The syntax for ArrayLists is quite different than that for arrays. IMHO, if you don't understand the difference, you should do a little more research to figure it out

Keep Coding!

Layne

Andy Rayner
Ranch Hand
Posts: 36
Sorry i meant an ArrayList *smiles* my program is working now, thank you for your help

Horatio Westock
Ranch Hand
Posts: 221
Great - glad you got it working!

Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
I think the confusion is that it doesn't find the item with the highest number of occurances, it finds the majority

Might be. At the end of the demo it says this ... which I find a bit less than clear ...

Note that if you replaced the first C with an A, above, the algorithm would still end with C being chosen, but in fact C would not be the majority element: there is no majority in the modified list.

In some situations you know, or assume, there is a majority element.

But if you must confirm that the chosen element is indeed the majority element, you must take one more linear pass through the data to do it.

I've stepped through the interactive demo there and no lights went on in my head. The authors are Boyer & Moore, who I knew from their String Search algorithm. The interactive demo of the string search is very cool, tho. The longer the needle the faster it goes, and it's sub-linear on the haystack length in many cases.

BTW: Glad the original problem went well in the end. I was away a day and missed about 50 posts!

Andy Rayner
Ranch Hand
Posts: 36
Hello sorry to bring this topic up again its about the dice again *grins*
Everything seems to be working fine
the problem is, it only gives me the most number on the highest
for example if i have

6 6 2 2 2 1 i get the result 2
or
4 4 4 5 5 3 i get the result 2

but if i have

6 6 6 4 4 2 i get 3, but only because 6 is the highest
I cant seem to figure out how to work it so i can get the highest number from any dice, here is the code i am using

David Harkness
Ranch Hand
Posts: 1646
The second for loop can be written in psuedocode like this:Now walk through this with your examples and see if you can determine the problem and a solution. In these cases it helps to play "Be the JVM" and step through your code on paper.