C++ local variable changes value -
I have the following C ++ function, which is trying to find the maximum and constant amount, negative and positive Within the array of integers
int MaxSubArray :: find_max_subarray (zero) {int maxsofar = 0; Integer maximum = 0; For (Int i = 0; I For the array containing 10,20,30, -50, 50, I get the following output
Here's the maximum: 0 maxsofar is: 0 Here's the maximum: 10 maximum: 10 maximum: 30 maximum is: 30 here maximum: 60 maximum: 60 maximum is here: 10 maximum: 60 here max: 60 max: 60 maximum till last is135205 max end 135205 is 135205 max sub array
Can someone tell me why the value for variable maxsofar 135205 varies, outside of the loop. Thanks in advance
It should not be:
for( IIT = 0; i
?
Note that you have modified
maxsofar in the last loop running after you printed it, so you see a difference - it is possible That you are adding to the value of the garbage on that last trip due to your off-by-one loop boundary.
Hope you are enjoying programming pearls
Comments
Post a Comment