# Random Array, max and min

Page 1 of 1

## 4 Replies - 9755 Views - Last Post: 24 February 2010 - 06:23 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=158106&amp;s=7c501f4757a15b65c13136dc7f9f8b4b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 LaMorte85

Reputation: 0
• Posts: 9
• Joined: 22-February 10

# Random Array, max and min

Posted 24 February 2010 - 02:33 PM

Stuck on this last part with Max and min, having trouble using the max and mins with the array.

```search the array to output the following items:

//1. Sum of the 15 random numbers.

//2. Average of the 15 random numbers.

//3. Maximum value of the 15 random numbers.

//4. Minimum value of the 15 random numbers.

import java.util.Random;
public class RA2 {

public static void main(String[] args){

Random num = new Random();

int RA[][] = new int[3][5];
int sum=0;

for (int i = 0; i < 3; i++) {
for (int j = 0; j < 5; j++){

RA[i][j] = 1+num.nextInt(100);
sum += RA[i][j];

}
}

for (int i = 0; i < 3; i++) {
for (int j = 0; j < 5; j++){
System.out.print(RA[i][j] + "  ");
}

System.out.println();
}
int average= sum/15;
System.out.print("The Sum of these numbers is ");
System.out.println(sum);
System.out.print("The Average of these numbers is");
System.out.println(average);
System.out.println(Math.max(RA);
System.out.println(Math.min(RA);
}
}
```

Is This A Good Question/Topic? 0

## Replies To: Random Array, max and min

### #2 erik.price

• D.I.C Lover

Reputation: 486
• Posts: 2,690
• Joined: 18-December 08

## Re: Random Array, max and min

Posted 24 February 2010 - 02:39 PM

```System.out.println(Math.max(RA);
System.out.println(Math.min(RA);
```

That isn't how the Math.max(), and Math.min() methods work.

To find the minimum value, try this:
```let min = array[0]
for(i in array)
if i < min
min = i

```

And for maximum, something similar:
```let max = array[0]
for(i in array)
if i > max
max = i

```

### #3 SwiftStriker00

• No idea why my code works

Reputation: 439
• Posts: 1,617
• Joined: 25-December 08

## Re: Random Array, max and min

Posted 24 February 2010 - 02:42 PM

Well you can use
``` Math.random()*RA.length
```
to get random number thats between 0 and ur array's length.

create a method that does that 15 times and returns the sum.
---------
However I think your prof wants you to create a random number 15 times, and store them into an int[]. Then use that array to find the four answers. Once again i would create four methods that you passed in the array, and they returned the value you were looking for to get you started heres what the sum would look like:
```public int sum( int[] array ){
int sum = 0;
for( int i = 0; i < array.length; i++ ){
sum += array[i];
}
return sum;
}

```

This post has been edited by SwiftStriker00: 24 February 2010 - 02:43 PM

### #4 LaMorte85

Reputation: 0
• Posts: 9
• Joined: 22-February 10

## Re: Random Array, max and min

Posted 24 February 2010 - 03:01 PM

Yes, I was hoping a Math.max(RA) would work or something close to that. Just to find the maximum number each time for the random array.

This post has been edited by LaMorte85: 24 February 2010 - 03:11 PM

### #5 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Random Array, max and min

Posted 24 February 2010 - 06:23 PM

Math.max() and Math.min() expect 2 arguments and return the max/min of the two
have not be written to expect an array as parameter

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }