0

Please, help me I'm a novice programmer.
I trying to make an array with random numbers and print the maximum, minimum, and average values.

To generate a random number, use the Math.random() method, which returns a value in the range [0, 1].

public class Main {
    public static void main(String[] args) {
        int a = 99;
        double[] array = new double[a];

        int b;
        for(b = 0; b < array.length; ++b) {
        }

        array[b] = Math.random();
        double max = array[0];
        double min = array[0];
        double abg = 0.0D;

        for(b = 0; b < array.length; ++b) {
        }

        if (max < array[b]) {
            max = array[b];
        }

        if (min > array[b]) {
            min = array[b];
        }

        abg += array[b] / (double)array.length;

        System.out.println("max = " + max);

        System.out.println("min = " + min);

        System.out.println("abg = " + abg);
    }
}
GURU Shreyansh
  • 881
  • 1
  • 7
  • 19
Diana
  • 3
  • 1
  • 2
    it means that your array has less than 100 elements and you're trying to get the 100th element – Stultuske Jul 02 '21 at 11:55
  • 1
    Does this answer your question? [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – 17xande Jul 02 '21 at 12:59
  • Basically your code is a good point of start. Pay attention for your loops: the are empty. Place the code below the loops ''inside'' the loop braces (except the System.* commands), and it will work. The exception I have explained within my answer. – ChristophS Jul 02 '21 at 13:26

3 Answers3

0

Your both array iteration will be like this:

for(b = 0; b < array.length; ++b) {
    array[b] = Math.random();
}

and ...

for(b = 0; b < array.length; ++b) {
    if (max < array[b]) {
        max = array[b];
    }
    if (min > array[b]) {
        min = array[b];
    }
    abg += array[b];
}
abg /= array.length;

Your code will be between the curly braces of the loop.

Mukit09
  • 2,956
  • 3
  • 24
  • 44
  • The primary intention was to help him to get rid of the exception. But thanks for pointing that out. @GURUShreyansh – Mukit09 Jul 02 '21 at 12:07
0

You're using braces incorrectly, try as follows...

public static void main(String[] args) {
    int a = 99;
    double[] array = new double[a];

    int b;
    double max , min, abg = 0;
    for(b = 0; b < array.length; ++b) {
        array[b] = Math.random();
        if(b == 0) {
            max = array[0];
            min = array[0];
        }
        abg += array[b];
    }

    for(b = 0; b < array.length; ++b) {
        if (max < array[b]) 
            max = array[b];

        if (min > array[b]) 
            min = array[b];
    }
        
    abg /= (double)array.length;
    System.out.println("max = " + max);
    System.out.println("min = " + min);
    System.out.println("abg = " + abg);
}
Akshay Saambram
  • 155
  • 1
  • 2
  • 12
0

To understand your Exception, take a look at the line

array[b] = Math.random();

This causes your exception, because b has the value of 99, but your array has the size of 99, so highest valid index is 98, thus an arrayIndexOutOfBounceException is thrown.

b gets the value of 99 from the loop before:

for(b = 0; b < array.length; ++b) {
}

On last iteration b becomes 99 and the loop is interrupted, because b < array.length becomes false.

To offer a solution, I tried to modify your example as less as possible, but as far as required:

public static void main(String[] args) {

    // variables
    final int array_size = 99;
    int loop;
    double max, min, abg;
    double[] array = new double[array_size];

    // fill array with random numbers
    for (loop = 0; loop < array_size; loop++) {
        array[loop] = Math.random();
    }

    // initialize start values
    max = 0.0; // lowest possible value
    min = 1.0; // highes possible value
    abg = 0.0; // average

    // loop through array
    for (loop = 0; loop < array_size; loop++) {
        // current value lower than found minimum?
        if (array[loop] < min) {
            min = array[loop];
        }
        // current value greater than found maximum?
        if (array[loop] > max) {
            max = array[loop];
        }
        // add all values for average
        abg += array[loop];
    }
    System.out.println("max = " + max);
    System.out.println("min = " + min);
    System.out.println("abg = " + (abg/(double)array_size));
}

And notably you are a beginner let me give the hint: write comments about functionality as much as you can. Good code contains nearly the same amount of comment lines a functional code itself. It will help yourself to understand, what you have done.

ChristophS
  • 598
  • 4
  • 23