How make a bar chart in C?
I have written a program that prints out the integers in the same order as the input with 5 numbers per line. That is, the first 5 integers will be printed in the first line; the next 5 integers in the next line; and so on. I was also was trying to print out the numbers in a bar chart format, like,
81-105 ( 1) x
56-80 ( 5) xxxxx
6-11(5) xxxxx
-1-5 (3) xxx
My program:
cntr=0;
while (fscanf(in, "%d", &a[i]) != EOF)
{i++;
fprintf(out, "%d-%d (%d) %s\n", A, B, k, x, cntr);
fprintf(out, "%d\n", k, cntr);
fprintf(out, "x", a[i]);
i++;
}
fprintf(out, "1864-2336 (%d)%s\n", k, x);
fprintf(out, "1391-1863 (%d)%s\n", k, x);
fprintf(out, "918-1390 (%d)%s\n", k, x);
fprintf(out, 开发者_如何学运维"445-917 (%d)%s\n", k, x);
fprintf(out,"-28-444 (%d)%s\n", k, x);
fclose(in);
fclose(out);
return 0;
}
You don't have to use a loop to print out your x's. You can declare a single string of x's that's as long as the maximum you will need, and then use a size specification in the printf
call to control how many are printed. Here's a simple example to illustrate what I mean:
#include <stdio.h>
#include <string.h>
int main(void)
{
char maxBar[] = "xxxxxxxxxx";
size_t i;
for (i = 0; i < strlen(maxBar); i++)
{
printf("%lu: %-*.*s\n", (unsigned long) i, strlen(maxBar), i, maxBar);
}
return 0;
}
The output should look as follows:
0: __________
1: x_________
2: xx________
3: xxx_______
4: xxxx______
5: xxxxx_____
etc., where _
represents a space character.
A *
in a printf
conversion specifier indicates that a field width or precision specification is being passed as an argument to printf
. Writing
printf("%-*.*s\n", 10, 2, "test");
is the same as writing
printf("%-10.2s\n", "test");
which, reading left to right, means
-
: Left-justify the output10
: Minimum field width is 10 characters.2
: Precision (max number of chars printed) is 2
So the output looks like
te________
where _
represents a space character.
So, assuming you know how big your bar needs to be in advance, you can write something like
for (i = 0; i < k; i++)
{
printf("%d-%d (%d) %*.*s\n", lo[k], hi[k], ct[k], strlen(maxBar), ct[k], maxBar);
}
just do a loop to print the amount of x's you need
fprintf(out, "1864-2336 (%d)%s\n", k, x);
for(x=k;x>0;x--)
fprint(out,"%s","x");
fprintf(out,"\n");
Cheers!
/B2S
There are many ways to do this
It seems you already have somewhere declared an array of the values that user enters. a[]
In order to calculate the bar chart you could after user has entered all values, sort that array (e.g. using qsort()) then traverse the array checking the number of same values that occur in the array - this would be the number of '*' to print out
I think you should first let user enter the values, then do the output.
The code you show looks wrong, e.g. you increment 'i' twice in the while loop. You use the a[i] after the increment so it will not have the read value.
Also its good to use fgets/atoi instead when reading numbers that way invalid numbers will not cause your program to potentially crash.
精彩评论