Can someone tell me what is wrong with this code?

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 :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace 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! 
uninitialized local variable 'E'
initialize E :)

good practice to initialise everything. e.g.

1
2
3
4
5
6
7
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.
Last edited on
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
If this is the mathematical e (ie: Euler's number), then it is equal to:

2.71828182845904523536028747135266249775724709369995...

http://en.wikipedia.org/wiki/E_%28mathematical_constant%29


EDIT: or, are you trying to calculate e from stratch?

EDIT 2: if you are calculating e from scratch, then it looks like you have it correct. e5 is 148.4131591
Last edited on
prof said it's the mathematical e
K
Last edited on
are you trying to calculate e from stratch?

nah:
Enter The Value of X to estimate e^X:

OP are you trying to do a Taylor expansion kind of thing?

i.e.
e^x = 1 + x + (x^2)/2! + (x^3)/3! + etc etc..

http://en.wikipedia.org/wiki/Taylor_series
Last edited on
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.
Last edited on
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
e^x = (x^0)/0! + (x^1)/1! + (x^2)/2! + (x^3)/3! + ..... (x^n)/n!

so, for (A = 1; A <= 100; A++) in this code you have to run for A=0 to A<=n

and run it 4 times as asked by teacher
where N is 1, 5, 25, and 125.


then you have to print four e^x values for four different n values.
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++) ?
make a function of return type double (the result) which takes two parameters: x and n.

EDIT: or use same copy of your loop 4 times.
Last edited on
Thanks everyone!
Last edited on
Topic archived. No new replies allowed.