aspose file tools*
The moose likes Beginning Java and the fly likes program calculating the mode Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "program calculating the mode" Watch "program calculating the mode" New topic
Author

program calculating the mode

Chase Becicka
Greenhorn

Joined: Feb 16, 2004
Posts: 23
ok heres the thing i am trying to create a program (in java of course) that will find the mode of ten seperately inputted numbers, no arrays, i hate them and i know theres plent of ways to get around them, anywho im not having any luck, help me please
Dmitry Melnik
Ranch Hand

Joined: Dec 18, 2003
Posts: 328
calculating what?
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

Defintion of 'mode' --> The mode of a set of data is the value in the set that occurs most often.

Chase-
So, you're trying to calculate the mode of a set of data, and you don't want to use an array. What have you tried? Where are you having trouble? What part are you not able to "get"? We'd love to help you, but we're not going to do the work for you. If you'd like to post your code and specifically explain what the problem is -- we'd be more than happy to help you de-bug the problem and maybe even offer some alternative solutions. How's that sound?
In the mean time, Im moving this to Java In General (Beginner) forum, where I think its more appropriate. Good luck!


- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Try to think up a solution in relatively plain english. Maybe

Now you have a handful of small problems:
How do you go through your input set?
How do you keep a counter for each number?
Where do you remember the highest counter so far?
Where do you remember the number that got the highest count?
And if you really want to blow your instructor away, show em this: http://www.cs.utexas.edu/users/moore/best-ideas/mjrty/index.html (Shucks, that only works if there is a MAJORITY number, I think)
Have fun! Don't take my word for that plain language algorithm. See if you can do better!


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Chase Becicka
Greenhorn

Joined: Feb 16, 2004
Posts: 23
ok there is nothing wrong with the code, but i just have no clue of the formula i am sposed to actually use, that is my only problem, and if you cant help that is fine
Derek Schmidt
Greenhorn

Joined: May 10, 2004
Posts: 2
im sitting right next to him and here is the problem that he is having he needs to write a program to calculate the code he has the code wrote out he just needs help figuring out the math like should he use a switch statement but the only problem is the user can enter any range of numbers
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11494
    
  16

What code do you have written out? What does it do/not do? What part are you having trouble with exactly?
People here love to help, but i'm not sure what your problem is. just telling us "it doesn't work" doesn't do any good. If the code isn't too long, you can use the code tags and post what you written so far, and someone will probably give you some suggestions.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Derek Schmidt
Greenhorn

Joined: May 10, 2004
Posts: 2
ok he has the input wrote out and what he is having problems with is the math cause he doesnt really know what to do like what system he should use or what so is there anyone that can help him figure out the math part of the program of like what he should do cause the user can enter any number
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11494
    
  16

i don't know what you mean by "what system should he use".
there really isn't a lot of math involved in figuring out the mode. you need to scan all the input values, and count how many times each unique value appears. for example, if the 10 input numbers are:
5,8,2,3,6,5,1,8,0,8

since 8 occurs more than any other number, the mode is 8. the only math involved is simple addition and doing some comparisons (i.e. is one value greater than another).
when you say "he has the input wrote", is he saving all 10 input numbers, so they can all be accessed later? or is he incrementing a counter for each as it is input?
again, it's much easier to help if you POST THE CODE. there are probably hundreds of ways to do this, but without knowing what path you've started, everybody is just making blind guesses on how to help you get further along.
[ May 11, 2004: Message edited by: fred rosenberger ]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Stan James has already posted pseudocode for the algorithm you can use. Now all you need to do is change it into Java code.
If you still don't understand, please post the code you already have and what problems you've encountered in trying to implement the code. You should also try writing out an explanation, in your native language, of the steps you should take to find the answer. Perhaps it will help to do an example by hand. Then you can more easily see what steps are needed.
HTH
Layne


Java API Documentation
The Java Tutorial
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: program calculating the mode