Vector products: Difference between revisions

(→‎{{header|Groovy}}: new solution)
Line 197:
a x (b x c) = (-267, 204, -3)
</pre>
=={{header|C|C}}==
<lang c>
#include<stdio.h>
 
typedef struct{
float i,j,k;
}Vector;
 
Vector a = {3, 4, 5},b = {4, 3, 5},c = {-5, -12, -13};
 
float dotProduct(Vector a, Vector b)
{
return a.i*b.i+a.j*b.j+a.k*b.k;
}
 
Vector crossProduct(Vector a,Vector b)
{
Vector c = {a.j*b.k - a.k*b.j, a.k*b.i - a.i*b.k, a.i*b.j - a.j*b.i};
return c;
}
 
float scalarTripleProduct(Vector a,Vector b,Vector c)
{
return dotProduct(a,crossProduct(b,c));
}
 
Vector vectorTripleProduct(Vector a,Vector b,Vector c)
{
return crossProduct(a,crossProduct(b,c));
}
 
void printVector(Vector a)
{
printf("( %f, %f, %f)",a.i,a.j,a.k);
}
 
int main()
{
printf("\n a = "); printVector(a);
printf("\n b = "); printVector(b);
printf("\n c = "); printVector(c);
printf("\n a . b = %f",dotProduct(a,b));
printf("\n a x b = "); printVector(crossProduct(a,b));
printf("\n a . (b x c) = %f",scalarTripleProduct(a,b,c));
printf("\n a x (b x c) = "); printVector(vectorTripleProduct(a,b,c));
return 0;
}
</lang>
 
Output:
<lang>
 
a = ( 3.000000, 4.000000, 5.000000)
b = ( 4.000000, 3.000000, 5.000000)
c = ( -5.000000, -12.000000, -13.000000)
a . b = 49.000000
a x b = ( 5.000000, 5.000000, -7.000000)
a . (b x c) = 6.000000
a x (b x c) = ( -267.000000, 204.000000, -3.000000)
</lang>
=={{header|C sharp|C#}}==
<lang csharp>using System;
Line 230 ⟶ 293:
6
-267;204;-3</lang>
 
=={{header|C++}}==
<lang cpp>#include <iostream>
503

edits