我需要編寫一個函數來查找數組的模式。我不擅長提出算法,但我希望別人知道如何做到這一點。如何找到排序數組的模式?
我知道數組的大小和每個元素中的值,並且我從最小到最大排序了數組。
陣列將被傳遞到模式功能等
模式= findMode(arrayPointer,sizePointer);
UPDATE:
閱讀評論之後,我已經試過抓住了這個算法,這
int findMode(int *arrPTR, const int *sizePTR)
{
int most_found_element = arrPTR[0];
int most_found_element_count = 0;
int current_element = arrPTR[0];
int current_element_count = 0;
int count;
for (count = 0; count < *sizePTR; count++)
{
if(count == arrPTR[count])
current_element_count++;
else if(current_element_count > most_found_element)
{
most_found_element = current_element;
most_found_element_count = current_element_count;
}
current_element = count;
current_element_count=1;
}
return most_found_element;
}
我仍然有問題,但如果任何人都可以整理我出去。 我從來沒有使用過載體,所以不太瞭解其他例子。
你能描述一些你嘗試過的東西,也許發佈一些代碼嗎?這裏給你一個提示:你可能試着循環訪問數組,每次看到一個元素時,都會將一個元素特定的計數器加1。 – asf107 2012-02-16 17:49:35
什麼是「陣列模式」? – wilhelmtell 2012-02-16 17:51:51
你想要一個數組的_mode_嗎?你的意思是_mean_?或_median_?或者我需要學習一個新的術語?編輯:[模式是一件真實的事情! IR LERND](http://www.mathsteacher.com.au/year8/ch17_stat/02_mean/mean.htm) – 2012-02-16 17:52:00