I trying to calculate a cumulative sum for a list of ints, in imperative style in scala using var and for loop, this seems to be working but I wonder can we do this without using var and counter in a better way?
here is my code
val ar1 = Array[Int](1, 0, 1, 0, 0)
//> ar1 : Array[Int] = Array(1, 0, 1, 0, 0)
var cumsum = 0.0;
//> cumsum : Double = 0.0
for (i <- 0 until ar1.length) {
if (i == 0) {
cumsum = 0;
} else {
cumsum = ar1(i - 1) * 0.99 + cumsum;
}
println(cumsum)
}
and here is the output I am expecting
//> 0.0
//| 0.99
//| 0.99
//| 1.98
//| 1.98