c program exponential functions; function prototypes

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 19769 Views - Last Post: 06 April 2011 - 07:41 PM Rate Topic: -----

#1 iqbalmmz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 92
  • Joined: 15-February 11

c program exponential functions; function prototypes

Posted 04 April 2011 - 01:26 PM

hi guys
i need help with this program. it's very straightforward but I can't seem to get it working. i'm probably making a silly mistake or something. any help/advise/suggestions would be much appreciated, thanks.

Quote

The C library function that computes the exponential function, ex, has the following prototype:

double exp( double x);

For this exercise, you are to write your own function to compute the exponential. The prototype should be

double myexp(double x);

Implement the algorithm given by the pseudocode above. Do not use the pow function from the math library and do not use a factorial function in your implementation of myexp. Use a defined constant for EPSILON. Make it 0.000001. Be sure that your function handles negative as well as positive values for x. (You can see how many terms your function adds to approximate ex by printing the value of n just before returning from the function.)

In main, provide code that tests your function. It should prompt the user to enter a real number, and then call both your function and the library function to compute the exponential. The value returned by your function and the value returned by the library function are both estimates of the actual value. Neither, in general, is exact. Of course the accuracy of your function can be improved by using a smaller value for EPSILON. A sample run might look as follows:

Enter x: 2.0
exp(x) = 7.389056
myexp(x) = 7.389056


Pseudocode for exp function

Quote

Here is a pseudocode representation of an algorithm to compute exp(x) using the above series expansion.
1. Let variable term represent a term in the exponential series above.
2. Let variable sum represent an approximation to ex.
3. Let n represent a counter.
4. Let EPSILON denote some small constant (e.g., 0.000001).
5. Set term = 1.0, sum = 0.0, and n = 1.
Loop.
6. sum = sum + term
7. term = term * x / n
8. n = n + 1;
9. If | term | < EPSILON then exit loop (sum ~= ex),
else repeat loop (i.e., go to 6)


here is what I currently have:
#include <stdio.h>
#include <stdlib.h>
#define EPSILON 0.000001

double myexp(double x);
int main ()
{
	double x;
	printf("Enter x: ");
	scanf("%d", &x);
	printf("exp(%d) =  ", x, myexp(x));
	
	return 0;
	
}

double myexp(double x)
{
	double x;
	double term = 1.0; //represents a term in exponential series
	double sum = 0; //represents an approximation to e^x
	int n = 1; //counter LCV
	
	while (n <= x) {
		sum += term;
		term = term * x / n;
		n += 1;
		if (term < EPSILON)
			break;
		else
			sum += term;
	}
	return; //return value
}


This post has been edited by iqbalmmz: 04 April 2011 - 01:27 PM


Is This A Good Question/Topic? 0
  • +

Replies To: c program exponential functions; function prototypes

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,659
  • Joined: 23-August 08

Re: c program exponential functions; function prototypes

Posted 04 April 2011 - 01:33 PM

You need to tell us WHAT THE PROBLEM IS!!! You have 60 posts, you haven't figured out yet how to ask a question???

"It doesn't work", That's Not Good Enough!
Was This Post Helpful? 0
  • +
  • -

#3 iqbalmmz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 92
  • Joined: 15-February 11

Re: c program exponential functions; function prototypes

Posted 04 April 2011 - 01:41 PM

View PostJackOfAllTrades, on 04 April 2011 - 12:33 PM, said:

You need to tell us WHAT THE PROBLEM IS!!! You have 60 posts, you haven't figured out yet how to ask a question???

"It doesn't work", That's Not Good Enough!


lol, what is this jeopardy?? my code doesn't compile, I think I may have interpreted the pseudocode wrongly or I have some sort of logic error. Can someone check my code to see what I am doing wrong?
Was This Post Helpful? -1
  • +
  • -

#4 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1623
  • View blog
  • Posts: 5,710
  • Joined: 03-August 09

Re: c program exponential functions; function prototypes

Posted 04 April 2011 - 01:50 PM

