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/