Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Go programming The following program is to be changed such that it uses arbitrary sized slices instead of fixed size arrays. In particular, the user

Go programming

The following program is to be changed such that it uses arbitrary sized slices instead of fixed size arrays. In particular, the user entered size (variable sz) is to replace the hard-coded 32 in main and any function changed correspondingly.

package main import "fmt" import "math" import "math/rand" type Series struct { a, b float64 } func (s Series) add(t, TP int) float64 { return s.a*math.Sin(2.0*math.Pi*float64(t)/float64(TP)) + s.b*math.Cos(2.0*math.Pi*float64(t)/float64(TP)) } func fourier(c [32]Series, t, TP int) (res float64) { res = c[0].a for n := 1; n < 32; n++ { res += c[n].add(t, TP) } return } func main() { TP := 4 sz := 1 var res float64 // Enter size of fourier series fmt.Print("Size of series (1 ... 512): ") // Depending on your environment you may have to remove in the scanf _, err := fmt.Scanf("%d ", &sz) for err != nil || sz < 1 || sz > 512 { fmt.Println("Must be positive integer (1...512).") fmt.Println("Size of series (1 ... 512): ") _, err = fmt.Scanf("%d ", &sz) } fmt.Printf("Size: %d ", sz) var c [32]Series for t := 0; t < TP; t++ { for k := 0; k < 32; k++ { c[k].a = rand.Float64() c[k].b = rand.Float64() } res += fourier(c, t, TP) fmt.Printf("%f ", res) fmt.Println() } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Theory Icdt 97 6th International Conference Delphi Greece January 8 10 1997 Proceedings Lncs 1186

Authors: Foto N. Afrati ,Phokion G. Kolaitis

1st Edition

3540622225, 978-3540622222

More Books

Students also viewed these Databases questions