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

C++ program on visual studio. Part III. Columns of related data. First: lay out

ID: 3798209 • Letter: C

Question

C++ program on visual studio.

Part III. Columns of related data. First: lay out the program on paper first before you start, so you're clear of the steps it needs to go through. The fourth sample program above should help you with this part of the lab Like the sample program, calculate a range of x-values, then calculate a set of y, z and w values based on the x values. Calculate x from -PI to PI in steps of 0.1 (-3.1 to +3.1) calculate y cos(x) for each value of x, calculate z sin(x) for each value of x. calculate w .4 cos(x) .8 sin(x) Output results to a file "sines.txt", in 5 columns the index, x, y, z and w (63 lines). You can verify your results fairly easily by hand-checking a few of the values With a calculator (be sure to check beginning and end of your data), then plot the entire sequence with excel or matlab to see if looks correct. (You do not need to turn in a plot ofyour data... that's to help you verify that you got it right.) Remember that C++ trig functions

Explanation / Answer

PROGRAM CODE:

#include <iostream>

#include <cmath>

#include <ctgmath>

#include <fstream>

#include <iomanip>

#include <cstdlib>

using namespace std;

struct plots

{

   double x;

   double y;

   double z;

   double w;

};

int main()

{

   ofstream out("sines.txt");

   //struct to hold all the values from -3.1 to 3.1

   struct plots *plot[63];

   //counter for the struct array

   int counter = 0;

   for(double i=-3.1; i<=3.1; i=i+0.1)

   {

       //calculating the values for x, y, z, w

       plot[counter] = (struct plots*)malloc(sizeof(struct plots));

       plot[counter]->x = i;

       plot[counter]->y = cos(i);

       plot[counter]->z = sin(i);

       plot[counter]->w = 0.4*plot[counter]->y + 0.8*plot[counter]->z;

       counter++;

   }

   //writing to file

   for(int i=0; i<counter; i++)

   {

       out<<fixed<<setprecision(4);

       out<<i<<" "<<plot[i]->x<<" "<<plot[i]->y<<" "<<plot[i]->z<<" "<<plot[i]->w<<endl;

   }

   out.close();

   return 0;

}

OUTPUT: (file)

0 -3.1000 -0.9991 -0.0416 -0.4329

1 -3.0000 -0.9900 -0.1411 -0.5089

2 -2.9000 -0.9710 -0.2392 -0.5798

3 -2.8000 -0.9422 -0.3350 -0.6449

4 -2.7000 -0.9041 -0.4274 -0.7035

5 -2.6000 -0.8569 -0.5155 -0.7552

6 -2.5000 -0.8011 -0.5985 -0.7992

7 -2.4000 -0.7374 -0.6755 -0.8353

8 -2.3000 -0.6663 -0.7457 -0.8631

9 -2.2000 -0.5885 -0.8085 -0.8822

10 -2.1000 -0.5048 -0.8632 -0.8925

11 -2.0000 -0.4161 -0.9093 -0.8939

12 -1.9000 -0.3233 -0.9463 -0.8864

13 -1.8000 -0.2272 -0.9738 -0.8700

14 -1.7000 -0.1288 -0.9917 -0.8449

15 -1.6000 -0.0292 -0.9996 -0.8113

16 -1.5000 0.0707 -0.9975 -0.7697

17 -1.4000 0.1700 -0.9854 -0.7204

18 -1.3000 0.2675 -0.9636 -0.6638

19 -1.2000 0.3624 -0.9320 -0.6007

20 -1.1000 0.4536 -0.8912 -0.5315

21 -1.0000 0.5403 -0.8415 -0.4571

22 -0.9000 0.6216 -0.7833 -0.3780

23 -0.8000 0.6967 -0.7174 -0.2952

24 -0.7000 0.7648 -0.6442 -0.2094

25 -0.6000 0.8253 -0.5646 -0.1216

26 -0.5000 0.8776 -0.4794 -0.0325

27 -0.4000 0.9211 -0.3894 0.0569

28 -0.3000 0.9553 -0.2955 0.1457

29 -0.2000 0.9801 -0.1987 0.2331

30 -0.1000 0.9950 -0.0998 0.3181

31 0.0000 1.0000 0.0000 0.4000

32 0.1000 0.9950 0.0998 0.4779

33 0.2000 0.9801 0.1987 0.5510

34 0.3000 0.9553 0.2955 0.6186

35 0.4000 0.9211 0.3894 0.6800

36 0.5000 0.8776 0.4794 0.7346

37 0.6000 0.8253 0.5646 0.7818

38 0.7000 0.7648 0.6442 0.8213

39 0.8000 0.6967 0.7174 0.8526

40 0.9000 0.6216 0.7833 0.8753

41 1.0000 0.5403 0.8415 0.8893

42 1.1000 0.4536 0.8912 0.8944

43 1.2000 0.3624 0.9320 0.8906

44 1.3000 0.2675 0.9636 0.8778

45 1.4000 0.1700 0.9854 0.8563

46 1.5000 0.0707 0.9975 0.8263

47 1.6000 -0.0292 0.9996 0.7880

48 1.7000 -0.1288 0.9917 0.7418

49 1.8000 -0.2272 0.9738 0.6882

50 1.9000 -0.3233 0.9463 0.6277

51 2.0000 -0.4161 0.9093 0.5610

52 2.1000 -0.5048 0.8632 0.4886

53 2.2000 -0.5885 0.8085 0.4114

54 2.3000 -0.6663 0.7457 0.3301

55 2.4000 -0.7374 0.6755 0.2454

56 2.5000 -0.8011 0.5985 0.1583

57 2.6000 -0.8569 0.5155 0.0696

58 2.7000 -0.9041 0.4274 -0.0197

59 2.8000 -0.9422 0.3350 -0.1089

60 2.9000 -0.9710 0.2392 -0.1970

61 3.0000 -0.9900 0.1411 -0.2831

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote