Hey. I'm having some difficulty in Array lists. I have to find the average of all the numbers in an array list. That's my first task. My second one is to find the mode of all the numbers, meaning the number that shows up the most. And the third one is to find a standard deviation, which I will get to later.
Anyway, right now I'm doing Average. 
public class Statistics

{

public static void main(String args[])

{

ArrayList <Integer> intList = new ArrayList <Integer>();

int avgTemp = 0;

int count1 = 0;


for (int i = 0; i < intList.size(); i++)

{

int b = (int) (Math.random() * 10);

intList.set(i, b);

}


for (int i = 0; i < intList.size(); i++)

{

int j = intList.get(i);

avgTemp += j;

count1++;

}


System.out.println("The average of all the values in the Array List is " + avgTemp/count1);

That's what I have so far. The first for loop makes a random array list, but I think it's wrong. And the second one will store all the integers of the arrayList into avg temp, and then I'll get the average in my output statement. But it doesnt work.
22 5674
That's what I have so far. The first for loop makes a random array list, but I think it's wrong.
Greetings! Let's start with the first loop. Why do you think it's wrong? How can you prove or disprove your suspicions?

import java.util.ArrayList;


public class Statistics

{

public static void main(String args[])

{

ArrayList <Integer> intList = new ArrayList <Integer>();


int size = intList.size();

for (int k = 0; k < size); k++)

{

intList.add((int)(Math.random()*100));

}


double count1 = 0;

for (int i = 0; i < intList.size(); i++)

{

count1 += intList.get(i);

}


double average = (count1 / (intList.size()));


System.out.println("The average of all the integers in the Array List is " + average);

}

}

I modified it a little but it still doesn't work. In this one, I don't understand why it won't work. It returns "NaN"...
NaN means "Not a number". A double gets this value if you perform an illegal operation like taking the square root of a negative number. Look at the defintion of average:  double average = (count1 / (intList.size()));
print out the value of count1 and of intList.size()  what are their values?
NaN means "Not a number". A double gets this value if you perform an illegal operation like taking the square root of a negative number. Look at the defintion of average:  double average = (count1 / (intList.size()));
print out the value of count1 and of intList.size()  what are their values?
oh
count is 0.0
and intList is 0
but why? I am adding random numbers to the Array List. and the () at the end of the declaration of the Array List should equal 10. So intList.size() should be 10. and count should be all the random numbers added together....
oh
count is 0.0
and intList is 0
but why? I am adding random numbers to the Array List. and the () at the end of the declaration of the Array List should equal 10. So intList.size() should be 10. and count should be all the random numbers added together....
I don't understand what you mean by "and the () at the end of the declaration of the Array List should equal 10".
1. How many numbers are you adding to intList?
2. What is the size of intList after you execute:  ArrayList <Integer> intList = new ArrayList <Integer>();
I don't understand what you mean by "and the () at the end of the declaration of the Array List should equal 10".
1. How many numbers are you adding to intList?
2. What is the size of intList after you execute:  ArrayList <Integer> intList = new ArrayList <Integer>();
I thought that by default ArrayList <integer> intList = new ArrayList <integer>() has the size of 10....
So how do I set the size?
I thought that by default ArrayList <integer> intList = new ArrayList <integer>() has the size of 10....
So how do I set the size?
I think you are confusing size with capacity. Capacity is an implementation detail that can be safely ignored. When you create an ArrayList with that constructor, its initial size is 0. It is empty.
The easieest way to increase that list to size 10 is to add 10 values to it! In your first loop, why not use an upper bound of 10 instead of intList.size()?
I think you are confusing size with capacity. Capacity is an implementation detail that can be safely ignored. When you create an ArrayList with that constructor, its initial size is 0. It is empty.
The easieest way to increase that list to size 10 is to add 10 values to it! In your first loop, why not use an upper bound of 10 instead of intList.size()?
Ooh, thanks. I got it working.
The next part of my program needs to find the Standard Deviation of the Array List.
This is how to find it
a. Find the average of the list of numbers.
b. Determine the difference of each number from the average, and square each difference. Sum all the differences.
c. Divide this sum by (the number of values  1).
d. Take the square root of the above division result from step c.
Example, given this list of numbers: 7 4 5 9 10
a. The average = 7
b. Sum of square of differences:
(7  7)2 + (4  7)2 + (5  7)2 + (9  7)2 + (10  7)2
0 + 9 + 4 + 4 + 9 = 26
c. 26(51) = 6.50
d. 6.50 = 2.55
So, I'm gonna give a shot at it now. And I'll see how I do, if you guys have any ideas, please let me know
I did the Standard Deviation, but I want to round this off to 2 decimal places 
import java.util.ArrayList;

import java.lang.Math;


public class Statistics

{

public static void main(String args[])

{

ArrayList <Integer> intList = new ArrayList <Integer>();

double count1 = 0.0;

double sdTemp = 0.0;


for (int k = 0; k < 10; k++)

{

intList.add((int)(Math.random()*10));

}


for (int i = 0; i < intList.size(); i++)

{

count1 += intList.get(i);

}


double average = (count1 / (intList.size()));


System.out.println("The average of all the integers in the Array List is " + average);


for (int j = 0; j < intList.size(); j++)

{

int q = intList.get(j);

sdTemp += Math.pow((q  average), 2);

}


double sdTemp2 = (sdTemp / (intList.size()  1));

double standardDeviation = Math.sqrt(sdTemp2);


System.out.println("The Standard Deviation is " + standardDeviation);

Heh, never mind, I got it.
Okay, now here is the hardest part
I need to find the mode of the Array List, meaning the number that appears the most... 
import java.util.ArrayList;

import java.lang.Math;

import java.text.DecimalFormat;


public class Statistics

{

public static void main(String args[])

{

DecimalFormat df1 = new DecimalFormat("####.00");

ArrayList <Integer> intList = new ArrayList <Integer>();

double count1 = 0.0;

double sdTemp = 0.0;


for (int k = 0; k < 10; k++)

{

intList.add((int)(Math.random()*10));

}


for (int i = 0; i < intList.size(); i++)

{

count1 += intList.get(i);

}


double average = (count1 / (intList.size()));


System.out.println("The average of all the integers in the Array List is " + average);


for (int j = 0; j < intList.size(); j++)

{

int q = intList.get(j);

sdTemp += Math.pow((q  average), 2);

}


double sdTemp2 = (sdTemp / (intList.size()  1));

double standardDeviation = Math.sqrt(sdTemp2);


System.out.println("The Standard Deviation is " + df1.format(standardDeviation));


}

}

This is what I have so far. If anyone could get me started, that would be helpfult
How would you calculate the mode(s) by hand?
And can you make any assumptions about the range of the data values? You random number formula only generated numbers in the range 09. Can you assume that or do you have to assume any int is possible?
How would you calculate the mode(s) by hand?
And can you make any assumptions about the range of the data values? You random number formula only generated numbers in the range 09. Can you assume that or do you have to assume any int is possible?
from 0  99
I would calculate the mode by hand by doing:
Storing each value into a variable and just adding 1 to it everytime i pass it. Then i print out the one that has the highest number.
But doing it that way would take forever.
from 0  99
I would calculate the mode by hand by doing:
Storing each value into a variable and just adding 1 to it everytime i pass it. Then i print out the one that has the highest number.
But doing it that way would take forever.
That's not how you would do it by hand  that's a complicated method of solving the problem with a computer. Forget 'variables' and 'printing'  If I gave you a list of numbers on a piece of paper, and I gave you a pencil, could you write down the mode of that list of numbers? Figure out what you do to determine the mode in this way, find a way to generalize it (a.k.a. write a set of rules to be followed every time), and only then start coding.
That's not how you would do it by hand  that's a complicated method of solving the problem with a computer. Forget 'variables' and 'printing'  If I gave you a list of numbers on a piece of paper, and I gave you a pencil, could you write down the mode of that list of numbers? Figure out what you do to determine the mode in this way, find a way to generalize it (a.k.a. write a set of rules to be followed every time), and only then start coding.
I would tally each number that appeared and then see which one appeared the most.
To determine the mode, I have to see which number appears the most.
I would tally each number that appeared and then see which one appeared the most.
It's starting to sound like an algorithm, because you've broken it down into two phases:  Tally each number
 Then see which one(s) appears the most
It's starting to sound like an algorithm, because you've broken it down into two phases: Tally each number
 Then see which one(s) appears the most
Heh, yeah.
But my idea, in code, for it is to make different variables to store each number. But that would take forever.
Heh, yeah.
But my idea, in code, for it is to make different variables to store each number. But that would take forever.
You need 100 variables, one each to hold the tally count for that data value. Now if there was only an easy way to declare variables in bulk... Hmmm.... anything ring a bell... declare 100 at a time...
You need 100 variables, one each to hold the tally count for that data value. Now if there was only an easy way to declare variables in bulk... Hmmm.... anything ring a bell... declare 100 at a time...
i was thinking about using an array.... would that help
i was thinking about using an array.... would that help
Yes, indeed. Why not try using an array.
Yes, indeed. Why not try using an array.
Although, if the point of this exercise is to use lists, you could also using a list like an ArrayList instead.
I see. Yeah, ArrayLists are much easier to use.
I see. Yeah, ArrayLists are much easier to use.
My kneejerk reaction is to use the collection framework over an array in all but trivial cases, because the collection framework is more flexible.
But even so, sometimes the array code is simpler:  import java.util.*;


public class Comparison {

private static final int MAX = 10;


public static void main(String[] args) {

int[] v = new int[MAX]; //MAX zeroes

List<Integer> list = new ArrayList<Integer>(Collections. nCopies(MAX, 0)); //MAX zeroes


//increment the values at odd offsets

for(int i=1; i<v.length; i+=2) {

v[i]++;

}


//increment the values at odd offsets

for(int i=1; i<list.size(); i+=2) {

list.set(i, 1 + list.get(i));

}


System.out.println(Arrays.toString(v));

System.out.println(list);

}

}
Post your reply Sign in to post your reply or Sign up for a free account.
Similar topics
1 post
views
Thread by aemazing 
last post: by

1 post
views
Thread by aemazing 
last post: by

8 posts
views
Thread by Peter B. Steiger 
last post: by

4 posts
views
Thread by chaz 
last post: by

10 posts
views
Thread by javuchi 
last post: by

5 posts
views
Thread by desktop 
last post: by
 
35 posts
views
Thread by Lee Crabtree 
last post: by
          