0

My array has total 10 indices and I'm trying to set array[0] = 1/1, array[1] = 1/2, array[2] = 1/3, array[4] = 1/4 and so on.

After the calculation I want to display the elements with Sytem.out.println();

What am I doing wrong?

public static void main(String[] args) {
    final int VALUE =1;
    int[] array = new int[10];
    int counter=10;
    double result =0;
    for(int i =0; i<array.length; i++)
    {
        array[0]=VALUE/i;
        array[1]=VALUE/i;
        array[2]=VALUE/i;
        array[3]=VALUE/i;
        array[4]=VALUE/i;
        array[5]=VALUE/i;
        array[6]=VALUE/i;
        array[7]=VALUE/i;
        array[8]=VALUE/i;
        array[9]=VALUE/i;

    System.out.println( array[0] +" " + array[1] +" " + array[2] +" " + array[3]+" " +  array[4]+" "+array[5]+" " +array[6] +" " + array[7]+" " +   array[8]+" " +  array[9]+" " +  array[10]);
    }
}
3
  • you simply just want array[i] = VALUE/i; Commented Apr 18, 2016 at 16:23
  • 1
    What do you think array[0]=VALUE/i; does? What about array[1]=VALUE/i;, etc? What will they do differently each time through the loop? Commented Apr 18, 2016 at 16:23
  • i thought that, the i changes every single turn (i increasing by for loop +1) but it does not do that as i expect @azurefrog Commented Apr 18, 2016 at 16:57

2 Answers 2

5

Two problems. The first is that your loop already takes care of going over each index in the array, so you can just do something like:

for (int i = 0; i < array.length; i++)
{
    array[i] = 1.0/(i+1);
    System.out.println(array[i] + " ");
}

The other is that you use an array of integers. By definition, an integer cannot be a fraction. Try using an array of doubles instead:

double[] array = new double[10];
Sign up to request clarification or add additional context in comments.

4 Comments

Wasn't even thinking about how the OP defined the array. Good catch.
@Brandon would be a good idea to update your answer with the same =)
i've two questions. First of all thank you for your answer. I dont understand 2 thinks why didn't you use VALUE variabel. Second is does not i increase +1 already. If it is not why?
@LenaMonikaMarshall I simply used 1.0 instead of the VALUE variable - that's basically the same thing since VALUE was set to 1 anyway. You could use VALUE if you wanted (just set it to a double instead of an int). As for your other question, I divide by (i + 1) because on the first iteration of your loop, i is 0. In that case, you don't want to get 1 divided by 0, you want to get 1 divided by one. So you need to use i + 1 instead of i.
4

You are using a loop and also doing it inside manually. Just use the loops iterations to calculate it, and use the value of i for your array values.

for(int i = 0; i < array.length; i++)
{
    array[i] = VALUE/(i+1);
}

System.out.println( array[0] +" " + array[1] +" " + array[2] +" " + array[3]+" " +  array[4]+" "+array[5]+" " +array[6] +" " + array[7]+" " +   array[8]+" " +  array[9]);

Another problem, as mentioned by others, it that your result would be in fraction or floating point, and you are trying to store it in an int array

Use a float array instead

float[] array = new float[10];

7 Comments

Put parentheses around i+1 so it's VALUE/(i+1) and it'll be good to go.
@BrandonWillis, Thanks. Done.
Keep in mind storing the results of division using an integer.
@CubeJockey, I wasn't updating as that was mentioned in other answer. Never mind, Updated. :)
tthank you for reply @Pallavi. I write the code as you send and that's give eror. Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10 at kapital11.kaptal11.main(kaptal11.java:14)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.