cmatrixvectorcross-product

# How to write a function to do cross product of two vectors in C

I'm writing a function in C that takes two vectors, v1 and v3, and performs a vector times vector multiplication to create a matrix. v1 is a 1xL1 vector and v3 is a 1xL2 vector. The method takes the transpose of v3 times v1 and prints the matrix. This is what I have so far:

`````` void crossProduct(float *v1, float *v3, int L1, int L2){
int i, j;
float sum;
float c[L2][L1];
for(i = 0; i < L2; i++){
for(j = 0; j < L1; j++){
sum = 0.0;
sum += v3[i] * v1[j];
c[i][j] = sum;
printf("%d", c[i][j]);
}
}
}
``````

I'm not getting the expected outcome. I also want to matrix to print in matrix form but I'm not sure how.

Solution

• You need to use `%f` format for floats, not `%d`.

To get a readable array, put a space after each value, and a newline after each row.

`````` void crossProduct(float *v1, float *v3, int L1, int L2){
int i, j;
float sum;
float c[L2][L1];
for(i = 0; i < L2; i++){
for(j = 0; j < L1; j++){
sum = 0.0;
sum += v3[i] * v1[j];
c[i][j] = sum;
printf("%f ", c[i][j]);
}
printf("\n");
}
}
``````