【问题标题】:Coding for multiple modes in C?在 C 中编码多种模式?
【发布时间】:2018-10-20 14:06:51
【问题描述】:

我的任务是为一组数字(0 到 100)找到所有可能的模式。

我们被告知要使用数组来计算每个数字出现的频率。

我已经为模式编码,但是,我的程序无法工作,因为有多种模式(例如:1, 2, 7, 7, 9, 10, 7, 2, 2。在这种情况下,27 都是模式,我的程序需要打印它们,但我的没有)。

我想我可能需要制作另一个数组集,但我不确定?任何意见,将不胜感激。

这是我所拥有的:

#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++; ???

标签: c arrays mode


【解决方案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;
   }
}

【讨论】:

    【解决方案2】:

    您应该确定最大计数,而不是确定主入口循环中的模式。然后,您可以在最终循环中打印具有此出现次数的所有值。

    您还应该检查scanf() 的返回值。

    我还建议对数组使用初始化程序以避免循环,并使用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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-25
      相关资源
      最近更新 更多