Calculate x from –PI to PI in steps of 0.1 (-3.1 to +3.1). (make an array or set
ID: 3672407 • Letter: C
Question
Calculate x from –PI to PI in steps of 0.1 (-3.1 to +3.1). (make an array or set of x's from -3.1 all the way to 3.1 ie. -3.1,-3.0,-2.9,...3.0,3.1) then,
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)
then Output results to a file "sines.txt", in 5 columns – the index, x, y, z and w (63 lines).
Your output file should look something like this:
0 -3.1000 -0.0416 -0.9991 -0.0033
1 -3.0000 -0.1411 -0.9900 -0.0115
2 -2.9000 -0.2392 -0.9710 -0.0201
3 -2.8000 -0.3350 -0.9422 -0.0294
4 -2.7000 -0.4274 -0.9041 -0.0398
5 -2.6000 -0.5155 -0.8569 -0.0515
6 -2.5000 -0.5985 -0.8011 -0.0648
7 -2.4000 -0.6755 -0.7374 -0.0799
. . .
Explanation / Answer
c++ code to get the below desired output:
#include <math.h>
#include <stdio.h>
int main()
{
FILE *fp = fopen("sines.txt","w");
int i=0;
float x=-3.1;
while(x<=3.1)
{
fprintf(fp,"%d %.4f %.4f %.4f %.4f ",i,x,cos(x),sin(x),0.4*cos(x)+0.8*sin(x));
i=i+1;
x=x+0.1;
}
return 0;
}
//output:sines.txt
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.2393 -0.1970
61 3.0000 -0.9900 0.1411 -0.2831
62 3.1000 -0.9991 0.0416 -0.3664
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.