c - 打印垂直直方图

标签 c arrays histogram

我正在尝试打印一个垂直直方图,用于计算用户输入的每个数字的频率。 我首先存储频率如下:

 int a[10];  //array
 int c;      //store input from getchar
 int i;      //loop variable

 for(i=0;i<10;i++)
 {

    a[i]=0;   //initialize to 0
 }
 while((c=getchar())!=EOF)  //read character
 {
    ++a[c-'0'];
 }

接下来,我尝试绘制垂直直方图。

for(i=10;i>0;i--)  //asssumed max limit of frequency is 10
{
  int j;          //iterate through the array
  for(j=0;j<10;j++)
{
 if(a[j]==i)     //if frequency of any element in array matches i 
 {
  printf("* \t");
  --a[j];       //decrement array element frequency value
 }
 else
 printf(" \t");  //no action
 }
 printf("\n");   //next line
}

问题是当我尝试打印直方图时,我得到一个空白屏幕。我已经用水平直方图对其进行了测试,它有效。

最佳答案

准确地采用您的代码,对其进行格式化以提高可读性,并添加检查以确保仅使用数字以避免进一步破坏内容,它起作用了。对于功能而言,该检查不是必需的,但避免写入数组范围之外是个好主意。

#include <stdio.h>
#include <ctype.h>

int main() {
    int a[10];  //array
    int c;      //store input from getchar
    int i;      //loop variable

    for(i=0;i<10;i++)
    {
        a[i]=0;   //initialize to 0
    }
    while((c=getchar())!=EOF)  //read character
    {
        if (isdigit(c))
            ++a[c - '0'];
    }

    for(i=10;i>0;i--)  //asssumed max limit of frequency is 10
    {
        int j;          //iterate through the array
        for(j=0;j<10;j++)
        {
            if(a[j]==i)     //if frequency of any element in array matches i 
            {
                printf("* \t");
                --a[j];       //decrement array element frequency value
            }
            else
                printf(" \t");  //no action
        }
        printf("\n");   //next line
    }
}

结果:

$ ./a
0123456789666338592






                                                *
                        *                       *
                *       *               *       *               *       *
*       *       *       *       *       *       *       *       *       *

仅供引用,这是为 Cygwin 编译的。 我还通过几种方式修改了您的源代码,以符合我遵循的一些做法:

#include <stdio.h>
#include <ctype.h>

int main()
{
    int a[10] = {0}; // While use a for to initialize to 0 when you can do it easily?
    int i;
    char c;

    while((c = getchar()) != EOF)
    {
        if (isdigit(c))    // Never trust the user
            a[c - '0']++;  // Changed for readability, make it clear that we are incrementing the value, not the pointer.
    }

    for(i = 10; i > 0; --i)
    {
        int j;
        for(j = 0; j < 10; ++j)
        {
            // Made flow a little clearer
            if(a[j] >= i)
                putchar('*');
            else
                putchar(' ');
            putchar('\t');
        }
        putchar('\n');
    }
}

你做什么当然取决于你,我个人觉得这样更一致。

关于c - 打印垂直直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50530361/

相关文章:

c - 如何更新forloop流程中的变量?

c - 将字符串转换为 double 后返回值不正确?

arrays - 在 Swift 中拆分具有多个字符的字符串

python - 如何调整 pandas 直方图上的刻度标签?

c++ - 如何将 c++ 接口(interface) cv::Mat 转换为 c IplImage?

c - 静态与动态链接

arrays - strb w1, [x22,x23] 当x23达到一定值时导致的段错误

javascript - Angular 2 : Convert a complex JSON Object to a Simple one

bash - gnuplot 直方图 : line 0: Too many columns in using specification

python - OpenCv RGB 直方图反投影未按预期工作