-2

A quick question that bugs me (both from a mathematical perspective and implementation-wise). How do you multiply two one-dimensional arrays?

If we have:

int[] a = {1,2,3};
int[] b = {4,5,6};

And we wanna put the result into a variable c, how do you do the math and the implementation? Should c also be one-dimensional or two-dimensional?

Thank you in advance!

EDIT: To everyone asking what I want. I'm trying to solve a math problem that literally tells me:

a = {1,2,3};
b = {4,5,6};

c = a * b; //what is c?

I found nothing on the internet on how to do it mathematically and I am equally puzzled on how to do it in a programming language.

Eragon20
  • 483
  • 1
  • 7
  • 20
tudor balus
  • 149
  • 2
  • 10
  • 1
    show what you have tried. – Omore Apr 17 '17 at 19:26
  • 2
    What do you mean by "multiply two one-dimensional arrays"? Without an actual definition of the operation you want to perform, your question is about as meaningful as "how do you multiply two bananas". – user2357112 Apr 17 '17 at 19:26
  • Eh? It depends on how you perform the multiplication, obviously. Per element pair? Or just all of the values? We can't tell you if you can't tell us. – domsson Apr 17 '17 at 19:26
  • 1
    It's up to you to define what multiplication means for arrays. – Bill the Lizard Apr 17 '17 at 19:26
  • It means we have an extra variable c = a * b. What are the elements in c? What variable type is it? What are the steps used to reach those values? – tudor balus Apr 17 '17 at 20:06
  • The question is insufficiently defined. My vague memories of Linear Algebra class tell me that `a * b` could be any of: 1. a real (or complex?) number (dot product), 2. a 3×3 matrix (column vector times row vector), or 3. a 1×1 matrix (row vector times column vector). In the Python / NumPy world, it could also be a 3-element array containing the products: `[a0*b0, a1*b1, a2*c2]`. There probably exist still more ways to interpret "multiply two arrays". – Kevin J. Chase Apr 17 '17 at 21:34
  • Or, since your question is tagged [java], the answer is that there _is_ no answer; `c` does not exist. Compiling something like this fails: "error: bad operand types for binary operator '*'". – Kevin J. Chase Apr 17 '17 at 21:39

1 Answers1

1

I'm not sure if you are trying to find the sum if everything, or trying to create a matrix with the multiplication.

For sum refer to duffymo's answer.

For a new array, the end product will be:

int[][] c= {{4, 8, 12}, {5, 10, 15}, {6, 12, 18}};

Idea: You can just loop through both of them and multiply each index. Then store the products in int[][] c. You can also just have them in a list depending on your implementation like so: c = {4, 8, 12, 5, 10, 15, 6, 12, 18}.

int[][] c = new int[a.length][b.length];
// int[] c = new int[a.length * b.length];
for(int i = 0; i < a.length; i++){
    for(int j = 0; j < b.length; b++){
        c[i][j] = a[i] * b[j];
        // c[i * a.length + j] = a[i] * b[j]; if you want to store it in a 1D array
        // Try a few examples to see why this will work for 1D array
    }
}
Eragon20
  • 483
  • 1
  • 7
  • 20
  • Thank you for the answer.If I do it like that, won't the "base" array be very important? For example in your calculation, you use b as a base and multiply each element in b with the whole array a. – tudor balus Apr 17 '17 at 20:09
  • Yes, both arrays will be important because they are being multiplied by each other. Mathematically it is taking a 3x1 array, another 3x1 array, and multiplying each index of one array with each index of the other array. In the end, you get a 3x3 array. – Martin Morales Apr 17 '17 at 20:20
  • One risk of answering a question so ill-defined (see [my comment](https://stackoverflow.com/questions/43458155#comment73976000_43458155)) is that you might waste your time answering the wrong question, which is always disheartening. I don't know if that's happened here, but that's pretty much my point --- the question is so vague that _I can't tell_ if this is a helpful answer. I suggest waiting before answering questions like this, to avoid frustration and possible downvotes (an edit that clarifies the question could make your answer nonsensical, although we try to limit such drastic edits). – Kevin J. Chase Apr 18 '17 at 00:00
  • @Martin Morales : Thank you very much Martin! Now I understand both the mathematical process and how to do the implementation. Based on the above, I guess that the order of multiplying is very important. Kevin J. Chase : While you are right that it's hard to answer a vague question, I believe it's unethical to hold back from answering just for the fear of being downvoted. This is a knowledge site, we should keep out focus on knowledge, not our rep on the site. Also, I added clarifying details and Martin got what I was referring to. A nice day to both of you & thank you very much! – tudor balus Apr 18 '17 at 08:22
  • 1
    @tudorbalus: My main concern is warning beginners who haven't seen this happen before. When a question starts out, "How can I do (A or B, maybe C, or perhaps Q)?", but later gets clarified to "Oh, I guess I really meant B, C, and D. Sorry!", anyone who answered questions A or Q has wasted their time. The risk of getting (rightly) downvoted later for blathering about Q when the question was _obviously_ asking BCD means they have to either delete their answer or waste _more_ time answering the question again. Also, unclear questions aren't helpful to future visitors. – Kevin J. Chase Apr 18 '17 at 18:17
  • 1
    @MartinMorales: You should link to and possibly quote some of "duffymo's answer", since I don't see it here. – Kevin J. Chase Apr 18 '17 at 18:18
  • 1
    @KevinJ.Chase : you do have a point there. I'll do my best to be as clear as possible from now on; but sadly the question's nature was so that I really didn't know what I wanted. Thankfully, things got straightened out eventually. Thank you for your input Kevin. – tudor balus Apr 19 '17 at 09:46