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); } }
Random Array, max and min
Page 1 of 14 Replies  6038 Views  Last Post: 24 February 2010  06:23 PM
#1
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.
Replies To: Random Array, max and min
#2
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
Re: Random Array, max and min
Posted 24 February 2010  02:42 PM
Well you can use
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:
Math.random()*RA.lengthto 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
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
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
have not be written to expect an array as parameter
Page 1 of 1
