【问题标题】:Vertical Histogram Printing Issue垂直直方图打印问题
【发布时间】:2014-12-08 07:16:35
【问题描述】:
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 编程的新手,仍在尝试了解它的行为。谢谢!

【问题讨论】:

  • 您的问题是什么?此外,您应该正确缩进代码以使其可读。
  • Okey 我曾经在 NetBeans 自动格式化我的代码,但由于我使用的是 Dev c++,我找不到用于格式化我的代码的快捷键。这种格式对我来说更容易理解。

标签: c histogram


【解决方案1】:

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

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;

【讨论】:

    猜你喜欢
    • 2016-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-17
    • 1970-01-01
    • 1970-01-01
    • 2015-06-24
    • 1970-01-01
    相关资源
    最近更新 更多