Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Homework 2 We will be writing a program to work with Vectors from math, given magnitude and direction or an x and y coordinate, we

Homework 2

We will be writing a program to work with Vectors from math, given magnitude and direction or an x and y coordinate, we will create vectors, decompose vectors, and add vectors.

Details

The basis of this homework is a series of functions you design and create to take input from a file and execute a given command. Sample input will be provided below.

You are required to have at least 1 function for each of the following commands that adhere to the interface in the provided header file.

add The add command will be followed by a set of 4 integers. They are pairs of magnitudes and directions for two vectors. This command will compute the magnitudes and directions for resultant vector. Tabs separate the integers. The line will have the format:

add

It will first output each of the X and Y components of the input vectors and then output the resultant vector. See the output below.

vector The vector command will be followed by two integers representing the X and Y components of a vector. This command will print out the magnitude and angle for this vector. Tabs separate the integers. The line will have the format:

vector

decompose The decompose command is more or less the opposite command from the Vector command. This command will be followed by two integers that represent the magnitude and angle of a vector and this command will print the X and Y components for the given vector. Tabs separate the integers. The line will have the format:

decompose

Sample Input

Here is a sample of each command

add 34 73 56 256 decompose 78 0 vector 34 45

A file with this input will be provided, as well as others, will be provided below.

All of the values that represent angles are given in degrees between 0 and 359.

Sample Output

Here is the corresponding output for the given input from above

Command X-Part Y-Part Magnitude Direction ------------------------------------------------------ one 9.94064 32.51436 34 73 two -13.54763 -54.33656 56 256 add -3.60699 -21.82220 22.11829 260.61445 decompose 78.00000 0.00000 78 0 vector 34 45 56.40035 52.92693

The first line is a simple header file. You do not need to match the spacing on any of the lines. The second line is 54 minus signs (-). Yes you need to have 54. I suggest just copying and pasting it from the sample output files.

You can see that the first two lines in the output table are related to the add command, they are the given values and the decomposed X and Y components. Then the 3rd line is the result of the add command and both the component parts and the recomposed parts are displayed.

Note: be careful with the resultant direction. Arctan in most programming languages gives a very small range of answers. Note: most programming languages expect the input to the trig functions be in radians.

Implementation Ideas

I would recommend you match the data types that are shown in the function headers when you create local variables. If you need, and you will, to make floating point variables, use doubles, floats will not be big enough for the precision that is needed.

Hopefully you can see that if you write the vector and decompose functions the add function becomes a combination of using those functions to get the add working . Use the functions you are writing to do the work for you.

You should use the main that is provided. It will handle the input and output for you. Dont worry, youll get to write your own main soon enough.

In the math.h header file there are constants declared for pi, M_PI, is the one that use. You are welcome to use other constants for pi if you want. In order to use M_PI, we have to use a different standard for C, so we are using the gnu11 standard. You can see that in the compile command below.

use c to solve. Thank you.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions