Results 1 to 7 of 7
Thread: Need help with random arrays
- 12-05-2011, 03:45 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 14
- Rep Power
- 0
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?
- 12-05-2011, 03:55 PM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
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
- 12-06-2011, 03:12 PM #3
Member
- Join Date
- Sep 2011
- Posts
- 14
- Rep Power
- 0
- 12-06-2011, 04:30 PM #4
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.
- 12-07-2011, 03:32 PM #5
Member
- Join Date
- Sep 2011
- Posts
- 14
- Rep Power
- 0
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 03:47 PM.
- 12-07-2011, 04:08 PM #6
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Re: Need help with random arrays
What does the output tell you?
- 12-08-2011, 03:04 PM #7
Member
- Join Date
- Sep 2011
- Posts
- 14
- Rep Power
- 0
Similar Threads
-
How to use arrays with Math.random
By LennyKosmos in forum New To JavaReplies: 2Last Post: 09-20-2010, 04:44 PM -
using random strings from arrays insult generator
By carden2 in forum New To JavaReplies: 5Last Post: 04-05-2010, 05:51 AM -
LUDecomposition, random arrays.
By rootpi in forum New To JavaReplies: 5Last Post: 12-23-2009, 03:32 PM -
Random numbers and arrays
By caro in forum New To JavaReplies: 6Last Post: 06-10-2009, 01:09 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks