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 5842
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);

}

}
Sign in to post your reply or Sign up for a free account.
Similar topics
by: aemazing 
last post by:
hello, im new to the forum and i wanted to help with my c++ program.
the teacher wants us to design a progrm that would keep track of
airplanes awaitin landing at an airport. the program will...

by: aemazing 
last post by:
i've been tryin to do the following 
Add a new flight number to the end of the queue (got it done)
LAnd the plane at the front of the queue  problems wit it
display the queue  got it done...

by: Peter B. Steiger 
last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C
is a simple word game that involves stringing together random lists of
words. In the Pascal version the whole array was...

by: chaz 
last post by:
here is the html :
<br>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="<%=LABEL_WIDTH%>" class="formtext"><%= HTEXT("Connection
type:")%></td>
<td...

by: javuchi 
last post by:
I just want to share some code with you, and have some comments and
improvements if you want.
This header file allocates and add and delete items of any kind of data
from a very fast array:
...

by: desktop 
last post by:
I have a function that takes two pointers to an array. The first point
to the first element while the other points to the last element.
int nums = { 1, 2, 3, 4, 5, 7, 8, 9};
int* result;
int...

by: StevenChiasson 
last post by:
For the record, not a student, just someone attempting to learn C++. Anyway, the problem I'm having right now is the member function detAddress, of object controller.
This is more or less, your...

by: Lee Crabtree 
last post by:
This seems inconsistent and more than a little bizarre.
Array.Clear sets all elements of the array to their default values (0,
null, whatever), whereas List<>.Clear removes all items from the...

by: CloudSolutions 
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...

by: Faith0G 
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

by: isladogs 
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...

by: ryjfgjl 
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...

by: Charles Arthur 
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone

by: ryjfgjl 
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...

by: BarryA 
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

by: nemocccc 
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

by: Sonnysonu 
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by columnwise with in the specific length.
suppose the i have to...
 