Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,880 questions

51,806 answers

573 users

How to calculate r = 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(e float64, a, b, c, d []float64) ([]float64, error) {
	// Ensure all arrays have the same length
	if len(a) != len(b) || len(b) != len(c) || len(c) != len(d) {
		return nil, fmt.Errorf("arrays must have the same length")
	}

	// Initialize result array
	r := make([]float64, len(a))

	// Perform the calculation
	for i := 0; i < len(a); i++ {
		r[i] = e * (a[i] + b[i] * c[i] + math.Cos(d[i]))
	}
	
	return r, nil
}

func main() {
	e := 7.0
	a := []float64{1.0, 2.0, 3.0}
	b := []float64{4.0, 5.0, 6.0}
	c := []float64{7.0, 8.0, 9.0}
	d := []float64{0.0, math.Pi / 4, math.Pi / 2}

	// Calculate r
	r, err := calculateFormula(e, a, b, c, d)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}

	// Print the result
	fmt.Println("After calculation: ", r)
}


/*
run:

After calculation:  [210 298.9497474683058 399]

*/

 



answered Jul 1, 2025 by avibootz
...