c - 垂直直方图打印问题

标签 c histogram

for (a = 0; a < 9; a++)
{
    if (hm <= arr[a]) //hm is Maximum number in array for height of a column.
        hm = arr[a];
}
for (i = hm; i >= 0; i--)
{
    for(t = 0; t < width; t++) //Width is where i got in trouble.
    { 
        printf("|");
        for (a = 0; a < 9; ++a)
        { 
            if (arr[a] > i)
            {       
                printf("*|");
            }
            else
            {
                printf(" |");
            }
        }
        printf("\n");
    }
}

所以我现在有了这段代码。我从用户那里得到 9 个数字输入并将其转换为垂直直方图。例如,当用户输入 1-2-2-4-.... 并输入宽度为 3 时;输出是:

  | |*|         //Prints "width" as height.
  | |*|
  | |*|
  |*|*|
  |*|*|
  |*|*|.....

我希望它像:

  |   |   |
  |   |   |
  |   |***|
  |***|***|.....
    1   2

有什么方法可以用我的代码实现这个输出吗?对不起,如果我不清楚,我不擅长英语。我也是 C 编程的新手,仍在努力理解它的行为。谢谢!

最佳答案

这段代码如你所说的那样工作

for (int a = 0; a < 9; a++) {
    if (hm <= arr[a]) //hm is Maximum number in array for height of a column.
        hm = arr[a];
}
for (int i = hm; i >= 0; i--) {
    printf("|");
    //for(int t = 0; t < width; t++){ //Width is where i got in trouble.
        //printf("|");
        for (int a = 0; a < 9; ++a) {
            if (arr[a] > i) {
                for(int t = 0; t < width; t++){ //Here where you should have added the for loop
                    printf("*");
                }
                //printf("*|");
                printf("|");
            }
            else{
                for(int t = 0; t < width; t++){ //Here where you should have added the for loop
                    printf(" ");
                }
                printf("|");
                //printf(" |");
            }
        }
        printf("\n");
    //}
}
return 0;

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

相关文章:

python - 去掉条形图比例中的微小分数(Pandas/Python)

c - 错误 : initializer element is not constant

C - while 循环中的 fscanf 跳过用户输入

C语言-if语句问题

r - ggplot2 geom_bar位置失败

opencv - 如何计算亮度直方图?

c - OpenGL矩形动画

c - pragmaatomic 的无效形式

python - 打印直方图

r - 如何在 R 中将密度表示为百分比?