1

Write a method named createDoubles that builds an array of floating point values that represent the squares of the numbers from 10.0 to 13.0, in steps of 0.5, inclusive of both boundaries. The method has no parameters and returns an array of doubles. There are exactly 7 numbers in this array. I keep trying nut nothing is working. Thanks!

public static double[] createDoubles(){
    double[] dArray= new double[7];
    double[] squareArray= new double[dArray.length];

    for(int i= 0; i< dArray.length-1; i+=0.5){

        squareArray[i]= dArray[i]* dArray[i];
    }
    return squareArray;
}
4
  • Can you provide the output of what your code currently produces? Commented Feb 28, 2017 at 2:27
  • It is not producing anything Commented Feb 28, 2017 at 2:29
  • Why are you creating two arrays in the method? Also, you create dArray, but fail to place any values in it, then try to reference those uninitialized values. And your loop will not satisfy the requirement that the output array has the first value equal to 100. Commented Feb 28, 2017 at 2:32
  • Fixed the incrementing output is now: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] Commented Feb 28, 2017 at 2:32

7 Answers 7

1

If you are incrementing i with 0.5, how do you expect array index to work in loop? Can you please double check the code.

Sign up to request clarification or add additional context in comments.

Comments

1

Something like this will work. Idea is that array indexes increment in steps of 1 and not 0.5 as your loop is expecting.

public static double[] createDoubles(){
    double[] dArray= {10.0,10.5,11.0,11.5,12.0,12.5,13.0};
    double[] squareArray= new double[dArray.length];
    for(int i= 0; i< dArray.length; i++){
        squareArray[i]= dArray[i]* dArray[i];
    }
    for(double f: squareArray)
        System.out.println(f);
    return squareArray;
}

Comments

1

Try the following way:

public static double[] createDoubles(){
  double[] squareArray= new double[7];
  double number = 10.0;
  int i=0;
  while(number <= 13.0) {
     squareArray[i] = number*number;
     number = number + 0.5;
     i++; 
  }
}
return squareArray;

1 Comment

You are welcome. Does my answer fully address your question?
0

You can see the following demo:

int i = 0;
i = i + 0.5;

The i will always be 0. That means your method will not stop when you invoke it.

Comments

0

Here's one way to do it, but probably not what the teacher wants:

public static double[] createDoubles() {
   double[] answer = {10.0, 10.5, 11.0, 11.5, 12.0, 12.5, 13.0 );
    // Actually, the above values should be 100,m ... 169.
    // but I'm too laze to do the math
   return answer;
}

Note that it is never a good idea to increment a loop index by a non-integer value. You might be surprised if you tried your method for increments of 0.1 instead of 0.5.

Comments

0

The reason this code isn't running but isn't working is because java is rounding down the i+=0.5 to i+=0, which is causing i to remain equal to 0, your loop never to exit, and your code not to do anything at all. To fix this part of your code, change the i+=0.5 to i+=1. However, as you do not declare anything in the square array, the resulting array that you return is nothing but 0s. Fix that by defining whatever you want in the d array, and multiplying those values together to create the values in the array you want to return. Or, you could simply declare put each value into the array like so:

for(int i= 0; i<= dArray.length-1; i+=1){
   squareArray[i]= Math.pow(10 + (0.5*i), 2);
}

Comments

-1

squareArray[integer+double] is not a valid array index. Array index are always integer.

Comments

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.