Burhan
Burhan

Reputation: 43

For loop troubling for printing multiple values

So the question is: Write a program that reads a sequence of input values and displays a bar chart of the values using asterisks. You may assume that all values are positive. First figure out the maximum value. That value’s bar should be drawn with 40 asterisks. Shorter bars should use proportionally fewer asterisks. eg.

***********************
*********
****************************************
****
*******************

This is my code below:

    int count = 0;
    //Finds largest Value
    int largestValue = numbersArray[0];
    for (int i = 1; i < numbersArray.length; i++) {
        if (numbersArray[i] > largestValue) {
            largestValue = numbersArray[i];
            count++;
        }
    }
    //Prints number of asterisks
    final int MAX = 40;
    String asterisks = "****************************************";
    for (int i = 0; i < count + 2; i++) {
        System.out.print(numbersArray[i]);
        if (numbersArray[i] == largestValue) {
            System.out.print(asterisks);
        } //if (numbersArray[i] != largestValue) {
        else {
            for (int j = 0; j < (40 * numbersArray[i] / largestValue); j++) {
                System.out.print("*");
            }
        }
        System.out.println();
  }

This code doesn't seem to run properly. If I enter values in the order: 5 8 6 4 7, it will just print the stars for 5 and 8, and not the rest. Basically it prints stars for values till the largest number.

I can't find what's wrong with my code. Any help would be greatly appreciated!

Thanks for reading <3

Upvotes: 2

Views: 1566

Answers (1)

Ori Lentz
Ori Lentz

Reputation: 3688

First of all, you don't need to count variable - it does nothing helpful to you and you for some reason limit yourself (you increment everytime you find a larger element so you only increment once since there's nothing larger than 8).

What you should be doing is finding the largest value, as you did, and then running over the entire array and displaying each element proportional to that value, as you did (but without the special case for the actual largest value - that is atrocious).

Also, you should note that division of two integers would result in an integer, which is not what you want, so you'll have to cast one of them to float or double.

//finds largest Value
int largestValue = numbersArray[0];
for (int i = 1; i < numbersArray.length; i++) {
    if (numbersArray[i] > largestValue) {
        largestValue = numbersArray[i];
    }
}

//Prints number of asterisks
final int MAX = 40;
for (int i = 0; i < numbersArray.length; i++) {
    int portion = (int)(MAX * (numbersArray[i] / (float)largestValue));
    for (int j = 0; j < portion; j++) {
        System.out.print("*");
    }
    System.out.println();
}

So you'll find the largest value is 8.

Then for 5, you'll do 5/8 which is 0.625 and times MAX (40) that would be 25, so you'll print 25 *.

Then for 8 - 8/8 = 1.0 * MAX = 40 so you'll print the whole 40 *.

For 6 - 6/8 = 0.75 * MAX = 30 so you'll print 30 * and so on.

Note that if you want to fine-tune it, you could use Math.round instead of simply casting the portion to int (which simply truncates the floating point).

Upvotes: 6

Related Questions