# Thread: printing axis for histogram

1. ## printing axis for histogram

Hey guys im trying to print this
Code:
```          0     5    10    15   20   25   30   35
+----+----+----+----+----+----+----+-
|
|
|
|
5     |
|
|

10

15

20

25

and so on with 5 hiphens | in between each number i cant get the formatting correctly?

30

35```
could someone take a look

thankyou
Code:
```/* print the numbers*/
for(pos=0; pos<8; pos++)
{

n = pos * 5;
printf("  %-3d", n);

}

/* after a new line space print
each +----+ once at a time in the loop*/
/*
for(b=0; b<VER_ARRAY; b++)
{

}*/
printf("\n");
printf("  +----+----+----+----+----+----+----+-");
printf("\n");
/*for the amount entered keep printing numbers
on vertical axis limited to user input*/

/*keep printing hiphens for each interval*/
for(a =0; a<5; a++)
{
printf("  |");

printf("\n");

}

for(d=1; d<4; d++)
{
n = 0;
n = d * 5;

printf("%-5d\n", n);

/*
for(e =0; e<5; e++)
{
printf("  |");

printf("\n");
}*/

}

/* for the size of num being entered if
0 integers have been entered make it valid*/

for(c=0; c<sizeof(num[c]); c++);
{
if(num[c] %5 ==0)
{
printf("\n");
}

}
printf("\n");```

2. A hyphen is a dash. A pipe is the vertical bar. Try editing your post and wrapping code tags around your drawing to say what you're really trying to do.

Quzah.

3. You need TWO, and only two loops for a histogram. Here's some logic
Code:
```for (i = 0; i < number of rows of data to be in the histogram; i++)  {

for (j = low range; j <= high range; j++) {
if ((j > 0) && (j % 5 == 0)) {
print a pipe
continue;
}

if (array[i] > j)
print an asterisk
else
print a blank space

}
}```
J is our value in the histogram, as it's drawn, from left to right, low range to high range.

Note that if a value would print a * right on the fifth column's pipe, that value will not be printed in the above logic. The pipe will take precedence over the asterisk.

4. Rather than trying to figure out the exact printing order, how about

char screen[25][80];
Initialise the whole array to spaces.

Then you can write to any "position" on the screen by writing directly to the corresponding position in the array.

Once you've finished, then its a simple matter of working your way through the array printing each character.

5. Sorry i meant pipe | will that work for every 5 pipes theres a number from 5 10 15 20? however the numbers have to be before the pipes

6. You can do like Salem suggested, or you fill it in as you go.
Code:
```for each row
for each column
find what goes here and print it```
In short, you do a tiny bit of math, find out what goes in this spot, and print it as you move across the columns. Then you go down a row and do the same.

Quzah.

7. Originally Posted by bazzano
Sorry i meant pipe | will that work for every 5 pipes theres a number from 5 10 15 20? however the numbers have to be before the pipes
No it won't work for the header portion. That was intended for the body of the histogram. It should print a pipe in the body of the histogram, when they're needed. Otherwise it prints either a blank space, or an asterisk. When you see how it works, you should have no trouble getting the header to print right.

It would be best if you moved away from some of those mad loops you have in your code. They are enough to make Rube Goldberg, jealous.