just saying it's not working tells us nothing. how do we know that it's a compile time issue unless you tells us? if you expect us to look that closely at your code and compile it every time you have an issue then you expect too much.

in this case you should tell us that the compiler is issuing errors then show us those errors and what line the occur on.
Was This Post Helpful? 1
  • +
  • -

#5 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: c program exponential functions; function prototypes

Posted 04 April 2011 - 01:50 PM

A logic error doesn't stop it from compiling. The compiler tells you about any syntax errors:

iq.c: In function ‘main’:
iq.c:10: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘double *’
iq.c:11: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’
iq.c:11: warning: too many arguments for format
iq.c: In function ‘myexp’:
iq.c:19: error: ‘x’ redeclared as different kind of symbol
iq.c:17: note: previous definition of ‘x’ was here
iq.c:33: warning: ‘return’ with no value, in function returning non-void



So which of those warnings/errors do you need help with?
Was This Post Helpful? 1
  • +
  • -

#6 ap0c0lyps3  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 19-June 07

Re: c program exponential functions; function prototypes

Posted 04 April 2011 - 01:50 PM

Your function doesn't actually return any value even though it's format is a double.
Just change "return" to "return sum;" and it should work.
Was This Post Helpful? 0
  • +
  • -

#7 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: c program exponential functions; function prototypes

Posted 04 April 2011 - 01:55 PM

View Postap0c0lyps3, on 04 April 2011 - 03:50 PM, said:

Just change "return" to "return sum;" and it should work.

Really? You see no other problems?
Was This Post Helpful? 1
  • +
  • -

#8 iqbalmmz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 92
  • Joined: 15-February 11

Re: c program exponential functions; function prototypes

Posted 04 April 2011 - 01:56 PM

View Postr.stiltskin, on 04 April 2011 - 12:50 PM, said:

A logic error doesn't stop it from compiling. The compiler tells you about any syntax errors:

iq.c: In function ‘main’:
iq.c:10: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘double *’
iq.c:11: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’
iq.c:11: warning: too many arguments for format
iq.c: In function ‘myexp’:
iq.c:19: error: ‘x’ redeclared as different kind of symbol
iq.c:17: note: previous definition of ‘x’ was here
iq.c:33: warning: ‘return’ with no value, in function returning non-void



So which of those warnings/errors do you need help with?


all of them, these are the errors i'm getting:

day14ex1.c: In function ‘main’:
day14ex1.c:39: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘double *’
day14ex1.c:40: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’
day14ex1.c: In function ‘myexp’:
day14ex1.c:48: error: ‘x’ redeclared as different kind of symbol
day14ex1.c:46: error: previous definition of ‘x’ was here



I also made the change ap0c0lyps3 suggested

This post has been edited by iqbalmmz: 04 April 2011 - 01:58 PM

Was This Post Helpful? 0
  • +
  • -

#9 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1623
  • View blog
  • Posts: 5,710
  • Joined: 03-August 09

Re: c program exponential functions; function prototypes

Posted 04 April 2011 - 02:00 PM

lets look at how to fix those.

to format doubles correctly use "%lf".
in myexp you declare 'x' as an argument then re-declare it as a local variable...why did you do that?
then you want to return a value from the function which you said you have fixed.
Was This Post Helpful? 1
  • +
  • -

#10 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: c program exponential functions; function prototypes

Posted 04 April 2011 - 02:04 PM

The d in the format string indicates an int. To use scanf to read in a double use "%lf".

d also indicates an int for printf. But here, use "%f".

And in myexp, x is already declared in the function argument, so don't redeclare it inside the function. Just eliminate that line.

There seems to be an echo here. :)
Was This Post Helpful? 1
  • +
  • -

#11 iqbalmmz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 92
  • Joined: 15-February 11

Re: c program exponential functions; function prototypes

Posted 04 April 2011 - 02:17 PM

View Postr.stiltskin, on 04 April 2011 - 01:04 PM, said:

