Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,377 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,147 people online right now. Registration is fast and FREE... Join Now!




Exponential series in C

 
Reply to this topicStart new topic

Exponential series in C

aaru_89
post 29 Sep, 2006 - 05:11 AM
Post #1


New D.I.C Head

*
Joined: 26 Sep, 2006
Posts: 20



Thanked 1 times
My Contributions


I wrote this program to find the value of e raised to the power of any number using the exponential series.

CODE
#include<stdio.h>
#include<conio.h>
#include<math.h>

float fac(float);   //Function declaration

void main()   //Main function starts here
{
   float sum,n,fact;
   float i;
   printf("\n Enter the value of n: ");
   scanf("%f",&n);
   for(i=0;i<5000;i++)
      {
        fact=fac(i);
        sum=sum+(pow(n,i)/fact);
      }
   printf("\n e^%f=%e",n,sum);
   getch();
}

//Sub function to calculate factorial

float fac(float i)
  {
     if(i==1)
        return(1);
     else
        return(i*fac(i-1));
  }


But it didn't turn up the expected output. Can somenone please guide to me as to what I am missing here. Thank you.
User is offlineProfile CardPM

Go to the top of the page

Louisda16th
post 29 Sep, 2006 - 05:41 AM
Post #2


 

Group Icon
Joined: 3 Aug, 2006
Posts: 1,790



Thanked 1 times

Dream Kudos: 755
My Contributions


Wierd. If I run the program on turbo c++ 3.0 (Ive got access only to this compiler at the moment). Tc++ closes and I return to windows.
User is offlineProfile CardPM

Go to the top of the page

aaru_89
post 29 Sep, 2006 - 05:58 AM
Post #3


New D.I.C Head

*
Joined: 26 Sep, 2006
Posts: 20



Thanked 1 times
My Contributions


QUOTE(Louisda16th @ 29 Sep, 2006 - 06:41 AM) *

Wierd. If I run the program on turbo c++ 3.0 (Ive got access only to this compiler at the moment). Tc++ closes and I return to windows.



Oops... I think I'm not responsible...

But here's the weirdest part. The program actually RAN SUCCESSFULLY the first few times I ran it. But suddenly it started working erroneously while I was trying to alter the limit of i to get precise values. Initially, I thought that it was due to the huge value of i. But then I figured out that it didn't work altogether.
User is offlineProfile CardPM

Go to the top of the page

Louisda16th
post 29 Sep, 2006 - 06:03 AM
Post #4


 

Group Icon
Joined: 3 Aug, 2006
Posts: 1,790



Thanked 1 times

Dream Kudos: 755
My Contributions


Are you having the same problem as me? Or is it that your program's giving wrong answers. Which compiler do you use?
User is offlineProfile CardPM

Go to the top of the page

aaru_89
post 29 Sep, 2006 - 06:43 AM
Post #5


New D.I.C Head

*
Joined: 26 Sep, 2006
Posts: 20



Thanked 1 times
My Contributions


My problem is a bit different. First it was showing some weird values as the output, something like #IND08e+002. Now, when I run the program, it asks for the value of n and does nothing. After I hit the Enter key a few times, the window closes. One more thing I found out just now. Before the window closes, I do get some output for a fraction of a second but it doesn't stay there long enough for me to see what exactly it is. But I had a glance of something starting with e^5 (my input for n was 5). So if I could somehow find a way to make it stay there, the program would be solved. I tried using getch() but it doesn't work.
User is offlineProfile CardPM

Go to the top of the page

MingusDew
post 29 Sep, 2006 - 07:13 AM
Post #6


New D.I.C Head

*
Joined: 28 Sep, 2006
Posts: 13


My Contributions


QUOTE(aaru_89 @ 29 Sep, 2006 - 07:43 AM) *
I tried using getch() but it doesn't work.

maybe try
CODE
cin.get();

and are you double clicking the icon or accessing the prog. through the command prompt, I always use the latter method?

This post has been edited by MingusDew: 29 Sep, 2006 - 07:14 AM
User is offlineProfile CardPM

Go to the top of the page

aaru_89
post 29 Sep, 2006 - 07:17 AM
Post #7


New D.I.C Head

*
Joined: 26 Sep, 2006
Posts: 20



Thanked 1 times
My Contributions


QUOTE(MingusDew @ 29 Sep, 2006 - 08:13 AM) *

QUOTE(aaru_89 @ 29 Sep, 2006 - 07:43 AM) *
I tried using getch() but it doesn't work.

maybe try
CODE
cin.get();

and are you double clicking the icon or accessing the prog. through the command prompt, I always use the latter method?



I'm trying to write a program in C and I'm still a beginner . And I know practically nothing of C++ So I'm sorry.
User is offlineProfile CardPM

Go to the top of the page

born2c0de
post 29 Sep, 2006 - 09:16 AM
Post #8


printf("I'm a %XR",195936478);

Group Icon
Joined: 26 Nov, 2004
Posts: 3,905



Thanked 34 times

Dream Kudos: 2800

Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions


The reason is that you aren't realising the limitation of your algorithm.
The actual formula requires iteration till infinity.
Here you have chosen the iteration count as 5000.
Now, at a point depending on whether you are using a 16-bit or 32-bit compiler, the factorial function will fail for larger numbers.

factorial(5000) is totally out of bounds.
And because of this, the result may up as negative and hence you get wierd results. (If you want to know why it becomes negative, check my Blog on </dic>)

