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;
}
6 5859 - double sum (double factors)
-
{
-
double sum;
-
for (double n=1; n<=100; n++)
-
sum = pow(x,n)/factorial(x);
-
-
return sum;
-
}
should be - 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 - double sum (double factors)
-
{
-
double sum;
-
for (double n=1; n<=100; n++)
-
sum = pow(x,n)/factorial(x);
-
-
return sum;
-
}
should be - 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
THANK YOU SO VERY MUCH! I will enter that in and give it a whirl!
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???
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. - #include <iostream>
-
#include <iomanip>
-
-
using namespace std;
-
-
-
int main()
-
{
-
cout << setiosflags(ios::fixed) << setprecision(5);
-
-
double x;
-
cout << "Enter a value for 'x' =>";
-
cin >> x;
-
-
double estimate = 0;
-
double exp = 1;
-
double fact = 1;
-
double r;
-
int n = 1;
-
-
do
-
{
-
r = exp / fact;
-
estimate += r;
-
-
exp *= x; // x^n
-
fact *= n++; // n!
-
} while(r > 10e-6);
-
-
cout << "The estimated value for pi is " << estimate << ".\n";
-
-
system("PAUSE");
-
return EXIT_SUCCESS;
-
}
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.
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. ;)
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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...
|
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,...
| |