hm, a few ideas came to my mind, but they all seem horribly inelegant.
1. make two new arrays, one storing the values and another storing their counts.
2. (probably easiest) for each index of the array, going through the array and finding counts. You'll also need to store the number with the highest count found so far and it's count.
3. sorting the array first, and then counting how many times a number appears in a row. You'll also need to keep track of the highest count and its corresponding number.
int mode(int *array, int size) {
int mode;
count = 0;
for (int *p = array; p < array + size; ++p) // for each value of array
int count2 = std::count(array, array + size, *p;
if (count2 > count) { // if it's count is larger than the highest count found so far
mode = *p; // store it's value
count = count2; // and it's count
}
}
return mode;
}
int size, *values;
cout << "Size = ? ";
cin >> n;
values = newint[size];
for( int i=0; i<size; i++)
{
cout << "values[" << i << "] = ? ";
cin >> values[ i ];
}
// Now use values as if it were a normal array.
To calculate mode, you can follow the following approach.
Note: This method is O(n Log n). There may be an O(n) method. I am not very sure of it. If you don't know the O notation, then ignore it. It's a measure of how complex an algorithm is.
ok... i do not understand this, im not really experienced... could somebody give me the whole program? I tried msrams (the program right above) but I dont get it? could someone give it to me?