First lay out the program on paper first before you start so
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 sm(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 of your data...that\'s to help you verify that you got it right.) Remember that C++- trig functions work in radians, not degrees!. Your output file should look something like this: If you are familiar with matlab. here\'s a matlab script which will plot your sines.txt if you\'ve output at to the file properly. Name your program sine_cosine.cpp
Solution
#include <iostream>
#include <fstream>
#include <iomanip>
#include <math.h>
using namespace std;
int main(){
ofstream outFile(\"sines.txt\");
outFile << fixed << setprecision(4);
int i = 0;
for(double x = -3.1; x <= 3.1; x += 0.1){
double y = cos(x);
double z = sin(x);
double w = 0.4 * y + 0.8 * z;
outFile << i++ << \"\\t\" << x << \"\\t\" << y << \"\\t\" << z << \"\\t\" << w << \"\ \";
}
outFile.close();
}
