如果有多种模式,我如何为模式编程?
How can I program for mode if there are multiple modes?
如果只有一种模式,我可以编写程序来查找模式。但是,我不确定如何更改我的代码,以便在存在多种模式时它可以正常运行。
这就是我所拥有的!任何有关如何修复我的代码的建议都将不胜感激。
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main ()
{
int x, i, c[101], mode;
printf("please enter test scores between 0 and 100\n");
i = 0;
mode = 0;
while (i<=100)
{
c[i]=0;
i=i+1;
}
scanf("%d", &x);
while ((x>=0) && (x<=100))
{
c[x]=c[x]+1;
if (c[x]>=mode)
{mode=x;}
scanf("%d", &x);
}
printf("the mode is %d\n", mode);
}
你想要:
a) 有一些东西可以跟踪每个值出现的频率。您已经为此使用了 "occurrence counts" 数组。
b) 找到最高的"occurrence count"
c) 找到共享最高 "occurrence count"
的所有值
对于您的代码,这主要可以通过替换来完成:
if (c[x]>=mode)
{mode=x;}
..更像是:
if (c[x] > highest)
{highest = c[x];}
..然后在最后做这样的事情:
printf("the mode/s are:");
for(i = 0; i <= 100; i++) {
if(c[i] == highest) {
printf(" %d", i);
}
}
printf("\n");
如果只有一种模式,我可以编写程序来查找模式。但是,我不确定如何更改我的代码,以便在存在多种模式时它可以正常运行。
这就是我所拥有的!任何有关如何修复我的代码的建议都将不胜感激。
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main ()
{
int x, i, c[101], mode;
printf("please enter test scores between 0 and 100\n");
i = 0;
mode = 0;
while (i<=100)
{
c[i]=0;
i=i+1;
}
scanf("%d", &x);
while ((x>=0) && (x<=100))
{
c[x]=c[x]+1;
if (c[x]>=mode)
{mode=x;}
scanf("%d", &x);
}
printf("the mode is %d\n", mode);
}
你想要:
a) 有一些东西可以跟踪每个值出现的频率。您已经为此使用了 "occurrence counts" 数组。
b) 找到最高的"occurrence count"
c) 找到共享最高 "occurrence count"
的所有值对于您的代码,这主要可以通过替换来完成:
if (c[x]>=mode)
{mode=x;}
..更像是:
if (c[x] > highest)
{highest = c[x];}
..然后在最后做这样的事情:
printf("the mode/s are:");
for(i = 0; i <= 100; i++) {
if(c[i] == highest) {
printf(" %d", i);
}
}
printf("\n");