I have written this code for Maclaurian series. however, this is not getting executed. Kindly help
double ExpSeries(double x, int N)
{
//int acc = 1;
int N = 2;
float answer = 1;
float temp = 1;
int i;
for (i = 1; i <= N; i++)
{
temp = (temp*i) / i;
answer += temp;
return answer;
return temp;
}
}
int main()
{
std::cout << "the sum of the exponential series is " << ExpSeries(1.0, 2) << std::endl;
}
#include <iostream>
double ExpSeries( double x, int N ) // x is not used in the function
{
//int acc = 1;
int N = 2; // this local N hides the parameter N.
float answer = 1; // these are float, x is double. Intentional?
float temp = 1;
int i;
for (i = 1; i <= N; i++)
{
temp = (temp*i) / i;
answer += temp;
return answer; // you return from first iteration
return temp; // this line is never executed
}
}
int main()
{
std::cout << "the sum of the exponential series is " << ExpSeries(1.0, 2) << std::endl;
}
Let me write a second program that does exactly the same as yours:
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <iostream>
double foo()
{
float temp = (1.0f * 1) / 1;
float answer = 1.0f + temp;
return answer; // answer is float. Function returns double
}
int main()
{
std::cout << "the sum of the exponential series is " << foo() << std::endl;
}
I don't know what the Maclaurian series is, but I bet that it is something else than foo.
Maclaurian series - 1+ [x/1!]+ [x^2/2!]+ [x^3/3!]..............
I was trying to write this code where the next term is multiplied by x^n/n and then the sum is taken for all depending upon the value of n.
<
#include <iostream>
double ExpSeries( double x, int N ) // x is not used in the function
{
int N = 2; // this local N hides the parameter N - // What do you mean by this line, can you explain it further?
float answer = 1; // these are float, x is double. Intentional? // Yes
float temp = 1;
int i;
for (i = 1; i <= N; i++)
{
temp = (temp*i) / i;
answer += temp;
return answer; // you return from first iteration // how do i return both the values of temp and answer in the program?
return temp; // this line is never executed
}
}
int main()
{
std::cout << "the sum of the exponential series is " << ExpSeries(1.0, 2) << std::endl;
}
>
Please study @Keskiverto's comments first. Then:
- remove the line int N = 2; completely; N IS SUPPLIED BY THE FUNCTION'S PARAMETER LIST - LOOK AT IT!
- remove the line return temp; completely; you DON'T NEED TO RETURN temp.
- move return answer; to after the loop;
- you should definitely use double, rather than float; there's nothing "intentional" about it; it's just silly here.
Your biggest mathematical error is temp = (temp*i) / i;
which should be temp = (temp*x) / i;
To get any sensible result, test with something considerably bigger than N=2 (e.g. 10 or above) and compare with std::exp(x), for which you will need the header <cmath>
A "Maclaurin series" is just a Taylor-Series expansion about x=0 (i.e., it's what you would most commonly think of as a power series in increasing powers of x).
PUT YOUR CODE IN CODE TAGS - it's the first item in the Format menu to the right of your text-entry box.
// this local N hides the parameter N - // What do you mean by this line, can you explain it further?
// these are float, x is double. Intentional? // Yes
// you return from first iteration // how do i return both the values of temp and answer in the program?
1 2 3 4 5 6 7 8 9 10 11 12
int snafu(
int foo // this function has a variable that has name foo
)
{
int foo = 42; // this function has a variable that has name foo
// if I now mention 'foo', do I mean 'foo' or 'foo'? They are two separate variables
// The compiler assumes that I mean the 'foo' that has been
// declared in the innermost scope
// What scopes do you see in this code?
return foo;
}
Your input data has type double.
Your function returns a double.
Why do you shrink intermediate values to float?
Why?
Why should the function return two values?
Does the "Maclaurin series" really produce a pair of values?
I thought that it is a sum; one value.
There are two ways for a function to return multiple values.
One is that it modifies caller's variables by reference.
The other is that it returns a data structure object that contains multiple values. For example, a tuple, a pair, a struct, or a class. (Cannot be a plain array.)