# Thread: Multiply 2 array's ?

1. ## Multiply 2 array's ?

Hello , where am i wrong with muliply 2 array's ?

Code:
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{

char egn[9];
int check[8] = {2,4,8,5,10,9,7,3,6};
int result[8];

scanf("%s",&egn);

result[0] = egn[0]*check[0];
printf("%d\n",result);

system("PAUSE");
return 0;
}

2. This is definitely not the way to do it. What are you trying to do anyway?

3. can you show me how can i multiply 2 array's :
arr[0]*arr2[0]

4. Originally Posted by Omar Do
arr[0]*arr2[0]
Here you are multiplying the 1st value of arr with the 1st value of arr2. What's the mathematical algorithm for what you want to do?

5. You are reading in a string and then using it to overwrite the address of your char array. Thankfully that address is local so you are really just detonating yourself in a confined environment. Only God knows what the result of that will be. My guess is a segmentation fault and a core dump.

6. There's more than one way to multiply two vectors and your code gives no hint as to which one you want. You're probably looking for either the "dot product" or the "cross product". The main difference between them is that the dot product produces a scalar result (a single number) whereas the cross product produces another vector (perpendicular to the two given vectors).

So which do you want?

EDIT: Actually, I suppose you could want to just multiply each element by the corresponding element in the other array (a kind of nonuniform scaling) but I don't know what that's called.

EDIT2: Now that I look into it, I'm not sure how to calculate the cross product of two nine-dimensional vectors! As I see it, it would probably take 8 of them for the concept to even be defined.

7. Think about these questions for a moment...

How many multiplications do you want to perform?
How many have you asked it to do?
How many items can an array of size 8 hold? (Hint, it's not 9)
How many results do you want to print out?
How many results are you trying to print currently?
Do you know how to use loops?

8. @oogabooga
-The cross product is only properly defined in 3 and 7 dimensional space, and you need 2 and 6 vectors respectively to calculate it.

9. Originally Posted by TheBigH
7 dimensional space
Wrong forum.

Quzah.

10. Originally Posted by quzah
Wrong forum.
Don't care.

11. Originally Posted by TheBigH
Don't care.
I guess it was a bit much to expect you to see the humor in that.

Quzah.

12. assuming the input for scanf is 01234567, you would want result[0] = 2 * 0? from the code you posted, you would get result[0] = 2 * 48, 48 being the ascii value for '0', so i think you want: result[0] = egn[0]*(check[0] - 48); if this is different from what you wanted please be a bit more specific and possibly provide a sample input for check[] and the ending values for result[] that you would want.

13. You are asking to multiply 2 arrays.

What comes to mind is this:
Code:
int array1[4] = {1,2,3,4}
int array2[4] = {5,6,7,8}

int result[4] = {5,12,21,32}
As for what your attached code is doing, it is multiplying user char values with int values. Generally speaking this is a horrible idea because what is A*6? In computer terms it is 260 (because of ASCII characters),

What about 1*4 when 1 is a char value? It is 196!

Edit: So how did Calculus 3 get involved in this again? :P