473,406 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Please Help with e^x estimating program

Hello,

I have been trying to figure out what I'm doing wrong on my program for two days. My professor keeps "forgetting" about me. It is due tomorrow. We are supposed to use the "summing estimator" to calculate an estimated value for exp(x) using the formula 1+ x^n/n! for a user entered value for x, and values for n from 1 to 100. I know my problem is in the logic of how to implement the formula in C++ because it compiles but gives the wrong values. I can't figure it out. Please help. Here is my program so far:

//
//
// This program will take an entered value 'x'
// and run an estimated calculation for e^x and
// also a calculation based on existing c++ exponent
// functions.
//
#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

double x; // Value entered by the user
double factorial(double n);
double sum (double factors);

int main()
{

double estimate;

cout << setiosflags(ios::fixed) << setprecision(5);

cout << "Enter a value for 'x' =>";
cin >> x;

estimate = 1 + sum(x);

cout << "The estimated value for pi is " << estimate << ".\n";

return 0;
}

// Function calculates n!

double factorial (double n)
{
double ans;
if (n<=1)
ans = 1;
else
ans = n * factorial(n-1);
return ans;
}

double sum (double factors)
{
double sum;
for (double n=1; n<=100; n++)
sum = pow(x,n)/factorial(x);

return sum;
}
Oct 18 '06 #1
6 5859
D_C
293 100+
Expand|Select|Wrap|Line Numbers
  1. double sum (double factors)
  2. {
  3.         double sum;
  4.         for (double n=1; n<=100; n++)
  5.         sum = pow(x,n)/factorial(x);
  6.  
  7. return sum;
  8. }
should be
Expand|Select|Wrap|Line Numbers
  1. sum += pow(x,n) / factorial(x);
Also, the first term of the series is 1, so you could start the loop at 0 instead of assigning
Expand|Select|Wrap|Line Numbers
  1. estimate = 1 + sum(x);
Oct 18 '06 #2
Expand|Select|Wrap|Line Numbers
  1. double sum (double factors)
  2. {
  3.         double sum;
  4.         for (double n=1; n<=100; n++)
  5.         sum = pow(x,n)/factorial(x);
  6.  
  7. return sum;
  8. }
should be
Expand|Select|Wrap|Line Numbers
  1. sum += pow(x,n) / factorial(x);
Also, the first term of the series is 1, so you could start the loop at 0 instead of assigning
Expand|Select|Wrap|Line Numbers
  1. estimate = 1 + sum(x);

THANK YOU SO VERY MUCH! I will enter that in and give it a whirl!
Oct 18 '06 #3
THANK YOU SO VERY MUCH! I will enter that in and give it a whirl!

Okay, I did that, but now all it will return no matter what I enter as the 'x' value is 1.000. That would normally tell me there is an integer division, but I intentionally used 'double' type for everything. Please help???
Oct 18 '06 #4
D_C
293 100+
The formula is summation (x^n)/n! and you are calculating summation (x^n)/x!. Change your parameter for factorial from x to n. You could also do an iterative version, it would be much faster.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. int main()
  8. {
  9.     cout << setiosflags(ios::fixed) << setprecision(5);
  10.  
  11.     double x;
  12.     cout << "Enter a value for 'x' =>";
  13.     cin >> x;
  14.  
  15.     double estimate = 0;
  16.     double exp = 1;
  17.     double fact = 1;
  18.     double r;
  19.     int n = 1;
  20.  
  21.     do
  22.     {
  23.         r = exp / fact;
  24.         estimate += r;
  25.  
  26.         exp *= x; // x^n
  27.         fact *= n++; // n!
  28.     }   while(r > 10e-6);
  29.  
  30.     cout << "The estimated value for pi is " << estimate << ".\n";
  31.  
  32.     system("PAUSE");
  33.     return EXIT_SUCCESS;
  34. }
Oct 18 '06 #5
Banfa
9,065 Expert Mod 8TB
You could also do an iterative version, it would be much faster.
Are you sure about that? can you provide references.

I only ask becasue to call a function requires a branch and the creation of a stack frame, where as a loop requires a branch and possibly the additional increment of a variable so it would seem to me that looping would be faster.
Oct 18 '06 #6
tyreld
144 100+
Are you sure about that? can you provide references.

I only ask becasue to call a function requires a branch and the creation of a stack frame, where as a loop requires a branch and possibly the additional increment of a variable so it would seem to me that looping would be faster.
I suspect you have misread iterative as recursive. The factorial function is currently implemented recursively. Changing it to a loop (ie. iterative implementation) would make it faster. Leaving you both in agreement. ;)
Oct 18 '06 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Peter King | last post by:
I am trying to figure out the best way to handle a system with lots of roaming laptops wanting to take the data with them. I have the following requirement: Web server (possibly several) which...
24
by: Greg N. | last post by:
Sorry if this topic has been discussed before: Is there any statistical data available about what percentage of browsers run with JS disabled? Thanks for any and all insights, Greg
1
by: Starx | last post by:
I have a while loop in my program that is going to execute a very large number of loops (well over 100000000). Sometimes this can take quite some time for the computer to process so I'd like the...
2
by: Jayjay | last post by:
When it comes to access, I'm pretty good using the built in features and can come up with some pretty complex functions to get what I need. But we have this database I'm doing for work that is...
2
by: Dip | last post by:
Hi Expert, How do I flatten a Parent Child hierarchy to regular flat data: please provide some SQL code: I have now: Task_ID, Parent_Task_ID, Task_Name Level 1 1 Project Management 1...
1
momotaro
by: momotaro | last post by:
The mathematician Gottfried Leibniz determined the following formula for estimating the value of Pi (3.1415…): Pi/4 = 1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + …. Evaluate the first 200 terms of this...
0
by: uncensored | last post by:
Hi, Sort of new with the whole treeview control and I was wondering is there a way to build a treeview menu using my SQL data I pull from a database instead of having to hard code it into the...
2
by: Martien van Wanrooij | last post by:
I am working on some financial calculators and although I succeeded to created the required formulas I am not sure about the following.To give an example: when somebody puts a capital on the bank...
8
by: cube | last post by:
I must sort the below values of table in a specific way. Everytime that I possition a sorted value to the best_time array, I marked it as a "job done" by giving the value 0 to the table...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.