how to find average of a List java - java

I have a question on how to find the average of a List. I don't know what is wrong with my code, because I did the same for all the three averages but only the first one works, the others just show 0, as shown below in my output:
Average: 58 0 0
Below is my code:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Hurricanes2{
public static void main(String args[]) throws FileNotFoundException{
Scanner scan = new Scanner(new FileReader(new File("/Users/timothylee/hurcdata2.txt")));
List<Integer> year = new ArrayList<Integer>();
List<Integer> windspeed = new ArrayList<Integer>();
List<Integer> pressure = new ArrayList<Integer>();
List<String> name = new ArrayList<String>();
List<Integer> category = new ArrayList<Integer>();
while(scan.hasNext()){
String input = scan.nextLine();
String[] hurricane = input.split("\\s+");
year.add(Integer.parseInt(hurricane[0]));
windspeed.add(Integer.parseInt(hurricane[2]));
pressure.add(Integer.parseInt(hurricane[3]));
name.add(hurricane[4].trim());
category.add(Integer.parseInt(hurricane[5]));
}
int sum = 0;
for(Integer integer : pressure){
sum += integer.intValue();
}
double average = sum / pressure.size();
// create menu
System.out.printf("%50s%n%n", "Hurricanes 1980 - 2006");
System.out.printf("%1s%20s%20s%20s%20s%n", "Year", "Hurricane",
"Category", "Pressure(mb)", "Wind Speed (mph)");
System.out.println("__________________________________________________"
+ "__________________________________");
for(int i = 0; i < year.size(); i++){
System.out.printf("%4d%20s%20d%20d%20d%n", year.get(i), name.get(i)
, category.get(i), pressure.get(i), windspeed.get(i));
}
System.out.println("__________________________________________________"
+ "__________________________________");
int averageCategory = 0;
int averagePressure = 0;
int averageWindSpeed = 0;
for(int i = 0; i < category.size(); i++){
averageCategory = i / category.get(i);
averagePressure = i / pressure.get(i);
averageWindSpeed = i / windspeed.get(i);
}
System.out.printf("%1s%35d%35d%35d%n", "Average: ", averageCategory,
averagePressure, averageWindSpeed);
}
}
How can I fix this? Any help will be greatly appreciated.

I can't see your input, but....
I don't think your for loop is doing what you want it to. Each iteration, it is completely overwriting what is in averageCategory, averagePressure, averageWindspeed with whatever is on the right side of the equals sign.
Is this is what you meant to do? Usually in calculating averages we get a sum and then divide once by the total number of items.

You are doing wrong here:
double average = sum / pressure.size();
In this way you are doing integer division, which is not good when computing average, expecially if the values are near to 1.
You need to cast it:
double average = (double)sum / (double)pressure.size();
also you need to cast here:
for(int i = 0; i < category.size(); i++){
averageCategory = (int) ((double)i / (double)category.get(i));
averagePressure =(int) ((double) i / (double)pressure.get(i));
averageWindSpeed = (int) ((double)i / (double)windspeed.get(i));
}
Also, this last piece of code doesn't seem to calculate any average. You should calculate the sum and divide it for the size of data. But remember to cast integer division to double as I shown.

Related

Math.random with changing values

I have this dilemma with a script
I want to do a Bingo game and here I'd use this Math.random script
public class Bingo{
public static void main(String[]args){
int num = (int) (Math.random() *(75)) +1;
int x = 0;
while(x==0){
System.out.println(num +"\n");
}
}
}
In this case my output is always 34
Is there a way so that my output is always a different number? Thanks!
First using Math.random() gives a floating point value. It is not advisable to use that for random integer generation. So I'll use random.nextInt() here.
Secondly you'll need a set to maintain previously generated numbers and break the loop when size of the set hits 75. You can continue the loop if you've already generated that number.
Here's the code.
int num = 0;
Random r = new Random();
Set<Integer> set = new HashSet<>();
while (set.size() < 75) {
num = r.nextInt(75) + 1;
if (set.contains(num))
continue;
set.add(num);
System.out.println(num + "\n");
}
imports:
import java.io.IOException;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
Based on your requirement to only draw each number once, you will have to choose a different approach. I would add all balls to a list and then shuffle the list. Then you can iterate over the balls (and perhaps break when the game is won) :
final List<Integer> balls = new ArrayList<>();
for (int i = 0; i <= 75; i++) {
balls.add(i);
}
Collections.shuffle(balls);
for (int ball : balls) {
System.out.println(ball); //or whatever your logic is
}
Imports:
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
Your num it is always with the same value because you are not changing it inside while loop.
Try something like:
/* your code here */
while (x==0) {
num = (int) (Math.random() *(75)) +1;
System.out.println(num);
}
Change to
public class Bingo {
public static void main(String[] args) {
int x = 0;
while (x == 0) {
int num = (int) (Math.random() * (75)) + 1;
System.out.println(num + "\n");
}
}
}
Why not use the following code:
Random random = new Random();
int randomNumber = random.nextInt(max + 1 - min) + min;
Where max will be 75 and min is 0. Also both the numbers are inclusive. Please note that the internal algorithm fo math random uses Leniar congruential generator(though not the best), but considering you will be using a 64 bit machine, the numbers should not repeat soon enough.

