FlyingKalamari
FlyingKalamari

Reputation: 15

Multiplying the array with itself

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

Upvotes: 1

Views: 2020

Answers (10)

Kyle
Kyle

Reputation: 374

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.

Upvotes: 0

tonga
tonga

Reputation: 11981

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

Upvotes: 0

Yehia Awad
Yehia Awad

Reputation: 2978

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

Upvotes: 0

Paul Samsotha
Paul Samsotha

Reputation: 209132

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

Upvotes: 0

Nishan
Nishan

Reputation: 2871

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

Upvotes: 0

Paperwaste
Paperwaste

Reputation: 685

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

change to

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

Upvotes: 0

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 280172

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[].

Upvotes: 1

Connor Neville
Connor Neville

Reputation: 7361

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.

Upvotes: 0

kba
kba

Reputation: 19476

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.

Upvotes: 0

Justin Jasmann
Justin Jasmann

Reputation: 2353

I think you mean

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

z is an array, not a double.

Upvotes: 0

Related Questions