Question

The standard math library provides a Complex functions such as these are usually approximated using some numeric analy techni

This is the given code:

/**
* This program uses a Taylor Series to compute a value
* of sine.
*
*/
#include<stdlib.h>
#include<stdio.h>
#include<math.h>

/**
* A function to compute the factorial function, n!.
*/
long factorial(int n) {
long result = 1, i;
for(i=2; i<=n; i++) {
result *= i;
}
return result;
}

int main(int argc, char **argv) {

if(argc != 3) {
fprintf(stderr, "Usage: %s x n ", argv[0]);
exit(1);
}

double x = atof(argv[1]);
int n = atoi(argv[2]);

double result = 0.0;

//compute sin(x) using a taylor series out to n terms


printf("sin(%f) = %f ", x, result);

return 0;
}

How would you solve this?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

EXPECTED ANSWER

//Program

#include<math.h>
#include<stdio.h>
int main()
{
double sum=0.00,num,s;
long x,i,n,den,f;
printf(" Enter the value of x: "); //sin(4)
scanf("%ld",&x);
printf(" Enter the value of n: ");
scanf("%ld",&n);
for(i=0;i<=n;i++)
   {
   den=2*i+1;
   num=pow(x,den);
   f=1;
   while(den!=0)
       {
       f=f*den;den--;
       }
   sum=sum+(num*pow(-1,i))/f;
   }
printf(" The taylor series approximation of sin(%ld) is : %lf",x,sum);
s=sin(x);
printf(" The Math library function aroximation of sin(%ld) is : %lf",x,s);
printf(" The error os %lf",sum-s);
return 0;
}

sample run

囲DOSBox 0.74, Cpu speed: max 100% cycles, Frameskip 0, Program: TC CNTURBOC3 BIN>exit mter the value of x: 4 Enter the valueTHANK YOU ....!

Add a comment
Know the answer?
Add Answer to:
This is the given code: /** * This program uses a Taylor Series to compute a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • so in this code, it computes the sum 1+2+....+n but i want it to compute 2*(1+2+....+n)...

    so in this code, it computes the sum 1+2+....+n but i want it to compute 2*(1+2+....+n) using semaphores implement solution to the critical section problem #include #include int sum; /* this data is shared by the thread(s) */ void *runner(void *param); /* threads call this function */ int main(int argc, char *argv[]) { pthread_t tid; /* the thread identifier */ pthread_attr_t attr; /* set of thread attributes */ if (argc != 2) { fprintf(stderr,"usage: a.out \n"); return -1; } if...

  • Would u help me fixing this CODE With Debugging Code with GDB #include <stdio.h> #include <stdlib.h>...

    Would u help me fixing this CODE With Debugging Code with GDB #include <stdio.h> #include <stdlib.h> #define SIZE (10) typedef struct _debugLab { int i; char c; } debugLab; // Prototypes void PrintUsage(char *); void DebugOption1(void); void DebugOption2(void); int main(int argc, char **argv) { int option = 0; if (argc == 1) { PrintUsage(argv[0]); exit(0); } option = atoi(argv[1]); if (option == 1) { DebugOption1(); } else if (option == 2) { DebugOption2(); } else { PrintUsage(argv[0]); exit(0); } }...

  • In C using the following 2 files to create a 3rd file that uses multiple threads...

    In C using the following 2 files to create a 3rd file that uses multiple threads to improve performance. Split the array into pieces and each piece is handled by a different thread. Use 8 threads. run and compile in linux. #include <stdio.h> #include <sys/time.h> #define BUFFER_SIZE 4000000 int countPrime=0; int numbers[BUFFER_SIZE]; int isPrime(int n) { int i; for(i=2;i<n;i++) if (n%i==0) return 0; return 1; } int main() { int i; // fill the buffer for(i=0;i<BUFFER_SIZE;i++) numbers[i] = (i+100)%100000; //...

  • Read given code RaceOrNot3.c and write all possible outputs of the program. Assume there will be...

    Read given code RaceOrNot3.c and write all possible outputs of the program. Assume there will be no thread creation or joining failures or semaphore failures. If you believe there is only one possible output, you just need to write that output. #include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <semaphore.h> #include <errno.h> sem_t s1; int c=0,x=0; void *UpdateC1(void *arg) {    int i;    for(i=0;i<2000000;i++)   {        sem_wait(&s1);        c++;   x++;        sem_post(&s1);    } } void *UpdateC2(void *arg) {    int i,x=0;    for(i=0;i<2999999;i++)   {        sem_wait(&s1);        c++;   x++;       ...

  • Assume I don't understand C++ Can someone explain this program to me Line by Line? Basically...

    Assume I don't understand C++ Can someone explain this program to me Line by Line? Basically what each line actually does? whats the function? whats the point? Don't tell me what the program does as a whole, I need to understand what each line does in this program. #include #include #include #include #include #define SERVER_PORT 5432 #define MAX_LINE 256 int main(int argc, char * argv[]) {    FILE *fp;    struct hostent *hp;    struct sockaddr_in sin;    char *host;...

  • ANSWER ASAP PLEASE IN C Duplicate the program below to a new Program 2, and modify...

    ANSWER ASAP PLEASE IN C Duplicate the program below to a new Program 2, and modify Program 2 such that it generates n random virtual addresses between 0 and 232-1 and computes the page number and offset for each address. Here, do not write to the console. Instead, run your program with n = 1000000 random virtual addresses and compute the total CPU time of the task. Report your findings, as well as how you ran your program. Provide commentary...

  • Program already solved, but I need to put function documentation headers for each and every function...

    Program already solved, but I need to put function documentation headers for each and every function in your program (for the ones you write, and keep the function header I give you for the main() function). Also make sure you keep the file block header at the top of the file, and fill in the information correctly. Secondly this week you must get your indentation correct. All indentation must use 2 spaces, and you should not have embedded tabs in...

  • C++ code for CIS054 Create a program that uses a function to determine the length of...

    C++ code for CIS054 Create a program that uses a function to determine the length of a line by inputting the X,Y coordinates of the line endpoints. Show the result with a precision of four decimal places. The function prototype is to be specified as follows:     double LengthOfLine (double X1, double Y1, double X2, double Y2); // returns length of line by using this code: #include <iostream> #include <iomanip> #include <cmath> using namespace std; int main(int argc, char* argv[])...

  • Read given code and write all possible outputs of the program. Assume there will be no...

    Read given code and write all possible outputs of the program. Assume there will be no thread creation or joining failures or mutex failures. If you believe there i only one possible output, you just need to write that output. Code: #include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <semaphore.h> #include <errno.h> sem_t s1; int c[2] = {0,1}; void *UpdateC1(void *arg) { int i; for(i=0;i<1000000;i++) { sem_wait(&s1); c[0]=(c[0]+1)%2; c[1]=(c[1]+1)%2; sem_post(&s1); } return NULL; } void *UpdateC2(void *arg) { int i; for(i=0;i<2000000;i++)...

  • Write a C++ program In this assignment you will complete the definition of two functions that...

    Write a C++ program In this assignment you will complete the definition of two functions that implement the repeated squaring algorithm described in the Stamp textbook on pages 98-99. Note that your implementation should not use the pow() function, the powl() functions, or any other built-in exponentiation functions. program4-driver.cpp - This file contains a completed main() function that serves as the driver to parse the command line, pass the values to the powerModN() function, and print the result. Make no...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT