writing array in loop form getting extra values

Jun 26, 2013 at 6:16am
Why do I get extra values when writing the code in a loop? I am not sure why I am getting 0.001 , 0.002, etc Also why is my setprecision not getting most of the values to 2 decimal places?
Last edited on Jun 27, 2013 at 12:35am
Jun 26, 2013 at 7:44am
Can't tell without seeing code.
Jun 26, 2013 at 9:11am
It is obvious because you are doing it incorrectly.:)
Jun 26, 2013 at 4:30pm
Updated.
Jun 26, 2013 at 7:08pm
Try to avoid doing this: (Line 2)

for(int i = 1; i <=5; i++)

If you want to do something 5 times - it looks like this :

for(int i = 0; i < 5; i++)

This is important so you don't over run array bounds.

Also, rather than hard code magic numbers, use a const variable :

1
2
3
4

const int SIZE = 5;

for(int i = 0; i < SIZE; i++)


That way if it needs to change, you can do it once, rather than changing it everywhere.

HTH
Jun 26, 2013 at 11:43pm
Thanks
Last edited on Jun 27, 2013 at 12:35am
Topic archived. No new replies allowed.