How to calculate a = e*(a+b*c+cos(d) where a,b,c,d are arrays of equal length and e is a scalar in Go

1 Answer

0 votes
package main
 
import (
    "fmt"
    "math"
)
 
func calculateFormula(a, b, c, d []float64, e float64) {
    for i, v := range a {
        a[i] = e * (v + b[i] * c[i] + math.Cos(d[i]))
    }
}
 
func main() {
    a := []float64{1.0, 2.0, 3.0, 4.0}
    b := []float64{3.1, 2.5, 8.3, 4.6}
    c := []float64{7.0, 8.0, 9.0, 10.0}
    d := []float64{11.0, 12.0, 0.0, 15.0}
    e := 7.0
 
    calculateFormula(a, b, c, d, e)
    fmt.Println("After calculation: ", a)
}
 
 
 
/*
run:
 
After calculation:  [158.93097988591634 159.90697771112744 550.9 344.6821846099882]

*/

 



answered Jul 1, 2025 by avibootz
edited Jul 1, 2025 by avibootz
...