The d in the format string indicates an int. To use scanf to read in a double use "%lf".

d also indicates an int for printf. But here, use "%f".

And in myexp, x is already declared in the function argument, so don't redeclare it inside the function. Just eliminate that line.

There seems to be an echo here. :)


okay its now compiling. however i'm not getting correct output. exp(x) should be epsilon to power x. if I enter 2, exp(2) should be 7.38
this is the output im getting:
Enter x: 2
exp(1) =  



How did the 1 get into exp()? Why isn't it at least giving any kind of output?
Was This Post Helpful? 0
  • +
  • -

#12 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1623
  • View blog
  • Posts: 5,710
  • Joined: 03-August 09

Re: c program exponential functions; function prototypes

Posted 04 April 2011 - 02:20 PM

can we see the code?
Was This Post Helpful? 0
  • +
  • -

#13 aaa111  Icon User is offline

  • D.I.C Regular

Reputation: 88
  • View blog
  • Posts: 284
  • Joined: 21-February 07

Re: c program exponential functions; function prototypes

Posted 04 April 2011 - 02:24 PM

View Postiqbalmmz, on 04 April 2011 - 02:17 PM, said:

okay its now compiling. however i'm not getting correct output. exp(x) should be epsilon to power x. if I enter 2, exp(2) should be 7.38
this is the output im getting:
Enter x: 2
exp(1) =  



How did the 1 get into exp()? Why isn't it at least giving any kind of output?


It prints what you told it to print:
printf("exp(%d) =  ", x, myexp(x));

Pay special attention to your specifier.
Was This Post Helpful? 1
  • +
  • -

#14 iqbalmmz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 92
  • Joined: 15-February 11

Re: c program exponential functions; function prototypes

Posted 04 April 2011 - 02:30 PM

View Postishkabible, on 04 April 2011 - 01:20 PM, said:

can we see the code?

yessir
#include <stdio.h>
#include <stdlib.h>
#define EPSILON 0.000001

double myexp(double x);
main ()
{
	double x;
	printf("Enter x: ");
	scanf("%lf", &x);
	printf("exp(%lf) = %lf ", x, myexp(x));
	
	return 0;
}

double myexp(double x)
{
	double term = 1.0; //represents a term in exponential series
	double sum = 0; //represents an approximation to e^x
	int n = 1; //counter LCV
	
	while (n <= x) {
		sum += term;
		term = term * x / n;
		n += 1;
		if (term < EPSILON)
			break;
		else
			sum += term;
	}
	return sum;
}



this is my output:
Iqbals-MacBook-Air:desktop iqbal$ ./l
Enter x: 2
exp(2.000000) = 7.000000 
Iqbals-MacBook-Air:desktop iqbal$ gcc day14ex1.c -o l
Iqbals-MacBook-Air:desktop iqbal$ ./l
Enter x: 1
exp(1.000000) = 2.000000 
Iqbals-MacBook-Air:desktop iqbal$ 



I should be getting:
exp(2) = 7.389056099
exp(1) = 2.718281828


This post has been edited by iqbalmmz: 04 April 2011 - 02:43 PM

Was This Post Helpful? 0
  • +
  • -

#15 iqbalmmz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 92
  • Joined: 15-February 11

Re: c program exponential functions; function prototypes

Posted 04 April 2011 - 02:37 PM

View Postaaa111, on 04 April 2011 - 01:24 PM, said:

View Postiqbalmmz, on 04 April 2011 - 02:17 PM, said:

okay its now compiling. however i'm not getting correct output. exp(x) should be epsilon to power x. if I enter 2, exp(2) should be 7.38
this is the output im getting:
Enter x: 2
exp(1) =  



How did the 1 get into exp()? Why isn't it at least giving any kind of output?


It prints what you told it to print:
printf("exp(%d) =  ", x, myexp(x));

Pay special attention to your specifier.


got it!! Now i'm making progress, however the output im getting now is : 7.00000
it should be 7.32...
SHould I change the variable type? to get the correct output
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2