简体   繁体   中英

Vertical Histogram Printing Issue

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");
    }
}

So I have this code now. I get 9 number inputs from user and convert it to vertical histogram. For example when the user enters 1-2-2-4-.... and enters width as 3; the output is:

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

I want it to be like :

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

Is there any way to achieve this output with my code ? Sorry if I am unclear, I'm not good at English. I'm also a newbie at C programming, still trying to understand its behaviour. Thank you !

This code works as you said

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM