java - printing an int[] array with decimal approximations -
how return array of sequence [1/1 , 1/2, 1/3] , on decimal approximations int[] array? have tried far:
public static int[] decimalapproximations (int arraysize) { int [] sequence = new int[arraysize]; for(double = 1; <= arraysize; i++) { sequence[(int)(i)-1] = (int)(1.0/i); } return sequence; }
but still prints 1 1/1 , 0 because of int truncation. there possible way have decimal approximations in array?
as mentioned in comments, int array can not store decimal values, if want array sequence, might help:
public double[] decimalapproximations (int arraysize) { double [] sequence = new double[arraysize]; for(int = 0; < arraysize; i++) { sequence[i] = 1.0/(i+1); } return sequence; }
Comments
Post a Comment