Q BgQuestion:

Rookie
Karma Points: 10
Respect (100%):
posted by  ribbit on 10/8/2008 12:20:45 AM  |  status: Live  

C++ Programming Help: Solving e^x STILLNEEDHELP!

Course Textbook Chapter Problem
N/A N/A N/A N/A
Question Details:
The value e^x can be approximated by the sum
1 + x + (x^2)/2! + (x^3)/3! + ... + (x^n)/n!

Write a program that takes a value x as input and outputs this sum for n taken to be each of the values 1 to 100. The program should also output e^x calculated using the predefined function exp. The function exp is a predefined function such that exp(x) returns an approximation to the value e^x. The function exp is in the library with the header file cmath. Your program should repeat the calculation for new values of x until the user says she or he is through.

Use variables of type double to store the factorials or you are likely to produce integer overflow (or arrange your calculation to avoid any direct calculation of factorials). 100 lines of output might not fit comfortably on your screen. Output the 100 output values in a format that will fit all 100 values on the screen. For example, you might output 10 lines with 10 values on each line.

Can anybody help me write this or or at least help me get started with it? I don't know where to start from. I tried writing it out in "pseudocode" or whatever, but I'm just confused and don't know where to go. I just need a jumpstart! Thanks!

AAnswers:

Answer Question
Guru
Karma Points: 2,805
posted by Pradeep on 10/8/2008 12:53:40 AM  |  status: Live
Asker's Rating: Helpful   
ribbit's comment:
"Um.. I don't know what float or half of the stuff that is there. Can you do it in beginner C++ language? "
Response Details:

#include <stdio.h>
#include <conio.h>
#include<math.h>
long int factorial(int n);
void main()
{
 int x,i;
 float s,r;
 char c;
 clrscr();
printf("You have this series:-1+x/1! + x^2/2! + x^3/3! + x^4/4!
..x^x/x!");
 printf("
To which term you want its sum?  ");
 scanf("%d",&x);
 s=0;
 for (i=1;i<=x;i++)
  {   s=s+((float)pow(x,i)/(float)factorial(i)); }
 printf("The sum of %d terms is %f",x,1+s);
 fflush(stdin);
      getch();
}

long int factorial(int n)
 {
  if (n<=1)
 return(1);
  else
 n=n*factorial(n-1);
 return(n);
 }

Guru
Karma Points: 2,805
posted by Pradeep on 10/8/2008 5:14:47 AM  |  status: Live
Asker's Rating: Lifesaver   
ribbit's comment:
"Thanks!"
Response Details:
Now here is a code with C++ syntax.
hope this will help you and you will rate me..
 
 

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

void main()
{
 int n;
 float x,s=0;

 cout<<"You have this series:-1+x/1! + x^2/2! + x^3/3! + x^4/4!..x^x/x!";
 cout<<"\n";  \\\\for line change
 cout<<"To which term you want its sum?"
 cin>>n;

 s=0;
 for(i =1;i<=n;i++ )

   {
    s=s+((float)pow(x,i)/(float)factorial(i));     ////float is a data type
 }
     cout<<"The sum of  " <<n<<" terms is :" <<1+s;

               getch();
}

long int factorial(int n)   /////factorial function
 {
  if (n<=1)
 return(1);
  else
 n=n*factorial(n-1);
 return(n);
 }
 

Answer Question
Ask New Question

Join Cramster's Community

Cramster.com brings together students, educators and subject enthusiasts in an online study community. With around-the-clock expert help and a community of over 100,000 knowledgeable members, you can find the help you need, whenever you need it. Join for free today » How Cramster is different than tutoring »