Computer Science Canada Find the mode |
Author: | nis [ Thu Nov 06, 2003 6:16 pm ] | ||
Post subject: | Find the mode | ||
I am supposed to make a program that finds the mode! (for anyone that dosn't know the mode is the most common number in a set of numbers. for example:
the mode would be 2. Could some one help me with this thanks |
Author: | Tony [ Thu Nov 06, 2003 7:39 pm ] | ||
Post subject: | |||
the most efficient way I could come up with on top of my head would be to sort the array and then go though it, keeping track of two numbers to compare. such as after the sort (in accending order in this example) Quote: 1 1 2 2 2 3 3 3 3 4 you start a forloop. if first two numbers match, you increase the counter. If next number doesn't match, its another number and you start second counter. When 3rd number doesn't match, you compare the first two, keep the largest and replace smaller one with new number. Another way would be to count each number.
then sord your counter array in desending order. |
Author: | thoughtful [ Thu Nov 06, 2003 7:56 pm ] |
Post subject: | |
Well if you know the ma integers you are going to have you , If your max integers are 100 you can do something like : var number:array 1..100 of int % keeps track of how many of each number % is there for each number that is inputed or randomized get that number lets say this number is stored in a temperary variable current_number. You can use this line for calculating the mode. number(current_number)+=1 % adds 1 to the array that keeps track of how % many of each number there are[/code] at the end you can check that which array is the biggest , that will be your mode. |
Author: | nis [ Thu Nov 06, 2003 11:14 pm ] |
Post subject: | |
The problem is that i am sorting real numbers and i have know idea how many decimals there will be |
Author: | Tony [ Thu Nov 06, 2003 11:18 pm ] |
Post subject: | |
why would desimal places matter? |
Author: | nis [ Thu Nov 06, 2003 11:42 pm ] | ||
Post subject: | |||
Thanks Tony i used your idea and it works fine thanks Here is the code for anyone that wants it O || || VVV VV V
|