0

I'm having an issue with incrementing 4 nested while loops. Say you had an array of type double, and many integers to increment the loops. Here's an example

 private static double[] approxOfU(double w, double x, double y, double z,
        double u) {
    double a = 0, b = 0, c = 0, d = 0;
    final double[] powerArray = { -5.0, -4.0, -3.0, -2.0, -1, (-1.0 / 2.0),
            (-1.0 / 3.0), (-1.0 / 4.0), 0, (1.0 / 4.0), (1.0 / 3.0),
            (1.0 / 2.0), 1, 2, 3, 4, 5 };
    double[] temp = new double[5];
    double approx;
    int i = 0, j = 0, k = 0, l = 0;

    while (i < powerArray.length) {
        a = powerArray[i];
        while (j < powerArray.length) {
            b = powerArray[j];
            while (k < powerArray.length) {
                c = powerArray[k];
                while (l < powerArray.length) {
                    d = powerArray[l];
                    approx = Math.pow(w, a) * Math.pow(x, b)
                            * Math.pow(y, c) * Math.pow(z, d);
                    if (Math.abs(approx - u) / u <= 0.01) {
                        temp[0] = a;
                        temp[1] = b;
                        temp[2] = c;
                        temp[3] = d;
                        temp[4] = approx;

                    }
                    l++;

                }
                k++;
            }
            j++;
        }
        i++;
    }

    return temp;
}

Where do I put my increments like i++, j++, k++, and l++? The assignment requires only while loops be used on this portion of the project. The for loops part of the project I already got working. Please let me know your thoughts. This code isn't the real code to the assignment but I wanna understand the logic of how to update these nested loops and be able to get the right data from them. When I do output to the screen using the variables below I end up getting 0's so something isn't right. Normally I put the increment by 1 (i++,j++, ... etc) at the end of the curly brace for the loop. In this case that method isn't yielding good results. Please help! :)

3
  • Generally, you don't put the increment at the end of the curly brace for the loop, you put it before the curly brace so it is in the loop and updates correctly so no infinite loop happens Commented Sep 22, 2014 at 3:24
  • let's change this up a bit hang on i'm gonna redit it because the 0's are still appearing. Commented Sep 22, 2014 at 4:04
  • When running this code above the a,b,c, and d up as zeros and so the approx value so something isn't right here Commented Sep 22, 2014 at 4:07

3 Answers 3

1

If you are not doing any magic with the indices, and only need the element of the array in the loop (not the index), then you could use the enhanced for loop:

double[] updateArray = new double[7];
/* fill your array */
for(double i : updateArray) {
    for(double w : updateArray) {
        for(double y : updateArray) {
            for(double x : updateArray) {
                // No incrementing or indices required :)
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

sadly im doing calculations and only while loops are allowed in this part
0

as the same as a for loop, execute the increment at the end of the loop.

double[] updateArray = new double[7];
double w,x,y,z;
int i=0, j=0, k=0, l=0;
while(i < updateArray.length)
{
      w = updateArray[i];
    while(j < updateArray.length)
    {
      x = updateArray[j];
        while(k < updateArray.length)
        {
           y = updateArray[k];
            while(l < updateArray.length)
             {
                  z = updateArray[l];
              l ++;
             }
        k ++;
        }
     j ++;
    }
 i ++;
}

1 Comment

I attempted your response as you see in my new edit. Sadly though when trying to update around that calculation and grab the right values of the array for each integer seems to be difficult. The for loop was much simpler then this but only while loops are allowed in this case.
0

You can do it like this.

double[] updateArray = new double[7];
double w,x,y,z;
int i=0, j=0, k=0, l=0;
while(i++ < updateArray.length -1)
{
      w = updateArray[i];
    while(j++ < updateArray.length -1)
    {
      x = updateArray[j];
        while(k++ < updateArray.length -1)
        {
           y = updateArray[k];
            while(l++ < updateArray.length -1)
             {
                  z = updateArray[l];
             }
        }
    }

}

2 Comments

Look at your suggestion again. It will throw ArrayIndexOutOfBoundsException.
@Le Duy Khanh Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7 at com.test.Test23.main(Test23.java:15)

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.