1

I am trying to get the minimum value from an array that I have set up but it keeps returning a value of 0?!?!

import java.util.Scanner;
public class ACTScoring {

    /**
     * Stack Underflow
     * 11/17/15
     * ACT Scores are pretty neat
     */
    public static void main(String[] args) {
        int[] score = new int[12];
        Scanner inputTest = new Scanner(System.in);
        int totalScores = 0;
        double average = 0.0;
        int high = score[0];
        int low = score[0];

         for (int count = 0; count < score.length; count++){
             System.out.println("Please Enter in the score: ");
             score[count] = inputTest.nextInt();

         }
         for (int count = 0; count < score.length; count++){
             totalScores = totalScores + score[count];

             if(score[count]>high)
                 high=score[count];
             //low keeps outputting 0
             else if (score[count]<low)
                 low=score[count];

             average = (totalScores*1.0) / score.length;
         }


         System.out.println("Your average score is: " + average);
         System.out.println("Your Highest Score was: " +high);
         System.out.println("Your lowest Score was: "+ low);
    }

}
3
  • 1
    Set your initial min value to a high number, maybe like Interger.MAX_VALUE. Also, calculate your average only after you've calculated the total Commented Nov 17, 2015 at 21:52
  • Have you tried running your code under debugger? Commented Nov 17, 2015 at 21:53
  • To expand on @MadProgrammer's comment, "int low = score[0]" is setting low to 0 before you even begin your loop. An empty array of ints will be initialized to zeros in Java. Commented Nov 17, 2015 at 21:54

4 Answers 4

1

It's a good idea to initialize your values to the opposite of what they represent before going into the loops.

int low = Integer.MAX_VALUE;
int high = Integer.MIN_VALUE;

But a better solution would be to do everything in the first loop:

int length = 12;
for (int count = 0; count < length; count++){
     System.out.println("Please Enter in the score: ");
     int value = inputTest.nextInt();
     if (value < low) {
         low = value;
     }
     if (value > high) {
         high = value;
     }
     totalScores += value;
}
average = (totalScores * 1.0) / length;
Sign up to request clarification or add additional context in comments.

Comments

0

you are initializing your min and max before you have any data in the array, so both get 0 (int's default).

this means that if all input are positive numbers, min will still be zero.

you need to move your initialization of min and max to between the two for loops

Comments

0
int high = score[0];
int low = score[0];

Here you set those variables to what your freshly initialized array contains at index 0. Which is 0. So as long as you do not enter negative numbers, 0 will always be your result.

If you move those two lines below your first for-loop (where the array actually gets filled), your code should work.

Comments

0

One of the easiest ways of getting the lowest value in your array, is by sorting the array into ascending order first, then get the first value.

public static void main(String[] args){
    int[6] results;
    //initialize results
    Arrays.sort(results);
    System.out.println("Lowest: "+results[0]+". Highest: "+results[results.length-1]);
}

Look here for a good example

1 Comment

if all he needs is min and max - sorting the array is too expensive and redundant. sorting an array is o(NlogN) complexity, his solution is o(N)

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.