| | |
solving a series
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jul 2005
Posts: 1,676
Reputation:
Solved Threads: 262
This should get you pretty close. Each term has the form (-1^n)*((x^n)/n!) where 0! is defined as 1 and x^0 is also defined as 1. E(x) is the sum of the number of terms you want to use. You calculate the sum using a running total within a loop adding each term as it is calculated. Calculate each term by changing the form to: a *(b/c) and then calculate a, b and c before calculating the term. a, b, and c can each be calculated using loops. As an alternative a and b could be calculated using pow(). As a further alternative a could be calculated using an if statement and n % 2.
Klatu Barada Nikto
I wrote two simple C++-functions:
-> One wich raises a number x to the power of y (
-> One which calculates the faculty of number x (
And here's a full example where you can see these functions at work:
-> One wich raises a number x to the power of y (
apow(x, y); )-> One which calculates the faculty of number x (
faculty(x); ) C++ Syntax (Toggle Plain Text)
long long faculty(long x) { long long y = 1; y = 1; for(int i = 1; i < (x+1); i++) y = y * i; return y; } long double apow(float x, int y) { long double result = 1; if(y == 0) return result; if(y < 0) { y = -y; for(int i = 0; i < y; i++) result = result * x; return 1/result; } for(int i = 0; i < y; i++) result = result * x; return result; }
And here's a full example where you can see these functions at work:
C++ Syntax (Toggle Plain Text)
#include <iostream> long long faculty(long x); long double apow(float x, int y); using namespace std; int main(void) { long double answer; int x = 1; cout << "6! = " << faculty(6) << endl; cout << "10^2 = " << apow(10, 2) << endl; cout << "10^-2 = " << apow(10, -2) << endl; cout << "10^0 = " << apow(10, 0) << endl; return 0; } long long faculty(long x) { long long y = 1; y = 1; for(int i = 1; i < (x+1); i++) y = y * i; return y; } long double apow(float x, int y) { long double result = 1; if(y == 0) return result; if(y < 0) { y = -y; for(int i = 0; i < y; i++) result = result * x; return 1/result; } for(int i = 0; i < y; i++) result = result * x; return result; }
Last edited by tux4life; Mar 13th, 2009 at 4:18 pm.
![]() |
Similar Threads
- Installing software on Linux: source code (*nix Software)
- power series for In(x) (challenging one) (C++)
- Hi Everyone, Have a C++ Problem. (C++)
- Hey!! Need Help With A Pseudocode Problem, Help Please (C)
- I have a few interesting problems with my PC and would love some help!!! (Windows NT / 2000 / XP)
- Slow Computer (Viruses, Spyware and other Nasties)
- CIS or CS degree question?? (Computer Science)
- New Thread button hard to find? (DaniWeb Community Feedback)
- relation of mathematics with computer science (Computer Science)
- have you ever abused ur powers? (Geeks' Lounge)
Other Threads in the C++ Forum
- Previous Thread: did you know you can do this?
- Next Thread: Using Functions and fractions
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






. 