Write a code in for vector math by breaking down direction and magnitude into tw
ID: 3624368 • Letter: W
Question
Write a code in for vector math by breaking down direction and magnitude into two parts, the answer will be the X and Y lengths that make up the two "sides" of the vector use the trig functions from the cmath library and write and use your own functionthe program will input a text file:
295 69
231 66
26 40
180 75
270 45
53 66
323 57
205 3
145 68
360 86
279 90
155 75
and output
Magnitude Angle X Y
69 295 29.16066 -62.53524
66 231 -41.53515 -51.29163
40 26 35.95176 17.53485
75 180 -75.00000 0.00000
45 270 -0.00000 -45.00000
66 53 39.71979 52.70994
57 323 45.52222 -34.30346
3 205 -2.71892 -1.26785
68 145 -55.70234 39.00320
86 360 86.00000 -0.00000
90 279 14.07910 -88.89195
75 155 -67.97308 31.69637
13 180 -13.00000 0.00000
40 356 39.90256 -2.79026
6 0 6.00000 0.00000
27 360 27.00000 -0.00000
3 90 0.00000 3.00000
46 173 -45.65712 5.60599
54 19 51.05800 17.58068
75 102 -15.59338 73.36107
16 0 16.00000 0.00000
95 90 0.00000 95.00000
32 180 -32.00000 0.00000
49 270 -0.00000 -49.00000
21 360 21.00000 -0.00000
Explanation / Answer
I will assume some basic familiarity with c++ programming and start with the basic structure of a program. >>>> //includes #include using namespace std; int main() { // code goes here ... return 0; } >>>> We have been asked to do trigonometry inside the program, so we are going to need a math library. #include Just the math part of the code (this belongs inside main). >>>> // I have used floats for all of the values to make sure I can get fractional answers float dir = 0; // this is ? float mag = 0; // this is r float rads = 0; // this is an extra variable for later float x = 0; float y = 0; rads = dir * 2*3.14159/360; // the math functions cos and sin take radians and not degrees // do the math x = mag * cos(rads); y = mag * sin(rads); >>>> Note: rads = degrees * 2p/360 is a conversion formula from degrees to radians. Now that we have coded the math, it is important to do the input/output. For this, we need to include stdio.h, the standard i/o library for c++. You can search all of the necessary functions on www.cplusplus.com. First you need an input file and an output file. FILE* infile = fopen("input.txt", "r"); FILE* outfile = fopen("output.txt", "w"); In order to not try to read past the end of the file, we need a loop that gets the data from the file while checking for the end. while( !feof(infile)) { ... } To read from the file, use the fscanf function: fscanf(infile, "%f", &dir); // this line will read the next piece of data from the file as a float and put it in dir To write to a file, use the fprintf function: fprintf(outfile, "%f ", dir); // this will output dir to the file as a float If you want to format your output you can add numbers in the form x.x in the %f like so: fprintf(outfile, "%.0f ", dir); // this will print dir with no decimal places This is all in the fprintf documentation. Remember to close your files when you are done with them: fclose(infile); fclose(outfile); NOTE: I have left putting all of the pieces together in the right order to you but all of the necessary functions have been given.Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.