How to sort the array in ascending order?

I cannot figure out why the r array will not sort into ascending order. I have tried Array.sort and manually sorting the array.
import java.lang.*;
import java.lang.Object;
import java.lang.Integer;
import java.util.Arrays;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class Gaussian {
public static int seed;
public static final int n = 100;
public static void main(String argv[]) {
double r[] = new double[100];
// Initiate the seed from the current time
GregorianCalendar t = new GregorianCalendar();
int t1 = t.get(Calendar.SECOND);
int t2 = t.get(Calendar.MINUTE);
int t3 = t.get(Calendar.HOUR_OF_DAY);
int t4 = t.get(Calendar.DAY_OF_MONTH);
int t5 = t.get(Calendar.MONTH);
int t6 = t.get(Calendar.YEAR);
seed = t6 + 65*(t5+12*(t4+31*(t3+24*(t2+60*t1))));
if ((seed%2) == 0) seed = seed-1;
**************This is the section giving me trouble*****************
// Put the Gaussian random numbers in the array
for (int i=0; i<n-1; i+=1) {
r = rang();
for (int l=0; l<r.length-1; l++) {
if(r[l] < r[l+1]) {
double tempValue = r[l+1];
r[l+1] = r[l];
r[l] = tempValue;
}
}
System.out.println(r[0]);
******************Between these stars*******************************
}
}
// Method to create two Gaussian random numbers from two
// uniform random numbers in [0,1].
public static double[] rang() {
double x[] = new double[1];
double r1, r2;
r1 = - Math.log(1-ranf());
r2 = 2*Math.PI*ranf();
r1 = Math.sqrt(2*r1);
x[0] = r1*Math.cos(r2);
return x;
}
// Method to generate a uniform random number in [0,1]
// following x(i+1)=a*x(i) mod c with a=pow(7,5) and
// c=pow(2,31)-1. Here the seed is a global variable.
public static double ranf() {
final int a = 16807, c = 2147483647, q = 127773,
r = 2836;
final double cd = c;
int h = seed/q;
int l = seed%q;
int t = a*l-r*h;
if (t > 0) seed = t;
else seed = c+t;
return seed/cd;
}
}
For some reason it is giving me this output:
-0.7286443240313888
0.9205595151394262
-0.1201523471771766
-0.2955395834645375
0.5562293071303744
0.5947383124976592
-0.5190410499731951
1.1878905341959594
-0.6530738641804281
1.92941716216534
-1.55458771926982
1.011542837179014
-1.2973072313970084
-0.5115664645635027
-0.4537839981939878
-0.43386113937789456
2.1877083571592637
-0.1869725174568339
1.0427194985616417
0.7491392218512473
-0.2837863829399006
0.09204148771478798
0.08980225475596745
-1.0595943397788652
0.2493101533697332
-1.3926086961785766
0.9722238128294852
0.4490619874581054
1.4379635505387074
1.4550206564181973
-0.9754513444753741
-1.6454765651087158
0.1683214049373476
0.9981636099004854
-0.7289169766110786
1.6612385375332162
0.19025688479326378
0.0830947016802825
0.4674778575126086
-0.9077431792737849
-0.5638299547034225
0.13229202082089384
1.2429372493642745
-0.006685432080368285
2.336192098747748
-0.5450098522726261
-1.6420372037670146
0.3400579125911062
0.48689741262698993
-0.5075527810259783
1.9679760629290328
-1.9114919760463223
0.5633783650935041
0.12871665512520616
-1.8826404473732248
0.16725744941405607
1.049647212107755
0.767071049830706
0.3366261688045942
-1.726395330988362
-0.15241706234915703
-0.17645549457761323
1.098469368528642
-0.3173352964219553
-2.6584067823396675
0.4136264577634812
-1.2506808927401905
2.0462718170224186
-2.380899123430688
-1.0340941198026203
-3.223035072470854
-0.1423807157151033
-0.048464495873010084
1.4690537691472332
0.9110766995396362
-0.040683539673625924
-0.3895836309957472
-0.4793889976948361
0.007621022168540105
0.4151797552606307
1.2734508381903344
0.6398148976757589
-2.0458807284022114
0.23937728902415573
0.09380205942857836
1.331532378407905
-0.09813530948364875
0.9515533393393638
-1.5924626733327882
-1.2504131049626441
0.3674623983411812
0.8204457493547238
0.2214473639135442
0.5573901544532469
1.6349106235864332
-1.4373482822115147
0.38216369985059967
-0.6869980429363977
0.30632157455967757
Instead of sorting the numbers in ascending order.
The reason the the array is not sorting is because of r = rang();.
Initially, double r[] = new double[100]; sets r to an array of length 100. However, r is being reassigned to the result of rang(), which is an array the length of 1. This causes the inner for loop to never run.
I recommend the following changes:
// I'm assuming that you want 100 Gaussian random numbers, not 99
for (int i = 0; i < r.length; i += 1) {
// you could also change rang() to return a
// double instead of an array of double
// because the array only contains 1 double
double[] randomGaussian = rang();
r[i] = randomGaussian[0];
}
// sort the array
Arrays.sort(r);
// print out the array
for(int i = 0; i < r.length; i++) {
System.out.print(r[i] + " ");
}
I figured it out guys!
// Put the Gaussian random numbers in the array
double copy[] = new double[n];
for (int i=0; i<n-1; i+=1) {
r = rang();
double temp = r[0];
copy[i] = temp;
}
// Sort the array
Arrays.sort(copy);
for (int i=0; i<n-1; i++) {
System.out.println(copy[i]);
}
Gives the right output!

A set of random values in an int array of a user specified size

Write a method to generate and return a set of random values in an int array of a user specified size. The values should all be between +/- N, where N is a constant such as 100.
Thank you.
Here's Mine;
import java.util.*;
public class Test
{
public static void main(String[] args)
{
int limit, numbers;
Random random = new Random();
Scanner scan = new Scanner(System.in);
System.out.print ("Enter your limit value for your array: "); //Needs to be positive.
limit = scan.nextInt();
int[] list = new int[limit];
for (int i = 0; i < list.length; i++)
{
System.out.print(list[i] + ", ");
}
numbers = random.nextInt(limit - (0 - limit)) + (0 - limit);
System.out.println (numbers);
System.out.println (list[numbers]);
}
}
public List<Integer> random(int range, int count){
List<Integer> result = new ArrayList<Integer>();
for(int i=0;i<count;i++){
if(Math.random() > 0.5){
//adding positive value with probability of 0.5
result.add((int)(Math.random() * (double)range));
}else{
//adding negative value with probability of 0.5
result.add(-1 * (int)(Math.random() * (double)range));
}
}
return result;
}
If you want to create your own random number generator, the easiest one to implement will be Linear Congruential Generator. Read from wiki and try yourself. Ask here if you need help.

Sorting program not sorting correctly

My program is meant to take in file in the current format with the top number being the max length of the words, the second number being the number of words that are there, and the rest being the numbers to be sorted.
4
10
437 1807 3218 1791 9058 9322 766 9977 16 7143
And then sort it so from lowest to highest. But every time i try to get it working i generally get the highest number in the first spot and the rest is just a jumbled mess. The final three for loops where meant to resemble this pseudo code:
the first power of ten = 10;
for each i up to the maximum number of digits+1
1. for each value v in the array, starting at position 0:
a. isolate the “ith” digit from v (call this digit d) – use the current power
of ten to do this
b. add v to bin[d]
2. for each bin (that is, each bin[j], starting with j=0)
a. while bin[j] is not empty
i. remove the first value from bin[j] and copy it back into the next
position in the array
3. Increase the power of ten to the next power of ten
Any help would be appreciated.
import java.io.*;
import java.util.*;
public class BinSort {
public static void main(String[] args) throws FileNotFoundException {
Scanner sc = new Scanner(new File(args[0]));
int length = sc.nextInt();
int size = sc.nextInt();
int[] temp = new int[size];
sc.nextLine();
for(int i = 0;i < temp.length;i++){
temp[i] = sc.nextInt();
}
sort(temp,size,length);
}
public static void sort(int[] input, int length,int size){
ArrayList<Integer>[]bin=(ArrayList<Integer>[]) new ArrayList[10];
for(int i = 0;i<length;i++){
bin[i] = new ArrayList<Integer>();
}
int power = 10;
for(int i = 0;i<size+1;i++){
for(int v = 0;v<input.length;v++){
int d = input[v] % power;
if(power>10)
d = d/ (power/10);
bin[d].add(input[v]);
}
for(int j = 0;j<10;j++){
while(!bin[j].isEmpty()){
int temp = bin[j].get(0);
bin[j].remove(0);
input[j] = temp;
}
}
power = power*10;
}
System.out.println("Final result");
System.out.println(Arrays.toString(input));
}
}
input[j] = temp;
should read
input[pos] = temp;

java array assignment - use of numeric literal

Create an application containing an array that stores 20 double values,
such as 2.34, 7.89, 1.34, and so on. The application should:
* Display the sum of all the array elements
* Display the average of all the array elements
* Display the largest value of all the array elements
This is a java class assignment. I got a bad grade on this one because my professor said I used a numeric literal. I lost 28 points out of 40. Did I design the solution so bad? His exact comments:
"The program you submitted uses numeric literals in place of the array’s length.
This cause several runtime errors when I change the size of your array and tested the code."
AND my solution:
import java.util.Random;
import java.util.Arrays;
public class MyArray{
public static double[] doubles;
public static void main(String args[]) {
MyArray.createDoublesArray();
MyArray.displayDoublesArray();
MyArray.displaySum();
MyArray.displayAverage();
MyArray.displayTheLargestValue();
}
/*Fill up the double Array class member*/
public static void createDoublesArray(){
doubles = new double[20];
//Create Random object
Random r=new Random();
double rangeMin = 1, rangeMax = 9;
//Generate random double number - 20 times within the range of 2 to 9
for(int i=0;i<20;i++) {
//Generate random double numbers and round them to the two decimal places
double randomdouble = Math.round((rangeMin + (rangeMax - rangeMin) * r.nextDouble())*100.0)/100.0;
doubles[i] = randomdouble;
}
}
/*Display the double Array*/
public static void displayDoublesArray(){
String delimiter;
Arrays.sort(doubles);
System.out.println("The double array: ");
// iterate through all the array elements
System.out.print("{");
for(int i=0;i<20;i++) {
if(i < 19){
delimiter = ", ";
}
else{
delimiter = "}";
}
System.out.print(doubles[i]+ delimiter);
}
System.out.println("\n");
}
/*Displays the sum of the double array.*/
public static void displaySum() {
//initialize the sum with 0
double sum = 0.0;
// iterate through all the array elements
for (int i = 0; i < doubles.length; i++) {
// add up each element to the sum variable
sum += doubles[i];
}
// display the sum
System.out.println("The sum is: " + Math.round(sum*100.0)/100.0 + "\n");
}
/*Displays the average of the double array.*/
public static void displayAverage() {
// initialize the sum with 0
double sum = 0.0;
// iterate through all the array elements
for (int i = 0; i < doubles.length; i++) {
sum += doubles[i];
}
// display the average by dividing the sum to the length of the array
System.out.println("The average is: " + Math.round((sum / doubles.length)*100.0)/100.0 + "\n");
}
/*Displays the largest value from the double array */
public static void displayTheLargestValue() {
//initialize the largest value with the first element
double largestValue = doubles[0];
//iterate through the remaining elements (ignore the first)
for (int i = 1; i < doubles.length; i++) {
// check if "i" element is greater then the current largest value
if (doubles[i] > largestValue) {
largestValue = doubles[i];
}
}
// display the largest value
System.out.println("The largest value is: " + largestValue);
}
}
I'm guessing that instead of things like the following
for(int i=0;i<20;i++)
he wanted you to use the length property
for(int i=0;i<doubles.length;i++)
additionally, here
//initialize the largest value with the first element
double largestValue = doubles[0];
you're assuming that doubles is not empty, and when it is empty, that will thrown an exception
To allow us to maintain this code easily I would 1st of all create:
private final static int SIZE = 20;
createDoublesArray:
public static void createDoublesArray(){
doubles = new double[SIZE];
//Create Random object
Random r=new Random();
double rangeMin = 1, rangeMax = 9;
//Generate random double number - 20 times within the range of 2 to 9
double randomdouble = 0.0; // <- we don't want to initiate double in 'for' loop
for(int i=0;i<SIZE;i++) {
//Generate random double numbers and round them to the two decimal places
randomdouble = Math.round((rangeMin + (rangeMax - rangeMin) * r.nextDouble())*100.0)/100.0;
doubles[i] = randomdouble;
}
}
displayDoublesArray:
/*Display the double Array*/
public static void displayDoublesArray(){
String delimiter;
Arrays.sort(doubles);
System.out.println("The double array: ");
// iterate through all the array elements
StringBuilder buff = new StringBuilder(); // use buffer
buff.append("{");
for(int i=0;i<SIZE;i++) {
if(i < SIZE-1){
delimiter = ", ";
}
else{
delimiter = "}";
}
buff.append(doubles[i]+ delimiter);
}
buff.append("\n");
System.out.println(buff.toString());
}
Our code seems a bit more generic and i can change SIZE (if needed) without actually change my code.
I agree with Maxim Shoustin...
Just one comment to add
1) It is not required to always use double. for eg.
double rangeMin = 1, rangeMax = 9; //can be replaced by int.
ref: Primitive Data Types

Categories