Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Find the mode
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
nis




PostPosted: 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:
code:

1
2
6
4
2
8
5
7
2
2
1


the mode would be 2.

Could some one help me with this thanks
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Thu Nov 06, 2003 7:39 pm   Post subject: (No 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.
code:

for i:1..length(arraySize)
counter(yourArray(i))+=1
end for

then sord your counter array in desending order.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
thoughtful




PostPosted: Thu Nov 06, 2003 7:56 pm   Post subject: (No 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.
nis




PostPosted: Thu Nov 06, 2003 11:14 pm   Post subject: (No subject)

The problem is that i am sorting real numbers and i have know idea how many decimals there will be
Tony




PostPosted: Thu Nov 06, 2003 11:18 pm   Post subject: (No subject)

why would desimal places matter? Confused
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
nis




PostPosted: Thu Nov 06, 2003 11:42 pm   Post subject: (No 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

code:


proc Mode (var std : array 1 .. 1000 of student, Entries : int)
    var CurrentMark : real
    var DisplayedMarkNum : int
    var HighestAmount : int := 0
    var CommonNum : real := 0
    Sort_By_Mark (std, Entries)
    DisplayedMarkNum := 1
    for i : 1 .. Entries - 1
        CurrentMark := std (i).avgmark
        if std (i + 1).avgmark = CurrentMark then
            DisplayedMarkNum += 1
            if DisplayedMarkNum >= HighestAmount then
                HighestAmount := DisplayedMarkNum
                CommonNum := std (i + 1).avgmark
            end if
        else
            DisplayedMarkNum := 1
        end if
    end for
    put "There mode is ", CommonNum, " and it occured ", HighestAmount, " times."
    put " "
    put "Press any key to go back to the menu"
    View.Update
    Input.Pause
end Mode

Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: