Page 1 of 1

Reputation:

# C Program to evaluate y=exp(x); beginner, help please!

Posted 07 February 2011 - 06:24 PM

Hi! I have written a part of the program but don't know if I'm going in the right direction. I was also wondering how to use getchar() because I've never used that before. And I know I'm supposed to use switch but I'm not sure how to use it in conjuction with getchar()?
I'm quite a novice at this so any help would be greatly appreciated. You can view what I've written so far at the bottom of this post. Thanks!

Scan a double variable x and evaluate y=exp(x) by
using the math.h library of functions.

Your objective in this homework is to compare the so calculated
value of y with the approximate value Y obtained by
using 1, 2, 3, 4, and 5 leading terms of the Taylor series:

exp(x) = x^0/0! + x^1/1! + x^2/2! + x^3/3! + x^4/4! + ....

Recall that k!=k*(k-1)*(k-2)* ... *3*2*1 and 0!=1.

Use a do/while loop in conjunction with a switch statement
switch(). Enter a character 1 for one term approximation,
2 for two terms, etc., by using getchar() function. Use
0 to exit the program. Within switch, use default:
if (char != '0') => unrecognized operator.

Let your opening case calculate the fourth term x^4/4!, let the
subsequent case calculate the x^3/3! term, etc. (In this way,
you wan't need to use repeatedly a break statement within your switch).

Evaluate the corresponding Y, print the result in one line,
and the relative error (Y-y)/y in another line.

Execute your program for all five cases (with corresponding
printouts).

............................................
Your output should be like this:

Enter x: 1.25

True value of exp(1.2500) = 3.490343

Enter a character 1-5 (0 to exit):
1
1 term(s) approximation
Approximate exp(1.2500) = 1.000000
Relative error = -71.349520 percent

Enter a character 1-5 (0 to exit):
2
2 term(s) approximation
Approximate exp(1.2500) = 2.250000
Relative error = -35.536421 percent

Enter a character 1-5 (0 to exit):
3
3 term(s) approximation
Approximate exp(1.2500) = 3.031250
Relative error = -13.153233 percent

Enter a character 1-5 (0 to exit):
4
4 term(s) approximation
Approximate exp(1.2500) = 3.356771
Relative error = -3.826905 percent

Enter a character 1-5 (0 to exit):
5
5 term(s) approximation
Approximate exp(1.2500) = 3.458496
Relative error = -0.912428 percent

Enter a character 1-5 (0 to exit):
6
unrecognized operator

Enter a character 1-5 (0 to exit):
0

Code:

```#include<stdio.h>
#include<math.h>

/* Scanning a double variable x and evaluating y=exp(x) by
using the math.h library of functions */

int main() {

/* Defining variables */
int k, N;
char ch;
double x, y, Y;

/* Two empty lines and data input + true value of exp(x) at beginning */
printf("\n\nEnter x:\n\n");
scanf("%lf", &x);

y=exp(x);
printf("True value of exp(%lf) = %9.6f\n", x, y);

/* Data input: do/while statement with a switch statement within */
do {
printf("Enter x:\n");
scanf("%d", &N);
y=0;

switch(N) {
case 5;
y-=

}
}

}
```

Is This A Good Question/Topic? 0

## Replies To: C Program to evaluate y=exp(x); beginner, help please!

### #2 CTphpnwb

• D.I.C Lover

Reputation: 3795
• Posts: 13,738
• Joined: 08-August 08

## Re: C Program to evaluate y=exp(x); beginner, help please!

Posted 07 February 2011 - 06:55 PM

Write and use these two functions:

```long factorial(long N)
{

}

double Approximate_exp( double X, int Terms )
{

}
```

This post has been edited by CTphpnwb: 07 February 2011 - 06:59 PM

Was This Post Helpful? 0

### #3 David W

• DIC supporter

Reputation: 298
• Posts: 1,839
• Joined: 20-September 08

## Re: C Program to evaluate y=exp(x); beginner, help please!

Posted 07 February 2011 - 08:28 PM

Using an iterative method, where one calculates the next term (with a rapidly decreasing value) from the previous term, is usually the way to go with problems like this ...
Take a look at the examples several posts into this link ...

http://www.dreaminco...or-series-help/
Was This Post Helpful? 0

### #4 Salem_c

• void main'ers are DOOMED

Reputation: 2160
• Posts: 4,225
• Joined: 30-May 10

## Re: C Program to evaluate y=exp(x); beginner, help please!

Posted 08 February 2011 - 02:18 AM

> Using an iterative method
Like this?
```const char *urls[] = {
"http://www.dreamincode.net/forums/topic/214655-c-program-to-evaluate-yexpx%3B-beginner-help-please/",
"http://cboard.cprogramming.com/c-programming/134486-c-program-evaluate-y%3Dexp-x-%3B-novice-help-please.html"
};
for ( spam = 0 ; spam < numForums ; spam++ ) {
postMessageOn(urls[spam]);
}

```

Was This Post Helpful? 1

### #5 David W

• DIC supporter

Reputation: 298
• Posts: 1,839
• Joined: 20-September 08

## Re: C Program to evaluate y=exp(x); beginner, help please!

Posted 08 February 2011 - 12:18 PM

So it seems the OP posted his question on a 2nd forum ...
I don't check other forums regularly ... (but I just did now to see the 2nd link posted above.)
I consider DIC 'home' and ...
so I like to be hospitable to all who come here, as I am enabled.
I recall how overwhelming it seemed some of my first days attempting programming.
I like to pattern my responses to requests for help with computer programming after Randy Hyde ...
( known for his gifted teaching and developing High Level Assembly )
I especially like to recall the words of our Creator and giver of every breath, Yeshua (Jesus), that even giving a glass of water in His Name is not without it's eternal reward.
So while we still have breath, let's be gracious and encourage each other.
Shalom shalom,
David
Was This Post Helpful? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }