| | |
Anyway can help me to solve this problem???
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
Are you trying to just get the natural exponential of a number?
There's a function in math library, if I'm not wrong.
Try to see if the math.h of your compiler has a double exp (double) function.
See
http://unixhelp.ed.ac.uk/CGI/man-cgi?exp+3
For microsoft, the function prototype may be different:
http://msdn.microsoft.com/library/de...larray_exp.asp
There's a function in math library, if I'm not wrong.
Try to see if the math.h of your compiler has a double exp (double) function.
See
http://unixhelp.ed.ac.uk/CGI/man-cgi?exp+3
For microsoft, the function prototype may be different:
http://msdn.microsoft.com/library/de...larray_exp.asp
•
•
Join Date: Sep 2005
Posts: 14
Reputation:
Solved Threads: 0
#include<iostream.H>
int main()
{
//(e^x = 1 + x/1! + x^2/2! + x^3/3! + .....)
int result = 1;
int x = 2;
int z = 1;
int e = 1;
for (int i = 1 ; i < 4 ; i++)
{
while ( z != 0)
{
result *= z;
z--;
}
//cout << "Result = " << result << endl;
//cout << "Z = " << z << endl;
e = e + x^i/result;
z++;
}
cout << "The Sum = " << e << endl;
return 0;
}The Bold font is the factorial part & its not working well, i need to fix tht, plz help (i dont wanna use any function)[I][U]
•
•
Join Date: Aug 2005
Posts: 80
Reputation:
Solved Threads: 2
I think you know how to take a factorial of a number, but your variable usage is not good.
Now, honestly, that works if you first set z to the value of whatever you want to take the factorial of, and set result to one. You're setting z equal to one before the for loop, and then incrementing it at the end of each loop, but you decrement it to zero before that, so your while loop is *entirely pointless*.
Just multiply result by i, you don't need a while/for loop or whatever.
-Fredric
C++ Syntax (Toggle Plain Text)
while ( z != 0) { result *= z; z--; }
Now, honestly, that works if you first set z to the value of whatever you want to take the factorial of, and set result to one. You're setting z equal to one before the for loop, and then incrementing it at the end of each loop, but you decrement it to zero before that, so your while loop is *entirely pointless*.
Just multiply result by i, you don't need a while/for loop or whatever.
-Fredric
•
•
Join Date: Sep 2005
Posts: 14
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
#include<iostream.H> int main() { //(e^x = 1 + x/1! + x^2/2! + x^3/3! + .....) int result = 1; int x = 2; int e = 1; for (int i = 1 ; i < 4 ; i++) { result *= i; //cout << "Result = " << result << endl; e = e + x^i/result; } cout << "The Sum = " << e << endl; return 0; }
You mean like this???
•
•
Join Date: Aug 2005
Posts: 80
Reputation:
Solved Threads: 2
I didn't see it the first time, but the ^ doesn't do what you think it does in C++, if you want to take m to the n power then you use the pow function like so...
That would return 2 to the 3rd, or 8. Hope that helps a bit.
-Fredric
C++ Syntax (Toggle Plain Text)
#include <math.h> ... double m=2.0, n=3.0, result; result = pow(m, n);
That would return 2 to the 3rd, or 8. Hope that helps a bit.
-Fredric
Hi, I have some issues with your variable names. result is actually not the result, and e is actually the natural log constant in your comments. Made some changes to the var and trying to do away with the pow fn.
Not sure if you want to restrict your input to just integral values, but output will most definitely be double?
I've not tried the code, but should be ok ...
Not sure if you want to restrict your input to just integral values, but output will most definitely be double?
C++ Syntax (Toggle Plain Text)
int main() { //(e^x = 1 + x/1! + x^2/2! + x^3/3! + .....) double x = 2.0; // input value int factorial = 1; double pow_x = 1.0; double result = 1.0; cout << "e^" << x << " = 1"; for (int i = 1 ; i < 4 ; i++) { factorial *= i; pow_x *= x; cout << " + " << pow_x << "/" << factorial; result = result + pow_x/factorial; } cout << endl << "= " << result << endl; return 0; }
I've not tried the code, but should be ok ...
![]() |
Similar Threads
- Back button problem with $Sessions and GET (PHP)
- Toshiba Satellite A20 Boot Priority Problem (Troubleshooting Dead Machines)
- Hard drive or motherboard problem? (Troubleshooting Dead Machines)
- Toshiba Laptop Hard Drive Problem (Storage)
- Problem after installing MessengerPlus! (Viruses, Spyware and other Nasties)
- Netscape 7.1, Hotmail login problem - cookies 'disabled" (Windows NT / 2000 / XP)
- "cannot resolve symbol"problem (Java)
- Problem Viewing Graphics in IE (Web Browsers)
- Port 80 problem (Linux Servers and Apache)
- Sendmail and SquirrelMail problem. (Linux Servers and Apache)
Other Threads in the C++ Forum
- Previous Thread: Why is destructor called twice?
- Next Thread: Prefix expressions(recursion)
| Thread Tools | Search this Thread |
api array based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






