13 - Quiz¶
-
Your name:
-
Your VT email:
func columnMean(data [][]float64, col int) float64 {
M := len(data) // number of rows
var sum float64
for i := 0; i < M; i++ {
sum += data[i][col]
}
return sum / float64(M)
}
Which statement best describes the relationship between the loop iterations?
- A. Each iteration is independent in terms of reading data, but the shared variable
sumintroduces a dependency that must be handled. - B. Each iteration depends on the previous iteration because
sumis updated sequentially. - C. Each iteration modifies the input matrix
data, creating dependencies. - D. Each iteration requires the final value of
sumbefore continuing.
func cumulativeMovingAverage(data [][]float64, col int) []float64 {
M := len(data) // number of rows
var sum float64
cma := make([]float64, M)
for i := 0; i < M; i++ {
sum += data[i][col]
cma[i] = sum / float64(i+1)
}
return cma
}
Why is it difficult to parallelize the loop in cumulativeMovingAverage directly?
- A. The slice
cmacannot be written by multiple goroutines. - B. The computation requires the accumulated
sumfrom all previous iterations. - C. The matrix
datacannot be accessed concurrently. - D. Go does not allow loops to be parallelized.