In this code, I am generating values for 2 different arrays. The value inside one array is dependant on the values inside the other array. The way it works is that in the array next_month_initial, the value is dependant on the corresponding value in the water_deficit array. However, the water_deficit array value is dependant on the previous (not corresponding) value in the next_month_initial array. Hence why the first value for the water_deficit array is calculated independently, as no previous value exists in the next_month_initial array. I hope this is clear enough to understand.
The code may be slightly confusing but content wise it is correct, the calculations are correct. There is no error message shown however the program is unable to correctly calculate all of the values in the array. When I print the array, instead of seeing an array with the correct values listed it says "Playground execution failed". I have no idea why this is happening, as i can see it this code should work.
var rainfall = [38,94,142,149,236,305,202,82,139,222,178,103]
let max_h2Ostore = 150
let carry_forward = 150
var evap_transpiration: [Int] = []
var water_deficit: [Int] = []
var next_month_initial: [Int] = []
// Generating values for water_deficit array
//The first values is generated differently to the remaining values
water_deficit[0] = rainfall[0] + carry_forward - evap_transpiration[0]
for i in 0...11 {
var x = i
if water_deficit[i] <= 0 {
next_month_initial.append(0)
} else if water_deficit[i] >= max_h2Ostore {
next_month_initial.append(max_h2Ostore)
} else {
next_month_initial.append(water_deficit[i])
}; if i != 11 {
x++
water_deficit.append(next_month_initial[i] + rainfall[x] - evap_transpiration[x])
}
}
println(water_deficit)