3 Replies - 279 Views - Last Post: 17 March 2011 - 03:55 PM Rate Topic: -----

#1 MrJabbaNoHere  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 08-March 11

Write a function that computes e^x

Posted 17 March 2011 - 03:50 PM

Hello everyone. I have written this simple code for an exercise in class. I understand all the logic, but why is it giving me these errors?

"Exercise1.c", line 19: syntax error before or at: ;
"Exercise1.c", line 25: syntax error before or at: return
"Exercise1.c", line 25: warning: old-style declaration or incorrect type for: sum

code is here
#include <stdio.h>
#define EPSILON 0.000001;
float exp(float x);

int main(void)
{
float x;
printf("enter x: ");
scanf("%f" ,&x);
printf("%f",exp(x));
}

float exp(float x)
{
	int n=1;
	float term=1.0;
	float sum=0.0;

	while(term<EPSILON)
	{
	sum=sum+term;
	term=term*x/n;
	n++;
	}
return sum;
}


Thanks.

Is This A Good Question/Topic? 1
  • +

Replies To: Write a function that computes e^x

#2 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: Write a function that computes e^x

Posted 17 March 2011 - 03:53 PM

you have a semicolon in your define directive for epsilon.
#define EPSILON 0.000001;//<-write here, that should not be there

Was This Post Helpful? 1
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: Write a function that computes e^x

Posted 17 March 2011 - 03:54 PM

Lose the ; here:

#define EPSILON 0.000001;


what happens is when the preprocessor replaces EPSILON with 0.000001, you end up with this:

while(term<0.000001;)


which is, of course, invalid.
Was This Post Helpful? 1
  • +
  • -

#4 MrJabbaNoHere  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 08-March 11

Re: Write a function that computes e^x

Posted 17 March 2011 - 03:55 PM

It's always something simple! Thanks guys.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1