在 C 中为多种模式编码?
Coding for multiple modes in C?
我的任务是找出一组数字(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;
}
}
您必须计算任何数字的最高频率,如果该频率等于任何其他数字的频率,则该数字也将是众数。
因此,您需要做的更改是:
#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;
}
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 = c[x];}
scanf("%d", &x);
}
for(i=0;i<=100;i++){//printing all values having highest frequency
if (c[i]==mode)
{
printf("THE MODE(S) ARE %d\n", i);
}
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;
}
}
您应该确定最大计数,而不是在主入口循环中确定模式。然后,您可以在最后一个循环中使用此出现次数打印所有值。
您还应该检查 scanf()
的 return 值。
我还建议对数组使用初始化程序以避免循环,并使用 for
循环更清楚地标识循环索引的初始化、测试和增量。
这是您的代码的更正版本:
#include <stdio.h>
int main() {
int x, i, c[101] = { 0 }, max_repeat;
printf("please enter test scores between 0 and 100\n");
max_repeat = 0;
// read the test scores and compute the maximum repeat count
while (scanf("%d", &x) == 1 && x >= 0 && x <= 100) {
c[x] += 1;
if (max_repeat < c[x]) {
max_repeat = c[x];
}
}
printf("The mode(s) are");
for (i = 0; i <= 100; i++) {
if (c[i] == max_repeat) {
printf(" %d", i);
}
}
printf("\n");
return 0;
}
我的任务是找出一组数字(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;
}
}
您必须计算任何数字的最高频率,如果该频率等于任何其他数字的频率,则该数字也将是众数。 因此,您需要做的更改是:
#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;
}
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 = c[x];}
scanf("%d", &x);
}
for(i=0;i<=100;i++){//printing all values having highest frequency
if (c[i]==mode)
{
printf("THE MODE(S) ARE %d\n", i);
}
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;
}
}
您应该确定最大计数,而不是在主入口循环中确定模式。然后,您可以在最后一个循环中使用此出现次数打印所有值。
您还应该检查 scanf()
的 return 值。
我还建议对数组使用初始化程序以避免循环,并使用 for
循环更清楚地标识循环索引的初始化、测试和增量。
这是您的代码的更正版本:
#include <stdio.h>
int main() {
int x, i, c[101] = { 0 }, max_repeat;
printf("please enter test scores between 0 and 100\n");
max_repeat = 0;
// read the test scores and compute the maximum repeat count
while (scanf("%d", &x) == 1 && x >= 0 && x <= 100) {
c[x] += 1;
if (max_repeat < c[x]) {
max_repeat = c[x];
}
}
printf("The mode(s) are");
for (i = 0; i <= 100; i++) {
if (c[i] == max_repeat) {
printf(" %d", i);
}
}
printf("\n");
return 0;
}