1

I need to store the min and max values in an array given, then print them out with specific characters (+ for the maximum values, "-" for the minimum value, and "*" for all the rest).

I think I have most of it completed except for the storing values appropriately so that all the values are not "++++++++++...." like they currently are when printed out.

Any ideas? Help is greatly appreciated.

      public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        int numbers[] = new int[24];
        int min = Integer.MAX_VALUE;
        int max=Integer.MIN_VALUE;
        int maxhour = 0; 
        int minhour = 0;
        int total = 0;
        char MAX = '+', MIN = '-', MIDDLE = '*';
        char currentchar = 0;
        for(int i=0; i< numbers.length; i++){
            numbers[i] = keyboard.nextInt();
            total = total + numbers[i];
           if(numbers[i]<min){
               min = numbers[i];
               minhour = i;
               currentchar = MIN;
           }else if (numbers[i]>max){
               max = numbers[i];
               maxhour = i;
               currentchar = MAX;
           }
        }
        for(int i=0; i< numbers.length; i++){
            System.out.print("Hour " + i + ":");
            printTimes(currentchar, numbers[i]);
            System.out.println("");
          }

        System.out.println("Largest Number of hits is : " + max + " at hour " + maxhour);
        System.out.println("Average Number of hits is : " + (total/24) + " per hour");
        System.out.println("Smallest Number of hits is : " + min + " at hour " + minhour);



    }

        public static void printTimes(char c, int times) {
        if (times >= 70) {
            for(int i=0; i< 69; i++){
            System.out.print(c);
            } System.out.print(">");
        } else if (times < 70) {
            for(int i=0; i< times; i++)
            System.out.print(c);
        }
    }


}

Example of current output:

42 29 36 7 5 3 10 13 33 40 51 49 22 58 63 102 65 58 48 24 36 48 52 42

Hour 0:++++++++++++++++++++++++++++++++++++++++++

Hour 1:+++++++++++++++++++++++++++++

Hour 2:++++++++++++++++++++++++++++++++++++

Hour 3:+++++++

Hour 4:+++++

Hour 5:+++

Hour 6:++++++++++

Hour 7:+++++++++++++

Hour 8:+++++++++++++++++++++++++++++++++

Hour 9:++++++++++++++++++++++++++++++++++++++++

....

Largest Number of hits is : 102 at hour 15

Average Number of hits is : 39 per hour

Smallest Number of hits is : 3 at hour 5

2 Answers 2

1

Just change your last for:

for (int i = 0; i < numbers.length; i++) {
    System.out.print("Hour " + i + ":");
    if (numbers[i] == min)
        currentchar = MIN;
    else if (numbers[i] == max)
        currentchar = MAX;
    else
        currentchar = MIDDLE;
    printTimes(currentchar, numbers[i]);
    System.out.println("");
}
Sign up to request clarification or add additional context in comments.

3 Comments

Awesome! That did the trick! Can you explain what that did to differ from my original code?
Just check each element of the array numbers with the min, max values you just computed. If it is not a min or max (else clause) it will set currentchar to MIDDLE.
@user2994377 In additon to Christian's answer, there is another more subtle bug in your code. In your first loop, when you are computing min/max, you should initialize both min and max to the first value of the array. As it stands, if the first value you enter is also the maximum, the results will be incorrect: It will be less than Integer.MAX_VALUE, so it will be temporarily set as the first value of min, but will never be set as the value of max (work through the logic in your head, you'll see). Alternate fix is to get rid of the "else" between those two ifs.
0

I would update your code as follows -

public static void main(String[] args) {
  int numbers[] = new int[24];
  int total = 0;

  System.out.println("Enter 24 integers please");
  Scanner keyboard = null;
  try {
    keyboard = new Scanner(System.in);
    for (int i = 0; i < numbers.length; i++) {
      numbers[i] = keyboard.nextInt();
      total += numbers[i];
    }
  } finally {
    keyboard.close();
  }
  Integer min = null;
  Integer max = null;
  int maxhour = 0;
  int minhour = 0;
  char MAX = '+', MIN = '-', MIDDLE = '*';
  for (int i = 0; i < numbers.length; i++) {
    if (min == null || numbers[i] < min) {
      min = numbers[i];
      minhour = i;
    } else if (max == null || numbers[i] > max) {
      max = numbers[i];
      maxhour = i;
    }
  }
  for (int i = 0; i < numbers.length; i++) {
    char currentchar = MIDDLE;
    if (i == minhour) {
      currentchar = MIN;
    } else if (i == maxhour) {
      currentchar = MAX;
    }
    System.out.print("Hour " + i + ":");
    printTimes(currentchar, numbers[i]);
    System.out.println("");
  }

  System.out.println("Largest Number of hits is : "
    + max + " at hour " + maxhour);
  System.out.println("Average Number of hits is : "
    + (total / 24) + " per hour");
  System.out.println("Smallest Number of hits is : "
    + min + " at hour " + minhour);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.