Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
Find the mode
Author Message
nis

Posted: 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

Tony

Posted: 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.
Tony's programming blog. DWITE - a programming contest.
thoughtful

Posted: 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

Posted: 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

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

why would desimal places matter?
Tony's programming blog. DWITE - a programming contest.
nis

Posted: 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: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 1  [ 6 Posts ]
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: