IN C++ For this assignment you are to write a number of C++ functions, and a tes
ID: 3677939 • Letter: I
Question
IN C++
For this assignment you are to write a number of C++ functions, and a test driver ( harness ) to test them with.
The Functions
For this assignment the functions you are to write are as follows:
double radians( double degrees );
double xCosXySinY( double x, double y );
int linspace( double data[ ], double dataMin, double delta, int nPoints );
o Fills data[ ] with nPoints values, starting at dataMin and increasing in steps of delta.
o Returns the number of array spots filled, ( normally nPoints ), or -1 in the case of errors ( bad input. )
int linspace( double data[ ], double dataMin, double delta, double dataMax );
o Fills data[ ] with values, ranging from dataMin to dataMax in steps of delta.
o Returns the number of array spots filled, ( calculated ), or -1 in the case of errors
( bad input. )
double distanceAndVector( double x1, double y1, double x2, double y2, double & deltaX,
double &deltaY );
o Returns the distance between ( x1, y1 ) and ( x2, y2 ).
o Stores in ( deltaX, deltaY ) the vector pointing from ( x1, y1 ) to ( x2, y2 ).
int xCosXySinY( int nRows, int nCols, double x[ ], double y[ ], double result[ ][ MAXCOLS ] );
Program Details
o Fills in result[ ][ ] by calling double xCosXySinY( double x, double y );
o The array passed to result must have MAXCOLS columns.
o nRows and nCols should be the number of rows and columns of result to fill.
They should be checked for validity.
o x[ ] holds the X values to be used for each row.
o y[ ] holds the Y values to be used for each column.
For this assignment you are to write a number of C++ functions, and a test driver ( harness ) to test them with.
Explanation / Answer
#include<iostream>
#include<cmath>
using namespace std;
double factorial(double n);
int main()
{
double sum;
double x;
double term;
int i,
j,
sign;
cout < < " C++ Program to Compute Cos x Using Taylor Series " << endl;
cout << " Enter the Value of X in Radians ";
cin >> x;
sum = 0;
i = 2;
j = 2;
sign = -1;
term = 1;
sum = 1;
do
{
term = pow(x, i) / (factorial(i));
sum = sum + term * sign;
i += 2;
sign = sign * (-1);
j++;
} while (term > 0.000001);
// NOTE : It Calculates Cos X Up to 35 Radians Correctly
// Because of Precision and Accuracy Error
cout < < " Approximate Value of Cos x = " << sum << endl;
cout << " Built in Cos(x) Function = " << cos(x) << endl;
cout << " Press enter to continue..." << endl;
cin.ignore();
cin.get();
return 0;
}
double factorial(double n)
{
double fact = 1;
for (int i = 1; i <= n; i++)
{
fact *= i;
}
return fact;
}