4 Replies - 6608 Views - Last Post: 24 February 2010 - 06:23 PM Rate Topic: -----

#1 LaMorte85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • 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


Was This Post Helpful? 0
  • +
  • -

#3 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,599
  • 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

Was This Post Helpful? 0
  • +
  • -

#4 LaMorte85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

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

Reputation: 8346
  • View blog
  • Posts: 31,904
  • 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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1