edited by
538 views
2 votes
2 votes
Given an array $A$ of n positive integers, write a program segment / pseudo-code to print the histogram of $A$ using the hash character ($\#$). Your histogram should consist of $n$ vertical columns of $\#$ with the $i$-th vertical bar containing $A[i]$ number of $\#$’s. For example, if you consider $A$ as the array $\{4, 1, 2, 1\}$, your output should look like

$\begin{array}{llll} \# \\ \# \\ \# & & \# & \\ \# & \# & \# & \# \end{array}$
edited by

1 Answer

0 votes
0 votes
#define max(x,y)   (x) > (y) ? (x) : (y)
#include <stdio.h>
int main()
{
    int i,j,n;
    scanf("%d",&n);
    int arr[n];
    int m=-9;
    for(i=0;i<n;i++)
    {
    scanf("%d",&arr[i]);
    m=max(arr[i],m);    
    }
    for(i=m;i>0;i--)
    {
    for(j=0;j<4;j++)
    {
    if(arr[j]>=i)
    printf("# ");
    else
        printf("  ");
    
    }
    printf("\n");    
    }   
    return 0;
}

Related questions

2 votes
2 votes
3 answers
1
go_editor asked May 29, 2016
699 views
Consider all possible permutations of eight distinct elements $a, b, c, d, e, f, g, h$. In how many of them, will $d$ appear before $b$? Note that $d$ and $b$ may not nec...
3 votes
3 votes
1 answer
2
go_editor asked May 29, 2016
670 views
Prove that in any sequence of $105$ integers, there will always be a subsequence of consecutive elements in the sequence, whose sum is divisible by $99$.