473,394 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

How to calculate mode

Hi there, I don't know if the answer to this has been posted before, but I'm having some problem calculating the Mode for a dynamic array in C++.

>
cout << "Enter the size of your array: ";
cin >> aSize;
int *newArray = new int[aSize];
runMode(newArray, aSize);
< this isi in my main
--------------------------------------
void runMode( int intArray[], int arraySize )
{
cout << "Enter in the numbers: ";
for (int i = 0; i < arraySize; i++)
{
cin >> intArray[i];
}//for loop for putting numbers into the array


}// runMore function

and that's the function I'm using, any ideas on how to calculate mode would be appreciated.
Thanks
Oct 5 '06 #1
3 14049
tyreld
144 100+
First start by sorting the array. Then loop through the array keeping track of the current number and how many times it appears. If it is more then then the previously most seen number update that value.

Here is some psuedo code.

Expand|Select|Wrap|Line Numbers
  1. sort ARRAY
  2.  
  3. num = 0
  4. count = 0
  5. max = 0
  6. mode = 0
  7.  
  8. for each i in ARRAY do
  9.  
  10.   if num not equal to ARRAY[i] then
  11.     num = ARRAY[i]
  12.     count = 1
  13.  
  14.   else
  15.     count++
  16.  
  17.   endif
  18.  
  19.   if count > max then 
  20.     max = count
  21.     mode = num
  22.  
  23.   endif
  24.  
  25. next
  26.  
Oct 5 '06 #2
Thank you so much :o)
Oct 11 '06 #3
Be aware that there can be more than one mode in a dataset.

JT
Jul 23 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Phil Powell | last post by:
Relevancy scores are normally defined by a MySQL query on a table that has a fulltext index. The rules for relevancy scoring will exclude certain words due to their being too short (minimum...
1
by: Building Blocks | last post by:
Hi, All I need is a simle calculate form script which contains this: A script that can handle text input, radio buttons, checkboxes, and dropdowns. Each one of these variables will contain a...
14
by: delerious | last post by:
I need to determine an element's width and height in pixels (not including padding, border, and margin) in Javascript. The element will not have width or height styles specified. In Mozilla, I...
5
by: mp141 | last post by:
Hello, I was wondering if there was an easy way for me to calculate the mode for a field of values in one query. I have one field of values say Names and all I want it to return is one record of...
4
by: Jan Szymczuk | last post by:
I'm creating an MS Access 2000 database where I have a number of people entered using simple basic fields, Surname: SMITH Forenames: John DoB: 09/09/1958 Age:...
1
by: Sam | last post by:
I successfully created DataGrid with connected environment but how do I perform sum of TotalRBAmount in DataGrid? Coding of ASP.Net 1.1 ------------------------ <form runat="server">...
6
by: PyPK | last post by:
how can we compute the current system disk space using a python script.? any ideas or have anyone tried this..
3
by: Carles Company Soler | last post by:
Hello, I want to calculate the value of an attribute. For example <rect x="2+3" y="12"and be <rect x="5" y="12">. Is it possible using XSLT? Thanks!
6
by: LaundroMat | last post by:
Hi - I'm trying to calculate unique hash values for binary files, independent of their location and filename, and I was wondering whether I'm going in the right direction. Basically, the hash...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.