它计算模式,但如果没有任何重复,我希望它能够“无模式”。我尝试了diff方法,但每次只给我一个数字的数组,而没有假设只打印没有模式这是jus模式计算功能我必须从txt文件中获取数据并对其进行排序然后计算模式< / p>
//calculating mode now
int num, count;
double max, mode;
for (int i = 0; i < numScores; i++){
if (num !=*(pscores+i) ){
num = *(pscores+i);
count = 1;
} else {
count++;
}
if (count > max){
max = count;
mode = num;
}
}
cout<<"Mode : "<<mode<<endl;
答案 0 :(得分:1)
除mode
时,您永远不会初始化count > max
。你的if条件也将永远不会起作用,因为你也没有初始化max
。假设你的“无模式”条件是count <= max
时的情况,只需在其周围放置一个,并相应地设置模式和最大值。
//calculating mode now
int num = -1, count = 0;
double max = /* whatever */, mode = 0.0;
for (int i = 0; i < numScores; i++)
{
if (num !=*(pscores+i))
{
num = *(pscores+i);
count = 1;
}
else
count++;
if (count > max)
{
max = count;
mode = num;
/* maybe you want to break here? */
}
}
if (mode == 0.0) /* this is dangerous (mode might be 0.00000001 != 0.0) */
cout << "Mode: " << mode << endl;
else
cout << "Mode: no mode" << endl;