Results 1 to 7 of 7
  1. #1
    rpark712 is offline Member
    Join Date
    Sep 2011
    Posts
    14
    Rep Power
    0

    Default Need help with random arrays

    Hello I've been trying to make a file for random arrays for my class.
    The problem I've been having is getting the mean on the random arrays of numbers.
    This is the code for my work.



    import java.util.*;


    public class TextLab06
    {

    public static void main(String args[])
    {
    System.out.println();
    System.out.print("Enter the quantity of random numbers ===>> ");
    int listSize = Expo.enterInt();
    System.out.println();
    Statistics intList = new Statistics(listSize);
    intList.randomize();
    intList.computeMean();
    intList.computeMedian();
    intList.computeMode();
    intList.displayStats();
    System.out.println();
    }
    }


    class Statistics
    {

    private int list[];
    private int size;
    private double mean; // used for the 80, 100 and 110 point versions
    private double median; // used for the 100 and 110 point versions
    private int mode; // used for the 110 point version only

    public Statistics(int s)
    {
    size = s;
    list = new int[size];
    }

    public void randomize()
    {
    // This provided method creates the same exact list of "random" numbers for every execution.
    // You will learn more about this in Chapter 14. For now just use the provided method.
    Random rand = new Random(555); //random generator
    for (int k = 0; k < size; k++) //conditional statement
    list[k] = rand.nextInt(11) + 10; //range of 10..20
    Arrays.sort(list); //puts the random array list in order

    }

    public void computeMean()
    {
    System.out.println("Compute Mean");
    /* Random rand = new Random(555); //random generator
    for (int k = 0; k < size; k++)

    int number = (rand.nextInt(11) + 10);
    //int things = number * size;
    System.out.println(k *" ");
    */
    //ignore the experiment above
    }

    public void computeMedian()
    {
    System.out.println("Compute Median");
    }

    public void computeMode()
    {
    System.out.println("Compute Mode");
    }

    public void displayStats()
    {

    System.out.println(Arrays.toString(list));
    System.out.println();
    System.out.println("Mean: " + mean);
    System.out.println("Median: " + median);
    System.out.println("Mode: " + mode);
    System.out.println();
    }

    }




    Can anyone give me some helpful tips to solve this thing?

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default Re: Need help with random arrays

    1. Create an int variable say called mean, before for loop in mean method, initialise to 0
    2. inside mean method for loop add each number in the array to the variable you created
    3. outside for loop divide the variable you created by the number of elements in the array

  3. #3
    rpark712 is offline Member
    Join Date
    Sep 2011
    Posts
    14
    Rep Power
    0

    Default Re: Need help with random arrays

    Quote Originally Posted by al_Marshy_1981 View Post
    2. inside mean method for loop add each number in the array to the variable you created
    3. outside for loop divide the variable you created by the number of elements in the array
    What do you mean by adding each number in the array to the variable I created?

  4. #4
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default Re: Need help with random arrays

    For future reference, please use [code][/code] tags around your code to preserve formatting.

    To your last question, al_Marshy_1981 suggested you added each number of the array to the variable.

    He is suggesting you create a new variable like double myMeanVar in which you loop through the entire array and during each loop you read the array slot of your increment counter.

    Java Code:
    for (looping through my random array(myRandomArray) for each slot it has and incrementing the counter (i) every time)
    {
      myDoubleVar = myDoubleVar + myRandomArray[i];
    }
    Calculate the mean using the new total of myRandomArray which is stored in myDoubleVar and the length of myRandomArray;
    Print results;
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  5. #5
    rpark712 is offline Member
    Join Date
    Sep 2011
    Posts
    14
    Rep Power
    0

    Default Re: Need help with random arrays

    Would this work?

    Java Code:
    	{	
    		int number = 0;
    		int numElements = 0;
    		double sum = 0; 
    		
    		numElements = size;
    	
    		for ( int i = 0; i < size; i++ )
    		sum = sum + list[i];
    	
    		double mean = sum / size;
    		
    		System.out.println(mean);
    	}
    Last edited by rpark712; 12-07-2011 at 04:47 PM.

  6. #6
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default Re: Need help with random arrays

    What does the output tell you?

  7. #7
    rpark712 is offline Member
    Join Date
    Sep 2011
    Posts
    14
    Rep Power
    0

    Default Re: Need help with random arrays

    Yes thank you. It worked. Thanks for the help!

Similar Threads

  1. How to use arrays with Math.random
    By LennyKosmos in forum New To Java
    Replies: 2
    Last Post: 09-20-2010, 05:44 PM
  2. using random strings from arrays insult generator
    By carden2 in forum New To Java
    Replies: 5
    Last Post: 04-05-2010, 06:51 AM
  3. LUDecomposition, random arrays.
    By rootpi in forum New To Java
    Replies: 5
    Last Post: 12-23-2009, 04:32 PM
  4. Random numbers and arrays
    By caro in forum New To Java
    Replies: 6
    Last Post: 06-10-2009, 02:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •