【发布时间】:2018-10-20 14:06:51
【问题描述】:
我的任务是为一组数字(0 到 100)找到所有可能的模式。
我们被告知要使用数组来计算每个数字出现的频率。
我已经为模式编码,但是,我的程序无法工作,因为有多种模式(例如:1, 2, 7, 7, 9, 10, 7, 2, 2。在这种情况下,2 和 7 都是模式,我的程序需要打印它们,但我的没有)。
我想我可能需要制作另一个数组集,但我不确定?任何意见,将不胜感激。
这是我所拥有的:
#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) { //setting all values to 0
c[i] = 0;
i = i + 1;
}
scanf("%d", &x); // scanning in the test scores
while ((x >= 0) && (x <= 100)) { // counting how often each score appears
c[x] = c[x] + 1;
if (c[x] >= mode) {
mode = x;
}
scanf("%d", &x);
}
printf("THE MODE(S) ARE %d\n", mode);
i = 0;
while (i <= 100) { //printing all values so long as they've occurred at least once
if (c[i] > 0) {
printf("%d occurs %d times\n", i, c[i]);
}
i = i + 1;
}
}
【问题讨论】:
-
不想回答作业,一些常见的数据结构可能会帮助你。也许研究堆栈,当你找到新模式时想想条件。
-
您有模态值
mode,并且您有一个用于打印所有值的循环,因此您可以通过仅打印计数等于模式的值来组合两者。 (c[i] == c[mode])。哦,请使用比c更长的变量名。还有一点 -mode是模态值还是模态值c的索引?它被用作两者...... -
你只扫描一个测试分数。
-
我很难理解这个问题
-
我对什么是“模式”感到困惑。根据您的代码,
mode是直方图单元格的 最大值 值。但是,您的printf暗示这是一个计数(例如MODE(S))。如果它是大于一的非零直方图单元数的计数(即c),则将您的if替换为if (c[x] == 2) mode++;???