how do i display a histogram that is horizontal to vertical without using arrays.

from:

0-29 ****
30-39 **
40-69 *

to: where the stars * in the catergory (0-29,30-39,40-69...) goes downwards not across the screen:

Recommended Answers

All 19 Replies

You're printing lines, one at a time, starting from the top.
What goes in the first row?

so far i have got this code:

    System.out.print("0-29    ");
    for (star = 0; star < low; star = star + 1) {
        System.out.print("*");

    }
    System.out.print("\n");
    System.out.print("30-39   ");
    for (star = 0; star < midlow; star = star + 1) {
        System.out.print("*");

    }
    System.out.print("\n");
    System.out.print("40-69   ");
    for (star = 0; star <midhigh; star = star + 1) {
        System.out.print("*");

    }
    System.out.print("\n");
    System.out.print("70-100  ");
    for (star = 0; star < high; star = star + 1) {
        System.out.print("*");

    }//end for loop

that outputs

0-29    ****
30-39   **
40-69   ******
70-100  ********

when i enter: 10 20 30 40 50 60 70 80 90 100 10 20 30 40 50 60 70 80 90 100

but i wanted to change the output, where the catergories 0-29,30-39 are all in a row, with the stars going downwards from them.

You want the stars to descend from the headers? Okay, that's novel at least. Usually they go up. :)

In any case, it's the same problem: you're printing a series of rows, which are made up of stars and spaces, separated by more spaces. How do you decide between printing a star and a space?

yep that's what i want
and ermmm don't know :/

Okay.

First thing, make a header. Show the user what you're going to report, when you figure out how to do it. To make your formatting a bit easier, use tabs rather than spaces to separate the columns: tab is "\t"

Then, make a row that prints just a single "*" under each of those.

When you've got that, maybe we can figure out how to make decisions.

okay i'll get down to doing that.
do i need to make a new code just to create a vertical histogram or can i use the code above and make changes to it?

Doesn't matter much to me. You've got some stuff you might want to reuse there, so maybe you want to add this on to the end of what you've already got, or make a copy of what you've got and remove the old display stuff. Do what you like.

Pet peeve time: "code" in the sense of programming is a mass noun, not a count noun. You don't write "a code", any more than you put "a butter" on your bread. You write a class file, or you write a program, or you write a shell script, or maybe you do all of the above and spend all day writing code.

so far, this is my header
System.out.print("0-29 ");
System.out.print("\t");
System.out.print("30-39 ");
System.out.print("\t");
System.out.print("40-69 ");
System.out.print("\t");
System.out.print("70-100 ");

but, how do i put the * under it?

Looks like that'll work for the header. For the next row, make a new line and then make a row of stars, one under each column head. You'll do this about the same way you did the header.
We're doing this one step at a time. After you get the stars in the right places, you'll figure out how to decide whether a star goes there or not.

i've done that, how would i now put the stars where they are suppose to be?

So you've got headers, and a row of stars underneath them. Now, instead of just printing a star in that row, you have to decide whether a star belongs there. That's going to be based on the value of the variable you're plotting in each column. For the first row, you're putting a star there if the column has one or more stars, otherwise you're putting a space.
Try doing that.

so far, my output displays:

0-29 30-39 40-69 70-100
*
*
*
*

*
*


*
*
*
*
*
*


*
*
*
*
*
*
*
*

but, how do i get the stars do be under the catergories?

Look at the code that you used to make the column headers happen - can you modify that to make a row of stars happen?

erm...this is the script that i am using.

    System.out.print("0-29");
    System.out.print("\t");
    System.out.print("30-39");
    System.out.print("\t");
    System.out.print("40-69");
    System.out.print("\t");
    System.out.print("70-100");

    {
        System.out.print("\n");

        for (star = 0; star < low; star = star + 1)
            System.out.println(" *");
    }
        System.out.print("\n");
        for (star = 0; star < midlow; star = star + 1) {                
            System.out.println("            *");

    }
        System.out.println("\n");
        for (star = 0; star < midhigh; star = star + 1) {
            System.out.println("                         *");

    }
        System.out.println("\n");
        for (star = 0; star < high; star = star + 1) {
            System.out.println("                                          *");
        }

But, because i am entering 0-29 first the stars come under that header; and the rest of the stars comes under the 0-29 header, so i put spaces on the stars to move the stars towards the right headers; but how do i get it underneath the headers?

Doing it that way, you can't. You have to produce rows. Each row has one piece of data from column 1, one from column 2, one from column 3, and one from column 4 - so you can't just run through one column's data at a time.

hmmmm...so, how would i do it?

One row at a time, like I said. Now, for the first row, you have to start by getting a row of stars, one in each column. Like I said. You're going to do that just about the same way you put the column headers in a column. Like I said.

Hi jon.
i have tried to play around with that problem but i sort of get similar things can you have a look?

for(star = 0;star <low;star++)
        {
            System.out.print("*" +"    \t");
            System.out.println();
            
        }//end low loop, made by         
        for(star = 0;star <midlow;star++)
        {
            System.out.print("    \t" +"*" );
            System.out.println();
        }//end midlow loop made by 
        for(star = 0;star <midhigh;star++)
        {
            System.out.print("    \t" +"    \t" +"*" );
            System.out.println();
        }//end midhigh loop        
        for(star = 0;star <high;star++)
        {
            System.out.print("    \t" + "    \t" + "    \t" +"*" );
            System.out.println();

ps. i know there print lines are wrong, but i have used this method to see if i get correct no. of stars for each category...
by doing this i have noticed that i Do get correct number of stars under a specific category, but there is a problem with it because it doesnt print out in the formar as i want, and so the maker of this thread. I dont really know how to put this print statements together so it will print correct number of stars in each line
i thought that by writing this

System.out.print("*" + "\t");
      System.out.println():

it will print a specific number of stars in a vertical row, but it will also allow the other row to be displayed next to that one... you know what i'm on about ?

You're having the same problem as the Beatles fan (Help!). You've got sequential output that you can add to from left to right, and then from top to bottom. You're trying to go from top to bottom, and then from left to right, and it's not going to happen.

Try looking at it this way. If your columns are A, B, C, and D, the order in which your entries are printed is like this:

A B C D
 -------
 1 2 3 4 
 5 6 7 8 
 ...

You have to decide those positions in that order. So the first thing is to just put stuff in those positions, so you can have something to work with. Your first attempt should be to get this:

A B C D
 -------
 * * * * 
 * * * * 
 * * * * 
 * * * * 
 * * * *

Once you've done that, you can move on to deciding for each of those instances of "*", whether there should be a "*" or a " ". The trick is, you have to do it in the above order. So for position 1, how do you decide whether a "*" goes there? Then for position 2, again, how do you decide whether that's a "*" or a " "?
There is a logical way to group these decisions. You'll use a loop, but it's not a loop on each column's data. It's a loop across the row.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.