Write a system of functions along with a main program that accomplishes the same
ID: 3621649 • Letter: W
Question
Write a system of functions along with a main program that accomplishes the same tasks as (http://www.cramster.com/answers-oct-10/computer-science/write-simple-program-sine-number-measured-radians-exp_991654.aspx). . Except replace the input and output functions to read and write to a file. The name of the file containing the name of the data file is name.file.txt. The name of the data file is string.assignment.txt. Your program DOES NOT know the name of the text file. You must read it from the name.file.txt. After your read the single set of numbers (angle and delta), perform the sine and cosine calculations and append the answers to the same file holding angle and delta. Use the following functions1. Void function to read in the data (file read)
2. Void function to convert degrees to radians (there will be one call by value and one call by reference)
3. Void function to output the answers. (all values will be passed by reference, file append)
4. sine function that returns the sine
5. cosine function that returns the cosine
Use a global constant for p = 3.14159
Explanation / Answer
please rate - thanks
hope this is correct
#include <iostream>
#include <cmath>
#include<fstream>
using namespace std;
double fact(double);
double calcsine(double,double);
double coss(double,double);
void degtorad(double,double&);
void print(double&,double&,string&,char[]);
void in(double&,double&,char[]);
int main ()
{
double x,xr,sinx,cosx,accuracy;
char filename[30];
string messsin="sin",messcos="cos";
in(x,accuracy,filename);
sinx=calcsine(xr,accuracy);
cosx=coss(xr,accuracy);
print(x,sinx,messsin,filename);
print(x,cosx,messcos,filename);
return 0;
}
void in(double& x,double& accuracy,char filename[])
{ifstream input;
input.open("name.file.txt"); //open file
if(input.fail()) //is it ok?
{ cout<<"file name file did not open please check it ";
system("pause");
return;
}
input>>filename;
input.close();
input.clear();
input.open(filename); //open file
if(input.fail()) //is it ok?
{ cout<<"input file did not open please check it ";
system("pause");
return ;
}
input>>x;
input>>accuracy;
input.close();
input.clear();
}
void print(double& x,double& val,string& mess,char filename[])
{ofstream out;
out.open(filename,fstream::app); //open file
if(out.fail()) //is it ok?
{ cout<<"output file did not open please check it ";
system("pause");
return;
}
out<<mess<<"("<<x<<")="<<val<<endl;
out.close();
}
void degtorad(double x,double& xr)
{xr=x*(3.14159/180.);
}
double coss(double x,double accuracy)
{int flip,i;
double term,next;
term=1;
flip=-1;
for (i=2;;i+=2)
{next=(pow(x,i)/fact(i))*flip;
flip=-flip;
term+=next;
if(fabs(next)<=accuracy)
return term;
}
}
double calcsine(double x, double accuracy)
{double term,numerator=x,denominator=1,sign=1,sin=0;
int i=1;
sin=x;
do
{i+=2;
sign *= -1;
term=pow(x,i)/fact(i) * sign;
sin +=term;
}while(fabs(term)>=accuracy);
return sin;
}
double fact(double n)
{
int i;
double nf=1;
for(i=1;i<=n;i++)
nf*=i;
return nf;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.