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

You are to write a C++ program using 2 user-defined functions to approximate the

ID: 3641309 • Letter: Y

Question

You are to write a C++ program using 2 user-defined functions to approximate the sine and cosine functions. You can start with this code for your program:

#include <iostream>
#include <cmath>

using namespace std;

double sine ( double x )
{
// set term to x
// set sum to x

    for ( i = 1; i <= 3; i++ ) {
//      multiply term by -x*x
//      divide term by (2*i)*(2*i+1)
//      add term to sum
    }

    return sum;
}

double cosine ( double x )
{
    ....
}


int main()
{
    double angle;

    cout << "Enter an angle measure (in radians): ";
    cin >> angle;

    cout << "cos(" << angle << ") = " << cos(angle) << endl;
    cout << "cosine(" << angle << ") = " << cosine(angle) << endl;

    cout << "sin(" << angle << ") = " << sin(angle) << endl;
     cout << "sine(" << angle << ") = " << sine(angle) << endl;

    return 0;
}


You need to write the internal code for the sine and cosine functions. The main program prompts for an angle measurement in radians which is an alternative to degrees. If you want to try 90 degrees enter 1.57. If you want to try 180 degrees enter 3.14. After reading the angle measurement the program prints the cosine of the angle using the cos function from the math library and then the value from your own cosine function. It next does the same thing for the sine function.

To compute the sine of an angle you need to use a return statement in your sine function to return a value computed as

                      3          5            7
                     x          x            x
    sin(x) = x - ---   + ------- - ---------
                     3*2      5*4*3*2     7*5*4*3*2

This can be done as a loop. Before the loop you need to set a sum variable equal to x and also set a term value equal to x. You then need to execute a loop incrementing i from 1 to 3. Inside the loop you need to multiply your current term value by -x2 and then divide the term by (2*i)*(2*i+1). After computing the new term value add term to sum. This is an efficient way to compute the values which maintains accuracy quite well.

                      2          4             6
                     x          x             x
    cos(x) = 1 -   ---   + -------   - -----------

                    2*1      4*3*2*1      6*5*4*3*2*1

Similarly to what was done with the sine function, you can use a sum variable and a term variable in a for loop in the cosine function. Before the loop set sum to 1 and set term to 1. Inside the loop multiply term by -x2 and then divide by (2*i)*(2*i-1). Then add term to sum.

In both the cosine and sine functions the approximation can be improved by executing the loop more times.

Explanation / Answer

Please rate...

#include <iostream>
#include <cmath>
#include<stdio.h>

using namespace std;

int fact(int a)
{
    if(a==1 || a==0)return 1;
    else return a*fact(a-1);
}
double power(double x,int a)
{
    int i;
    double m=1;
    for(i=1;i<=a;i++)
    {
        m=m*x;
    }
    return m;
}

double sine ( double x )
{
    int i,c=1;
    double sum=0;
    for ( i = 3; i <= 7; i+=2 )
    {
        if(c%2!=0)
        {
            sum=sum-(power(x,i)/fact(i));
            c++;
        }
        else
        {
            sum=sum+(power(x,i)/fact(i));
            c++;
        }
    }
    sum=x+sum;

    return sum;
}

double cosine ( double x )
{
    int i,c=1;
    double sum=0;
    for ( i = 2; i <= 6; i+=2 )
    {
        if(c%2!=0)
        {
            sum=sum-(power(x,i)/fact(i));
            c++;
        }
        else
        {
            sum=sum+(power(x,i)/fact(i));
            c++;
        }
    }
    sum=1+sum;
    return sum;
}

int main()
{
    double angle;

    cout << "Enter an angle measure (in radians): ";
    cin >> angle;

    cout << "cos(" << angle << ") = " << cos(angle) << endl;
    cout << "cosine(" << angle << ") = " << cosine(angle) << endl;

    cout << "sin(" << angle << ") = " << sin(angle) << endl;
     cout << "sine(" << angle << ") = " << sine(angle) << endl;

    return 0;
}

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