Write a Java program that generates and displays 100 random integers in the range of [10, 25] inclusively. The program then displays the number of occurrences for each number in that range. Next, the program displays the maximum, minimum and average (with exactly 2 digits after the decimal) value of the occurrences

Recommended Answers

All 8 Replies

DaniWeb Member Rules (which you agreed to when you signed up) include:
"Do provide evidence of having done some work yourself if posting questions from school or work assignments"
http://www.daniweb.com/community/rules

Post what you have done so far and someone will help you from there.

here is what i've already done:

public void displayOccurences(int min,int max){

        Random numRand = new Random(); 
        int[]Array= new int[100];
        for (int i=0;i<100;i++){
            Array[i]= numRand.nextInt((max-min)+1);
            System.out.println(Array[i]+"");}
 the other way i tried:

          Random r = new Random();
        int array[] = new int[100];
        int a = 0;
        while (a < 100){
            int random = r.nextInt(25);
            array[a] = random;
            a++;
        }

        int i = 0;
            for(int row = 0; row<= 10; row++)

            {
                for(int c = 0; c<=10; c++)
            {
                System.out.printf("%03d ", array[i]);
                i++;
                }
            System.out.println("");}

i just want it to displays like this
Display 100 random integers in [10, 25]
14 14 18 24 12 25 16 12 18 16
15 16 16 15 17 19 10 21 14 25
21 24 10 19 25 17 19 13 25 24
23 25 15 25 18 23 18 14 24 11
21 12 20 24 14 18 11 12 14 13
14 20 11 16 21 15 15 17 20 24
10 18 20 18 24 23 17 18 15 11
20 19 14 23 20 23 18 22 20 11
10 24 22 12 10 11 11 12 12 13
14 12 15 15 13 24 23 23 11 23
Display occurrences
10: 5
11: 8
12: 8
13: 4
14: 9
15: 8
16: 5
17: 4
18: 9
19: 4
20: 7
21: 4
22: 2
23: 8
24: 9
25: 6
Max occurrences: 9
Min occurrences: 2
Average occurrences: 6.25

OK, that's a good start. Now for counting the occurrences...
You know the numbers are in the range 10-25, which is a nicely small range - easily small enough to use as the indexes into an array. You could set up an array for the counts, one element for each possible value, then each time you process a value (n) you can increment the corresponding array element counts[n]

so which of those code is correct??? but it couldn't display like above :
it displays like this
run:
14
12
8
9
14
3
14
12
5
5
9
8
5
3
1
4
2
1
4
4
6
11
14
9....
it makes me confused==

The "other way" (lines 10-25) looks pretty close. The range for the random numbers isn't quite right yet, but you can fix that later.

i couldn't make it in range..it always like:
10 11 19 5 8 9 0 15 24 3
16 23 19 22 9 1 5 7 24 7
6 16 13 9 3 12 16 5 21 14
9 20 19 9 3 20 10 24 23 1
17 13 12 7 5 12 21 4 19 15
15 8 11 17 14 11 8 10 0 12
10 12 5 13 16 10 3 1 4 15
10 23 17 19 10 18 4 24 0 3
18 6 22 16 9 14 3 7 7 4
6 23 7 6 24 21 3 9 8 6

if i is the value you print:

if ( i < 10 )
System.out.print(" " + i);
else
System.out.prit("" + i);

Yes, you can do it that way, but what's wrong with the shorter and much more generic/extensible
System.out.printf("%3d", array[i]);
like that he was already using?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.