J.Doe
J.Doe

Reputation: 9

Having issues with an array method

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;
}

Upvotes: 1

Views: 162

Answers (7)

VHS
VHS

Reputation: 10184

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;

Upvotes: 1

humblefoolish
humblefoolish

Reputation: 429

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;
}

Upvotes: 1

Lavaman65
Lavaman65

Reputation: 863

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);
}

Upvotes: 0

FredK
FredK

Reputation: 4084

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.

Upvotes: 0

simiam
simiam

Reputation: 1

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.

Upvotes: 0

Yaman Jain
Yaman Jain

Reputation: 1251

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

Upvotes: -1

dogrgaut
dogrgaut

Reputation: 27

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.

Upvotes: 1

Related Questions