Mode? | | |
What is the easiest way to program a way to figure mode? I am just
having to do it with some simple Int.... Any suggestions? | | | | re: Mode?
Megantsu <MeganTSU@gmail.com> wrote:[color=blue]
> What is the easiest way to program a way to figure mode? I am just
> having to do it with some simple Int.... Any suggestions?[/color]
I would suggest looking at std::map<>, mapping a number to its count,
then return the key with the largest value. If you have any questions,
post your code.
--
Marcus Kwok | | | | re: Mode?
Megantsu wrote:[color=blue]
> What is the easiest way to program a way to figure mode? I am just
> having to do it with some simple Int.... Any suggestions?[/color]
Here is a hint: http://www.mathsisfun.com/mode.html | | | | re: Mode?
Here is one way I did. I used int, but you can ofcourse typedef it to
use anything. It tells everything, mean, median, mode. using a vector.
You could compile this and run to see what it does.
int main(){
vector<int> myArr;
int i;
cout << "Enter as many integers you like seperated by enter, when
tired type q" << endl;
while( cin >> i)
myArr.push_back(i);
vector<int>::size_type sz = myArr.size();
if(!sz) return 0; //if no input then exit
sort(myArr.begin(), myArr.end());
cout << "Sorted order: ";
std::copy(myArr.begin(), myArr.end(), std::ostream_iterator<int>(cout,
","));
cout << endl;
cout << "Max of your input is: " << myArr[sz-1] << endl;
cout << "Min of your input is: " << myArr[0] << endl;
cout << "Mean of your input is: " << (accumulate(myArr.begin(),
myArr.end(), 0.0))/sz << endl;
if((sz%2)==0)
cout << "Median of your input is: " << (myArr[sz/2] +
myArr[(sz/2)+1])/2 << endl;
else
cout << "Median of your input is: " << myArr[(sz+1)/2] << endl;
int max = myArr[myArr.size()-1];
vector<int>::const_iterator iter;
//initalize the arry
vector<int> arr(max, 0);
//insert the approrpate counts in the array.
for(iter = myArr.begin(); iter != myArr.end(); iter++){
arr[*iter]++;
}
int prevHighCount = 0;
int answer =0 ;
for(int i = 0; i!= max; i++){
if(arr[i] > prevHighCount){
prevHighCount = arr[i];
answer = i;
}
}
cout << "Mode of your input is: " << answer << endl;
cin.clear();
cin.ignore();
cout << "Enter a number to find: " << endl;
cin >> i;
iter=find(myArr.begin(), myArr.end(), i);
if(iter==myArr.end())
cout << i << " does not exist in the input" << endl;
else
cout << i << " exist in the input at index " << iter - myArr.begin();
return 0;
} | | | | re: Mode?
On 2005-12-01, pepp <pepphell@gmail.com> wrote:[color=blue]
> Here is one way I did. I used int, but you can ofcourse typedef
> it to use anything. It tells everything, mean, median, mode.
> using a vector. You could compile this and run to see what it
> does.[/color]
Don't forget your headers.
#include <vector>
#include <algorithm>
#include <numeric>
#include <iostream>
#include <ostream>
#include <iterator>
using namespace std;
[color=blue]
> int main(){
> vector<int> myArr;
> int i;
> cout << "Enter as many integers you like seperated by enter, when
> tired type q" << endl;
> while( cin >> i)
> myArr.push_back(i);
>
> vector<int>::size_type sz = myArr.size();
> if(!sz) return 0; //if no input then exit[/color]
This is much better written as if (sz == 0).
Yes, it works fine the way it's written, and it isn't actually
very confusing, but my suggestion is clearer because it says what
you really mean.
[color=blue]
> sort(myArr.begin(), myArr.end());
> cout << "Sorted order: ";
> std::copy(myArr.begin(), myArr.end(), std::ostream_iterator<int>(cout,
> ","));[/color]
That leaves a trailing comma.
[color=blue]
> cout << endl;
> cout << "Max of your input is: " << myArr[sz-1] << endl;
> cout << "Min of your input is: " << myArr[0] << endl;
> cout << "Mean of your input is: " << (accumulate(myArr.begin(),
> myArr.end(), 0.0))/sz << endl;
> if((sz%2)==0)
> cout << "Median of your input is: " << (myArr[sz/2] +
> myArr[(sz/2)+1])/2 << endl;
> else
> cout << "Median of your input is: " << myArr[(sz+1)/2] << endl;[/color]
You do not calculate the median properly.
[color=blue]
> int max = myArr[myArr.size()-1];
> vector<int>::const_iterator iter;
>
> //initalize the arry
> vector<int> arr(max, 0);[/color]
This is a bad idea.
First, it is buggy. What happens if my list contains negative
numbers?
Second, it possibly consumes ridiculous amounts of memory. What
if my list is (0, 4, INT_MAX)?
You don't calculate mode properly. If no number appears more than
once, then there is no mode. If more than one number repeats the
maximum number of times, then there are multiple modes. A one
element list may be a special case you need to handle.
You don't need an associative container at all. Try to think of a
better algorithm.
Here's a hint:
vector<int> modes(sz);
int max_repeats = 0;
for (iter = myArr.begin(); iter != myArr.end(); ++iter) {
// Do something here to populate the modes vector
}
if (modes.size() == 0) {
cout << "There is no mode." << endl;
} else {
cout << "The mode";
if (modes.size() > 1) {
cout << "s are ";
} else {
cout << " is ";
}
copy(modes.begin(), modes.end(), ostream_iterator(cout, " "));
cout << endl;
}
[color=blue]
> //insert the approrpate counts in the array.
> for(iter = myArr.begin(); iter != myArr.end(); iter++){
> arr[*iter]++;
> }
>
> int prevHighCount = 0;
> int answer =0 ;
> for(int i = 0; i!= max; i++){
> if(arr[i] > prevHighCount){
> prevHighCount = arr[i];
> answer = i;
> }
> }
>
> cout << "Mode of your input is: " << answer << endl;
> cin.clear();
> cin.ignore();
> cout << "Enter a number to find: " << endl;
> cin >> i;
> iter=find(myArr.begin(), myArr.end(), i);[/color]
Since the array is sorted, you could use lower_bound (an
implementation of binary search) instead to speed things up.
[color=blue]
> if(iter==myArr.end())
> cout << i << " does not exist in the input" << endl;
> else
> cout << i << " exist in the input at index " << iter - myArr.begin();
> return 0;
> }[/color]
--
Neil Cerutti | | | | re: Mode?
"Neil Cerutti" <leadvoice@email.com> wrote in message
news:slrndou361.1ms.leadvoice@FIAD06.norwich.edu.. .[color=blue]
> On 2005-12-01, pepp <pepphell@gmail.com> wrote:[/color]
....
[color=blue][color=green]
>> vector<int>::size_type sz = myArr.size();
>> if(!sz) return 0; //if no input then exit[/color]
>
> This is much better written as if (sz == 0).
>
> Yes, it works fine the way it's written, and it isn't actually
> very confusing, but my suggestion is clearer because it says what
> you really mean.[/color]
In that case why not:
if( myArr.empty() ) return 0;
Jeff Flinn | | | | re: Mode?
On 2005-12-01, Jeff Flinn <NONONE@nowhere.com> wrote:[color=blue]
>
> "Neil Cerutti" <leadvoice@email.com> wrote in message
> news:slrndou361.1ms.leadvoice@FIAD06.norwich.edu.. .[color=green]
>> On 2005-12-01, pepp <pepphell@gmail.com> wrote:[/color]
>
> ...
>[color=green][color=darkred]
>>> vector<int>::size_type sz = myArr.size();
>>> if(!sz) return 0; //if no input then exit[/color]
>>
>> This is much better written as if (sz == 0).
>>
>> Yes, it works fine the way it's written, and it isn't actually
>> very confusing, but my suggestion is clearer because it says what
>> you really mean.[/color]
>
> In that case why not:
>
> if( myArr.empty() ) return 0;[/color]
Ah! Even better. I never think to use that member. Thanks for the
correction.
--
Neil Cerutti |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,501 network members.
|