0

I am practicing Java arrays. I managed to create code to successfully extract array of random ints, sum, max element, difference, shift array to left; however, I cannot extract min element, which always comes up as zero(0) even if the array doesn't contain 0. I researched this issue, and it seems min value is always 0 because the array variables are initialized to 0 due to 0 being default value...however, I cannot find any solution to this issue.

I found several posts from users dealing with this issue as well, however, there doesn't seem to be any solution as far as I can tell...Posts seem to explain issue, but not solve it...I tried playing with the code, such as creating separate for loops for max and min, as well as set the below nested if condition for min loop, but nothing works. if (numArray[i] == 0) {continue;}

package june22;
import java.util.Arrays; 
import java.util.Scanner;

public class MoreArrayPractice {

    public static void main(String[] args) {            

        Scanner sc = new Scanner(System.in);
        System.out.println("Enter number of random values you want in your array: ");
        int num = sc.nextInt();
        int[] numArray = new int[num];      
        int total = 0;
        int diff = 0;
        int max = numArray[0];
        int min = numArray[0];
        int i, temp;
        for (i = 0; i < numArray.length; i++)
        {
            numArray[i] = (int) (Math.random()*num)+1;
            total = total + numArray[i];
            System.out.print(numArray[i] + " ");
            if (numArray[i] > max) {max = numArray[i];}                 
            if (numArray[i] < min) {
                if (numArray[i] == 0) {continue;}
                min = numArray[i];} 
        }       

        temp = numArray[0];
            for (i = 1; i < numArray.length; i++) {
            numArray[i - 1] = numArray[i];
        }   numArray[numArray.length-1] = temp;             

        System.out.println();
        System.out.println("Sum is " + total); 
        System.out.println("Max is " + max); 
        System.out.println("Min is " + min); 
        System.out.println("Array shuffled left is ");
        for (i = 0; i < numArray.length; i++) {System.out.print(numArray[i] + " ");}
        System.out.println();
        for (int a: numArray) {
            diff = diff - a; }
            System.out.println("Difference of numbers is " + diff);  
        //correct syntax for printing Array         
    }    
}
2
  • 1
    The issue is with your initialisation. Commented Jul 22, 2019 at 23:21
  • Don't try and do all of these operations in one function. Define separate functions called min, max, sum etc. and just call them passing in the array. This will make your code far easier to read. Commented Jul 22, 2019 at 23:28

2 Answers 2

4

Your problem is that when you run the line

int min = numArray[0];

your random numbers haven't been put into your array, so you're setting min to zero up front.

I would suggest you break your main loop into two separate loops - one to get the random numbers, and another to set max and min. So you'll have, in this order,

  • the loop that populates the array with random numbers,
  • the declaration of max and min, and setting their initial values to the first thing in the array,
  • the loop to find the right values for max and min.
Sign up to request clarification or add additional context in comments.

4 Comments

Question...In my original code, how come setting min to zero upfront was a problem for the min, but not a problem for the max (as max number generated just fine)?
@AnneBailly It has to do with the direction of number values. When looking for max, you're always checking for a higher value with 0 being lower than any of your other values you're checking. For min, you want to go the opposite direction on the number spectrum and start with a value higher than any of your values you're checking. You want to start with a high number and work your way towards the low end of the number spectrum.
@AnneBailly To clarify further on that point, instead of using 0 as a base for either max or min checking it's better to use the lowest or the highest value possible for a data type as the starting point depending which way you're going along the spectrum. So if you're using an Integer, for finding the max you would set your initial comparison value to the minimum possible value for an Integer. You would do the opposite for min and start at the maximum possible value for an integer.
@AnneBailly, in your original code, you were setting min and max to numArray[0], which is the correct thing to do. So Tim's comments don't really apply to what you were doing, so long as you set them after you populate the array. If you don't do that, that's when they come out 0.
0

The issue is with your initialisation:

 int max = numArray[0];
 int min = numArray[0];

As this point all elements in the array are zero. So that's (0) going to be your min, unless you have a negative element in your array. To solve this, you ought to set min to the maximum integer value:

int max = 0;
int min = Integer.MAX_VALUE;

This way, you can still have just one loop. It also simplifies your min-checking code to:

if (numArray[i] < min) {min = numArray[i];}

Of course, I assume you are doing all these for practice, like you noted. There are more efficient ways to do what you are doing.

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.