Question

My program filled an integer array with numbers, I've called it count array:

int[] array count = {5, 4, 3, 0};

Now i want to print my array grapically horizontally using for loop(s). I want to print all my values in asteriks, an example would be:

   *     
   *        *
   *        *        *
   *        *        *
   *        *        *
count(0) count(1) count(2) count(3)

First I want to find out what my max value of my array is. If this max value = matched then i print as asteriks otherwise an space. However I can't just loop, and do maxint - 1, and check if there as an match because then I only would get a asteriks for the max value of each array, and rest spaces.

So I tried to make a work around and start with each minimum value and work upwards.

for (int k = 0; k <= 6; k++) {
    System.out.println("NEXTLINE"); 
    maxInt = (maxInt - maxInt) + k;  
    for (int i = 0; i < controleArray.length; i++ ) { 
            int graphLetter = letterCount[i];
            for (int j = 0; j < 25; j++) { 
            //System.out.println("TESTinner ");  
                if (graphLetter > maxInt) {
                    System.out.print(" * ");
                    break;  
                } if (graphLetter <= maxInt) {
                    System.out.print(" ");
                    break; 
                }
            }

So far this code is counting each value of my array AND printing the asteriks. However, there are being printed upside down. So the output would look like this.

*   *   *   NEXTLINE
*   *   *   NEXTLINE
*   *   *   NEXTLINE
*   *       NEXTLINE
*           NEXTLINE

Why does this happen? And what is a better way to look at this solution?

Was it helpful?

Solution

 maxInt = (maxInt - maxInt) + k;

(maxInt - maxInt) is always zero, so something is wrong here.

You can use maxInt just as a trigger, using value>=maxInt instead ==, and then you could just loop, as you say.

Then you only would need two loops: the outer one iterating for each line and the inner one for each bar of the graph, something like:

//Given the max value of the graph maxInt
for (int i=maxInt;i>0;i--) {
    for (int j=0;j<count.length;j++) {
        if (count[j]>=i) {
            System.out.print(" * ");
        } else {
            System.out.print("   ");
        }
    }
    System.out.println("NEWLINE");
}

I think this way is more elegant (as it's simpler and easier to understand.

OTHER TIPS

for (int k = 6; k >= 0; k--) {
    System.out.println("NEXTLINE"); 
    maxInt = (maxInt - maxInt) + k;  
    for (int i = 0; i < controleArray.length; i++ ) { 
            int graphLetter = letterCount[i];
            for (int j = 0; j < 25; j++) { 
            //System.out.println("TESTinner ");  
                if (graphLetter > maxInt) {
                    System.out.print(" * ");
                    break;  
                } if (graphLetter <= maxInt) {
                    System.out.print(" ");
                    break; 
                }
            }

Try This.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top