1

I need help multiplying the same array with it self (squaring it). I tried copying it and make a new array but I still get the error of incompatible types. The error is higlighted over radius1 stating incompatible types.

public static double[] part1(double[] z, double[] radius,double [] radius1)
{
    for(int index=0; index <10; index++)
    {
       z= radius[index]*radius1[index];
      }



    // The formula to calculate gravity is:
    // 6.67E-11 times the massOfPlanet divided by the radius of the planet squared

    return z;
}

     public static void main(String[] args)throws IOException
    {
    double[] radius = {2439.7, 6051.9, 6378, 3402.5, 71492, 60270, 25562, 24774, 1195};
    double[] radius1 = {2439.7, 6051.9, 6378, 3402.5, 71492, 60270, 25562, 24774, 1195};
    double z[]= new double [9];
}

10 Answers 10

1

Consider this

z= radius[index]*radius1[index];

z is declared as a double[]. You are accessing the components in the array radius and radius1 with the expressions

radius[index] * radius1[index];

This evaluates to a double value.

You cannot assign a double value to a reference variable of type double[].

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

Comments

0

I think you mean

z[index] = radius[index] * radius1[index];

z is an array, not a double.

Comments

0

You don't need to make a copy of the array if you just want to square it, you can just multiply each entry with itself and store it where you read it.

public static double[] squareArray(double[] arr) {
  for (int i=0; i < arr.length; i++) {
    arr[i] = arr[i] * arr[i];
  }
  return arr;
}

Additionally, you use index < 10 as a condition, but it's better to have it dynamic by reading the length (the number of elements) of the input array arr.

Comments

0

You are getting the error because you are trying to declare the variable z (which is an array) as the product of multiplying two doubles (also a double). If you want each INDEX in z do have the product of the two elements, set z[index], not z, to the product.

Comments

0

Z is an array, the result of radius[index]*radius1[index] is a double

change to

z[index] = radius[index]*radius1[index]

Comments

0

You don't need two arrays to hold the "radius" values. You can keep them in one array.

Also, z is an array, not a double.

public static double[] part1 (double[] z, double[] radius)
{
     for (int index = 0; index < 10; index++)
    {
        z[index] = radius[index] * radius[index];
    }
    return z;
}

Comments

0

Try this

public static double[] part1(double[] radius,double [] radius1) 
{

    double[] z = new double[radius.length];
    for(int index=0; index <10; index++)
    {
       z[index]= radius[index]*radius1[index];
    }

    return z;
}

public static void main(String[] args)throws IOException
{
    double[] radius = {2439.7, 6051.9, 6378, 3402.5, 71492, 60270, 25562, 24774, 1195};
    double[] radius1 = {2439.7, 6051.9, 6378, 3402.5, 71492, 60270, 25562, 24774, 1195};
    double z[]= par1(radius, radius1);
}

Comments

0

what you need to do is change the type z in the for loop

z[index]= radius[index]*radius1[index];

You forgot you already declared as array

Comments

0

You should create an array inside the method part1 and return it. Also it's better to use radius.length than hard-coded number as the loop termination condition. In your case, 10 is incorrect since the length of radius is 9.

import java.util.Arrays;

public class ArrayTest {
    public static double[] part1(double[] radius, double[] radius1) {
        double[] z = new double[radius.length];
        for (int index = 0; index < radius.length; index++) {
            z[index] = radius[index] * radius1[index];
        }

        // The formula to calculate gravity is:
        // 6.67E-11 times the massOfPlanet divided by the radius of the planet
        // squared

        return z;
    }

    public static void main(String[] args) {
        double[] radius = { 2439.7, 6051.9, 6378, 3402.5, 71492, 60270, 25562,
                24774, 1195 };
        double[] radius1 = { 2439.7, 6051.9, 6378, 3402.5, 71492, 60270, 25562,
                24774, 1195 };
        double z[] = part1(radius, radius1);
        System.out.println(Arrays.toString(z));
    }
}

Comments

0

You are attempting to set z, which is a double[]‚ to the product of two doubles‚ which evaluates to a double. Setting an array to a single value is not allowed in Java.

Additionally, your for loops 10 times, even though your arrays only contain 9 values, which would result in an ArrayIndexOutOfBoundsException.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.