Question 2 10 marks Ever wonder how the standand C math fune

Question 2 (10 marks) Ever wonder how the standand C math funetions make their ealeulations. This question lets you explore howo compute the cosine. The following infinite series aives the cosine value cos x = 1--+--+ 2 41 61 \"Equation Such series serves as a basis for ealeulating cos in a programme, Of course a program has to limit the number or terms for computing the value (it is roin the valie cil posible to epuie an )Develop woftware that meets the following requirements Prompts the user for the value of s and the number of terms in the series used to compute sos() o Check that the number of terms given by the user is positive. * computes the value of cos(x) using the above series and displays the results to the user Prompts the user to quit the program with the message \"Do you wish to quit (y/n)? \", and read in a character. Repeat the above steps if the user enters \'y\' and stop the program if the user answers \'n\' Keep prompting the user if an invalid answer is entered Print the message \"Program terminated\" when the program terminates Respect the following guidelines: DO NOT use arays or structures in your answer. DO NOT use standard math functions in your answer. . Have the main function call a separate function, getinput, to get input values from the user Have the main function call a separate function, coaine, to compute the value of cos() Given that the powers of x in the numerator and the factorial in the denominator will become very large in a very few terms, calculating the values of each term separately will quickly reach the limit of the computer Use a strategy where the value of a term is calculated from the previous term. If we number the terms using i starting at 0 (and the termto 1), then the termt,can be computed from the previous term tuz as follows: o o 2(2i-1) The above computation does not involve the same large numbers as the numerator/denominator of the terms in Equation 1 Recall that the modulo operator allows you to determine if the term i is odd or even. This can be used to determine if the term is to be added or subtracted. o The loop to control termination of the program is part of the main function. Recall that telush function should be called before reading in a character input from the user. o

Solution

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
main()
{
float x,s;
int i,j,f=0;
printf(\"Enter the real value of x \ \");
scanf(\"%f\", &x);
do{
f=0;
printf(\"How many terms to use\");
scanf(\"%d\",&i);
if (i<=0){
printf(\"The value must be greater than 0\");
f=1;
}
}while(f==1);   
//s=(x/pow(10,i));   
printf(\"Cos(%f)\",x*atan(1.0));
printf(\" is %f\ \", cos(x));
getch();
}

 Question 2 (10 marks) Ever wonder how the standand C math funetions make their ealeulations. This question lets you explore howo compute the cosine. The follow

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site