Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

assuming writing in c and using include stdio and such In this exam you will wri

ID: 3574587 • Letter: A

Question

assuming writing in c and using include stdio and such

In this exam you will write code to: find the dot product between two vectors, and find the angle between two vectors. You will write out the following information to "out. txt" exactly as follows: The scalar dot product of the two vectors is = and the angle between the two vectors is = degrees. Additionally, only if the vector angle is 90 degree, include the output line: The two vectors are orthogonal. The vector information should be read from two files - data1.txt and data2.txt. You will write two functions:/*function prototype to calculate scalar dot product.*/double DotProduct(double x[], double y[], int size);/*function prototype to obtain vector angle.*/double theta (double x[], double y[], int size); Follow these detailed instructions: First, read this document in its entirety. Include the usual (detailed) comment block. Explain the program to the user by printing a description to the terminal window. For 10 points extra credit: make all the filenames user inputs, i.e. ask the user for the two input files and for the output file. Remember to incorporate error checks!! Here is some test data you might want to use to make sure that your code works. a [1, 2, 0], b [3, -2, 1] a [4, 3, -2], b [3, -4, 1] a [1, 0, 0], b [0, 1, 0]

Explanation / Answer

#include <stdio.h>
#include <math.h>

double DotProduct(double x[], double y[], int size)
{
double result = 0.0;
int i;
for ( i = 0; i < size; i++)
result += x[i]*y[i];
return result;
}

double theta(double x[], double y[], int size)
{
   double xmag,ymag;
   int i ;
   for ( i = 0; i < size; i++)
xmag += x[i]*x[i];

for ( i = 0; i < size; i++)
ymag += y[i]*y[i];
xmag=sqrt(xmag);
ymag=sqrt(ymag);

double dotpro=DotProduct(x,y,size);

double thetaangle=acos(dotpro/(xmag*ymag));

return thetaangle * (180.0 / M_PI);
}

int main(int argc, char const *argv[])
{
   double x[100];
   double y[100];

char input1[20],input2[20],output[20];
   printf("Enter input file 1 ");

   scanf("%s",input1);

   printf("Enter input file 2 ");
   scanf("%s",input2);

   printf("Enter output file ");
   scanf("%s",output);
  
   FILE *f1 = fopen(input1, "r");

FILE *f2= fopen(input2, "r");

int i=0;
double n;
while( fscanf(f1, "%lf,", &n) > 0 )
{
x[i++] = n;
}
i=0;
while(fscanf(f2, "%lf,", &n) > 0 )
{
y[i++] = n;
}
double dorp=DotProduct(x,y,i);
double angle=theta(x,y,i);

FILE *out = fopen(output, "w");

if(angle==90.000)
{
   fprintf(out, "The scaler dot product of two vectors is %lf and the angle between two vectors is %lf The Two vectors are orthogonal ", dorp,angle);
}
else
{
fprintf(out, "The scaler dot product of two vectors is %lf and the angle between two vectors is %lf ", dorp,angle);
   }
   return 0;
}

==================================================================================

Output.

akshay@akshay-Inspiron-3537:~/Chegg$ gcc vector.c -lm
akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
Enter input file 1
input1.txt
Enter input file 2
input2.txt
Enter output file
out.txt

=====================================================

input1.txt

1

0

0

=========================================================================

input2.txt

0

1

0

===============================================================================

out.txt

The scaler dot product of two vectors is 0.000000 and the angle between two vectors is 90.000000 The Two vectors are orthogonal