/* an example of code that calculates the dot product and the cross product
of two 3-vectors. The use of functions was not required for this assignment.
*/
#include <stdio.h>
#include <math.h>
#define N 3
main()
{
double x[N],y[N],z[N], dotprod;
int i;
dotprod = 0.0;
/* I've used two slightly different pieces of code to read in the vector
components of x and y. The end result is the same. The latter requires
less work if the value of N changes.
*/
printf("enter the 3 components of the 1st vector, separated by a space\n");
if(scanf( "%lf %lf %lf", &x[0], &x[1], &x[2]) != 3)
{
printf(" you must enter a number as the vector component\n");
exit(1);
}
printf("enter each component of the 2nd vector and hit return\n");
for (i = 0; i<N; i++)
{
if(scanf( "%lf", &y[i]) != 1)
{
printf(" you must enter a number as the vector component\n");
exit(1);
}
}
/* now calculate the dot product of x and y */
for( i = 0; i<N; i++)
{
dotprod += x[i]*y[i];
}
/* now calculate the cross product of x and 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];
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]);
}