Please write a Taylor series function. C++
Functions
The tool for solving these problems is to introduce functions, code which, like cos(x), takes one argument (a real number), performs an operation (such as a Taylor series approximation), and then returns a value (another real number).
When we define the function cos(x), we state that x is a formal parameter: that is, it is a place holder. We use that place holder in our definition of cos(x):
If we actually try to calculate, for example, cos(1), the value 1 is the actual parameter. We would substitute x = 1 into the formula for cos(x) and then return the result. In this case, we might get an approximation which looks like 0.5403023058681397174. This is the return value of the function cos(1).
In C++, we can similarly define a function, however, just as we defined variables to have a given type, we must define the types of the formal parameters and the return type, together with a name and code which must be evaluated.
Example 1
We have already seen such a function: main. Program 1 recalls the first program we saw: Hello World!
Program 1. Printing the value of a variable and its address.
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!" << endl;
return 0;
}
|
We will now examine each of the features:
int
The return type of the function: this function returns an int.
main
The name of the function (how it will be called).
()
The list of formal parameters. In this example, there are no formal parameters, so the set of parentheses is left empty.
{ ... }
This is the body of the function, the instructions which take the parameters and convert them into appropriate return values.
return 0;
The return keyword indicates that the following object should be returned from the function. The type of the object being returned must match the return type, in this case, int.
When you compile a file with a int main() function, the generated executable begins by executing that function. The returned value (in most cases 0) is returned to the operating system.
Example 2
Let us now write a function which calculates the absolute value of an int. We will call this function int abs( int x ) and it is shown in Program 2. The function void main() asks the user for a int and then prints the absolute value of the given int.
Program 2. Requesting a value and printing its absolute value.
#include <iostream>
using namespace std;
int abs( int n ) {
if ( n >= 0.0 ) {
return n;
} else {
return -n;
}
}
int main() {
int input;
cout << "Enter a number: ";
cin >> input;
cout << "|" << input << "| = " << abs( input ) << endl;
return 0;
}
|
The compiler uses the type of the parameter n to ensure that the actual parameter input is of the same type (or, at least, can be cast into the stated type). The compiler also uses the return type (int) to ensure that the result is printed correctly (as an int).
Example 3
In this third example, we will see a poor implementation of the cosine function which uses a Taylor series approximation. This is demonstrated in Program 3.
Program 3. Requesting a value and printing its cosine.
#include <iostream>
using namespace std;
double cos( double x ) {
// use the Taylor series approximation 1 - x^2/2! + x^4/4! - ...
double result = 1.0;
double term = 1.0;
double n = 0.0;
while ( term != 0.0 ) {
n += 2.0;
term *= -x*x/(n - 1)/n;
result += term;
}
return result;
}
int main() {
double x;
cout << "Enter a number: ";
cin >> x;
cout << "cos( " << x << " ) = " << cos( x ) << endl;
return 0;
}
|
Again, the return type double is used to ensure that whatever value is returned is printed as a double.
Example 4
Okay, the last two examples have been math-oriented. Now we will see a function which is related to programming.
Suppose you are writing a large piece of software and need to initialize all the entries of arrays of int to a default value: sometimes 0, sometimes 1, and sometimes -1. You could place a for-loop in every location, however, we will see that you will be writing a lot of code to do this.
Let's consider the Code Fragments 1 and 2.
Code Fragment 3. Initializing an array to -1.
int array_size = 1000;
int * array = new int[array_size];
for ( int i = 0; i < array_size; ++i ) {
array[i] = -1;
}
|
Code Fragment 4. Initializing an array to 0.
int num_accounts = 325;
int * account_balance = new int[num_accounts];
for ( int i = 0; i < num_accounts; ++i ) {
account_balance[i] = 0;
}
|
While examining the for loop, we notice that some things are the same: there is a variable i and entries of an array are being assigned.
Code Fragment 5. Extracting the common features of Code Fragments 3 and 4.
for ( int i = 0; i < array_limit; ++i ) {
array_name[i] = initial_value;
}
|
We can now create a function as follows:
We now take these pieces and define the function shown in Code Fragment 6.
Code Fragment 6. A function which initializes an array.
#include <iostream>
using namespace std;
void initialize_array( int * array_name, int array_limit, int initial_value ) {
for ( int i = 0; i < array_limit; ++i ) {
array_name[i] = initial_value;
}
// nothing to return
} |
Get Answers For Free
Most questions answered within 1 hours.