So the solution to this is to limit your iteration count to 10 or 20.
That way you'll get the answer, but it'll be less accurate...But it's better than getting some wierd values.
User is offlineProfile CardPM

Go to the top of the page

born2c0de
post 29 Sep, 2006 - 09:30 AM
Post #9


printf("I'm a %XR",195936478);

Group Icon
Joined: 26 Nov, 2004
Posts: 3,905



Thanked 34 times

Dream Kudos: 2800

Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions


So Here's how we can change your code:
CODE

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

__int64 fac(int);   //Function declaration

void main()   //Main function starts here
{
   float sum,n;
   long int fact;
   float i;
   printf("\n Enter the value of n: ");
   scanf("%f",&n);
   for(i=0;i<10;i++)
      {
        fact=fac(i);
        sum=sum+(pow(n,i)/fact);
      }
   printf("\n e^%f=%e",n,sum);
   getch();
}

//Sub function to calculate factorial

__int64 fac(int i)
  {
     if(i<=1)
        return(1);
     else
        return(i*fac(i-1));
  }

__int64 is a 64-bit variable that occupies 8 bytes (which means...more range)
See how I've reduced the iteration count to 10.

Calculating e^2 gives a respectable approximation with this algorithm.

QUOTE
Wierd. If I run the program on turbo c++ 3.0 (Ive got access only to this compiler at the moment). Tc++ closes and I return to windows.

It closes because of an access violation of the address caused due to the reasons I've mentioned above.

You might also receive Division by Zero Errors since the factorial function would fail after a certain number.
User is offlineProfile CardPM

Go to the top of the page

aaru_89
post 30 Sep, 2006 - 01:50 AM
Post #10


New D.I.C Head

*
Joined: 26 Sep, 2006
Posts: 20



Thanked 1 times
My Contributions


QUOTE(born2c0de @ 29 Sep, 2006 - 10:30 AM) *

So Here's how we can change your code:
CODE

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

__int64 fac(int);   //Function declaration

void main()   //Main function starts here
{
   float sum,n;
   long int fact;
   float i;
   printf("\n Enter the value of n: ");
   scanf("%f",&n);
   for(i=0;i<10;i++)
      {
        fact=fac(i);
        sum=sum+(pow(n,i)/fact);
      }
   printf("\n e^%f=%e",n,sum);
   getch();
}

//Sub function to calculate factorial

__int64 fac(int i)
  {
     if(i<=1)
        return(1);
     else
        return(i*fac(i-1));
  }

__int64 is a 64-bit variable that occupies 8 bytes (which means...more range)
See how I've reduced the iteration count to 10.

Calculating e^2 gives a respectable approximation with this algorithm.




b2c, I think you've forgotten to declare i as int instead of float. Stepping aside, isn't there anyway in which I can get a program which gives a somewhat-precise answer? Because, this way, the error of approximation is in powers of ten...
User is offlineProfile CardPM

Go to the top of the page

aaru_89
post 30 Sep, 2006 - 03:45 AM
Post #11


New D.I.C Head

*
Joined: 26 Sep, 2006
Posts: 20



Thanked 1 times
My Contributions


This program gives fairly good values (though I've altered the limit of i to 200 and that's the only change I've made) upto e^29 after which the errors in the powers of 10 are evident. Is there any way in which I can alter this to be even more efficient?

CODE
#include<stdio.h>
#include<conio.h>
#include<math.h>
//To evaluate exponential series
float fac(float);
//Main function starts here
void main()
{
   float sum=0,n=0,fact=0;
   float i=0;
   printf("\n Enter the value of n: ");
   scanf("%f",&n);
   for(i=1;i<200;i++)
      {
        fact=fac(i);
        sum=sum+(pow(n,i)/fact);
      }
   printf("\n e^%f=%e",n,sum);
   main();
   getch();
}
//Sub function to calculate factorial
float fac(float i)
  {
     if(i==0)
        return(1);
     else
        return(i*fac(i-1));
  }


Thanks
User is offlineProfile CardPM

Go to the top of the page

born2c0de
post 30 Sep, 2006 - 05:07 PM
Post #12


printf("I'm a %XR",195936478);

Group Icon
Joined: 26 Nov, 2004
Posts: 3,905



Thanked 34 times

Dream Kudos: 2800

Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions


QUOTE(aaru_89 @ 30 Sep, 2006 - 03:20 PM) *

QUOTE(born2c0de @ 29 Sep, 2006 - 10:30 AM) *

So Here's how we can change your code:
CODE

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

__int64 fac(int);   //Function declaration

void main()   //Main function starts here
{
   float sum,n;
   long int fact;
   float i;
   printf("\n Enter the value of n: ");
   scanf("%f",&n);
   for(i=0;i<10;i++)
      {
        fact=fac(i);
        sum=sum+(pow(n,i)/fact);
      }
   printf("\n e^%f=%e",n,sum);
   getch();
}

//Sub function to calculate factorial

__int64 fac(int i)
  {
     if(i<=1)
        return(1);
     else
        return(i*fac(i-1));
  }

__int64 is a 64-bit variable that occupies 8 bytes (which means...more range)
See how I've reduced the iteration count to 10.

Calculating e^2 gives a respectable approximation with this algorithm.




b2c, I think you've forgotten to declare i as int instead of float.

That is interesting aaru_89, I think you should be asking yourself that question.
I just pasted your code and compiled it and fixed the errors. I didn't write the code. My Coding Style is different.

Al least you realised where I (I mean you) went wrong wink2.gif
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/22/08 06:14AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month