Organize the three component vector into a struct Contains t

Organize the three component vector into a struct Contains three doubles, x, y and z. Define the struct at the top of the file. #include at the top of your file, you will need it. typedef struct double x; double y, double z; Vector Create the following functions AddVector INPUT: Two vector structs OUTPUT: One vector struct that is the two input vectors added -new,Vector,x = V1.x + V2.x Repeat for y and z SubtractVector INPUT: Two vector structs OUTPUT: One vector struct that is the two input vectors subtracted -new-Vector.x= V1.x-V2.x Repeat for y and z PrintVector INPUT: One vector struct OUTPUT: void Prints each component from the input vector: (x, y, z) Within your main function Create four instances of the vector struct, two for input, two to hold the output from the functions you created Prompt the user to enter three doubles. Save these values into x, y and z of the first input vector you created Repeat for the second input vector -You may scanf s into normal variables then assign them to the struct manually. You may also scanf s directly into the struct. Call AddVector with the two input vectors. Save the results into the third vector instance that you created. Call SubtractVector with the first and second vector. Save the results into the fourth instance you created. Call PrintVector to print the result from AddVector .Call PrintVector to print the result from SubtractVector PLEASE manage to define ALL of the functions with const and pass by reference.

Solution

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

typedef struct
{

double x;

double y;

double z;  
}Vector;

Vector AddVector(Vector v1,Vector v2)
{
   Vector newVector;
   newVector.x=v1.x+v2.x;
   newVector.y=v1.y+v2.y;
   newVector.z=v1.z+v2.z;
   return newVector;
}

Vector SubtractVector(Vector v1,Vector v2)
{
   Vector newVector;
   newVector.x=v1.x-v2.x;
   newVector.y=v1.y-v2.y;
   newVector.z=v1.z-v2.z;
   return newVector;
}

void PrintVector(Vector v)
{
   printf(\"(%lf,%lf,%lf)\",v.x,v.y,v.z);
}
int main()
{

Vector v1,v2,v3,v4;

   printf(\"Enter Value of vector 1 X\ \");
   scanf(\"%lf\",&v1.x);

   printf(\"Enter Value of vector 1 Y\ \");
   scanf(\"%lf\",&v1.y);

   printf(\"Enter Value of vector 1 Z\ \");
   scanf(\"%lf\",&v1.z);

   printf(\"Enter Value of vector 2 X\ \");
   scanf(\"%lf\",&v2.x);

   printf(\"Enter Value of vector 2 Y\ \");
   scanf(\"%lf\",&v2.y);

   printf(\"Enter Value of vector 2 Z\ \");
   scanf(\"%lf\",&v2.z);

   v3=AddVector(v1,v2);
   printf(\"Add Result \");
   PrintVector(v3);

   v4=SubtractVector(v1,v2);
   printf(\"\ Subtraction Result \");
   PrintVector(v4);
}

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

akshay-Inspiron-3537:~/Chegg$ gcc vec.c
akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
Enter Value of vector 1 X
1
Enter Value of vector 1 Y
2
Enter Value of vector 1 Z
3
Enter Value of vector 2 X
1
Enter Value of vector 2 Y
2
Enter Value of vector 2 Z
3
Add Result (2.000000,4.000000,6.000000)
Subtraction Result (0.000000,0.000000,0.000000)

 Organize the three component vector into a struct Contains three doubles, x, y and z. Define the struct at the top of the file. #include at the top of your fil
 Organize the three component vector into a struct Contains three doubles, x, y and z. Define the struct at the top of the file. #include at the top of your fil

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site