/* This code calculates the dot product and the cross product
of two 3-vectors.
Two functions are called - one returns the dotprod ie a number and the
second function returns the cross product ie a vector with three elements.
*/
#include <stdio.h>
#include <math.h>
#define N 3
double dotproduct( double X[], double Y[]);
void crossproduct( double Z[], double X[], double Y[] );
main()
{
double x[N],y[N],z[N], dotprod;
int i;
printf("enter each component of vectors x and y, separated by a space,
then hit return\n");
for(i = 0; i < N; i++)
{
if(scanf( "%lf %lf", &x[i], &y[i] != 2)
{
printf(" you must enter 2 numbers as the vector components\n");
exit(1);
}
/* now calculate the dot product of x and y */
dotprod = dotproduct( x, y);
/* now calculate the cross product of x and y */
crossproduct(z,x,y);
printf("\nthe dot product of the 2 vectors you entered is \n
%lf\n\n ", dotprod);
printf("\nthe vector cross product of the 2 vector you entered is \n
(%lf, %lf, %lf)\n\n ", z[0],z[1],z[2]);
}
double dotproduct( double X[], double Y[])
{
int i;
double sum = 0.0;
for( i = 0; i<N; i++){
sum += X[i]*Y[i];}
return sum;
}
void crossproduct( double Z[], double X[], double Y[])
{
Z[0] = X[1]*Y[2] - X[2]*Y[1];
Z[1] = -( X[0]*Y[2] - X[2]*Y[0]);
Z[2] = X[0]*Y[1] - X[1]*Y[0];
}