0

I would appreciate the communities support on how to multiply an array labelled as double by an array labelled int, which returns a array similar to the logic below, then stores the value?

[1, 2][1, 2, 3]

1*1   2*1   
1*2   2*2   
1*3   2*3   

[1, 2, 3][2, 4, 6]

I have tried:

import java.util.Arrays;

public class exercise_3_3 {
public static void main(String[] args) {
    double[] conversion = new double[] {0.62, 0.54};
    int[] distances = new int[] {2, 5, 10};
    double kilometers = (double)(conversion * distances);
    System.out.println(Arrays.toString(kilometers));
}
}

However, I get the following error:

The operator * is undefined for the argument type(s) double[], int[]

I can imagine this is possible with a for-loop, however, I have not reached this stage yet on the book I'm studying from. Though, I would really appreciate an answer without a for-loop and one with the use of a for-loop.

Using the recommended reference, I have come up with this, although It does not work effectively. I would really appreciate some feedback towards my solution:

import java.util.Arrays;

public class testLoop {
static double b[] = {0.62, 0.54};
static int a[] = {2, 5, 10};

public static void mul() {
    double c[] = new double[1];
    
    for(double i=0; i<b.length;i++) {
        for(int j=0;j<a.length;j++) {
            c[j] = 0;
            
            System.out.println(Arrays.toString(c));
        }
    }
}
}

Also, could someone explain to me whether I require a multidimensional array for multiplying a double vector with an int vector, and why this is necessary?

Lime
  • 738
  • 5
  • 17
  • 2
    @luk2302 Answers are a little too complex for me to understand with my current skills. To provide an idea of my experience - I've only been practicing for a few hours. – Lime Apr 16 '21 at 12:25
  • Does not really matter. You want a complex thing, you get a complex solution. `for` loops are not that complicated. SO is still a [site for professional and enthusiast programmers](https://stackoverflow.com/tour). – luk2302 Apr 16 '21 at 12:27
  • 1
    That's not the error I get when I try to compile your code. Please edit your question so that the code and the error are consistent. – tgdavies Apr 16 '21 at 12:28
  • 1
    @luk2302 Perhaps a simpler solution is possible? (Occams razor); (At)tgdavies That's exactly what I get when I run my code. – Lime Apr 16 '21 at 12:29
  • I guess I'll carry on reading until I'm adept enough for a solution ... – Lime Apr 16 '21 at 12:34
  • Of course you need a multidimensional output array because you are basically doing a matrix multiplication (as the duplicate performs) In theory you can get by without using any loops and any multidimensional arrays if you just hardcode the fact that the output will always be 2x3 large because the input is always 2x1 and 1x3 large, but at that point you will not have learned / gained anything. – luk2302 Apr 16 '21 at 14:02
  • @luk2302 I see, thank you for the response. I'll have to set this as a future challenge for myself when I delve into for-loops more deeply. – Lime Apr 16 '21 at 16:49

0 Answers0