I keep getting an error message that says uninitialized local variable 'E' used and I'm not sure how to fix it. Any feedback you could give me would be greatly appreciated :)
#include <iostream>
#include <iomanip>
#include <cmath>
usingnamespace std;
int main()
{
int A, B;
double E, D;
double input, Y;
cout << "Enter The Value of X to estimate e^X: ";
cin >> input;
for (A = 1; A <= 100; A++)
{
D = 1;
for (B = 1; B <= A; B++)
{
D = D * B;
}
Y = pow(input, A);
E = E + Y / D;
}
cout << setprecision(10) << "Value of e^X is " << E + 1 << endl;
system("pause");
}
On line 10, you declare E but give it no value. Then, the first time you reach line 24 you try to use the value of E when it still has no value. You will want to initialize E to an appropriate value before trying to use it.
The problem is exactly as the error describes it. You have a variable 'E'. You attempt to read from the contents of this variable before you ever put anything inside of it.
1 2 3 4 5 6 7
D = 1; // <- D is initialized... we now know what its contents will be
//...
Y = pow(input, A); // <- Y is initialized. We know what it's contents will be
// But now, look at the following computation:
E + Y / D; // <- we know the contents of Y and D... but what is in E? You never specified.
// it could be anything!
int A=0;
int B =0;
double E = 0.0;
double D = 0.0;
double input = 0.0;
double Y = 0.0;
You initialise your other variables at some point throughout your code, but not E. So when you come to this: E = E + Y / D;
it means the first value of E is garbage.
thanks so much everyone! only i'm not very good at math and have no clue what value to give E. i just set it equal to 0 but since i'm bad at math i'm not sure if i'm getting the right answer. i entered 5 and got 148.4131591
misslyss: So yes, you are calculating e from scratch (or rather, you are approximating it). And you seem to be doing it correctly, as you got the correct result for e5.
mutexe: i think it is a taylor expansion kind of thing
disch: thats a relief. thanks for checking that for me! as you can see i was very confused about what i was even trying to do
anup30: oh no so i did it wrong? i don't understand how i'm supposed to fix it. can you explain what i should change? like how do i get it to print 4 times. and do i change for (A = 1; A <= 100; A++) to for (A = 0; A <= n; A++) ?