I'm trying to make a vertical histogram. I'm a little confused because in the instructions it says "A standard terminal window is 80 character-columns across. If we limit the number of columns we can graph to a maximum of 80, we know how large an array we need to allocate and this program is simple to write."
What I understand from this is to create an array of length 80 and store input from scanf.
int arr[80];
for(i=0;i<80;i++)
{
scanf("%d", &arr[i]);
}
Then I find the maximum element in the array and use a while loop to print the histogram:
max = arr[0];
for(i=0;i<80;i++)
{
if(arr[i]>max)
{
max = arr[i];
}
}
while(max!=0)
{
for(k=0;k<80;k++)
{
if(arr[k]<max)
{
printf(" ");
}
else
{
printf("#");
}
}
printf("\n");
max--;
}
However when I run the program, nothing prints so I don't think it even reaches that point...I have not learned about malloc yet so I know I don't have to use that. Here is an example of what it should look like:
Input:
1 4 2 3
Output:
#
# #
# # #
# # # #
You are probably running into problems with this:
scanf("%d", &arr[i]);
since there is no delimiter (such as a newline or space) which scanf
can use to decide when a number is completed. People work around scanf
limitations by adding a dummy parameter to absorb the whitespace, eg,
scanf("%d%s", &arr[i], dummy);
However that runs into problems with the length of the dummy parameter.
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.