From 3e05c20e1cbcb27bc5a7b786fb1e84e00d49e6e8 Mon Sep 17 00:00:00 2001 From: hoang Date: Sun, 31 May 2020 21:32:30 +0700 Subject: [PATCH] c1-m2 function defines a function --- c2-m2/displacement-calculator.go | 17 +++++++++++++++++ c2-m2/instruction.md | 31 +++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 c2-m2/displacement-calculator.go create mode 100644 c2-m2/instruction.md diff --git a/c2-m2/displacement-calculator.go b/c2-m2/displacement-calculator.go new file mode 100644 index 0000000..460a151 --- /dev/null +++ b/c2-m2/displacement-calculator.go @@ -0,0 +1,17 @@ +package main + +import "fmt" + +func main() { + fn := GenDisplaceFn(10, 2, 0) + fmt.Println(fn(3)) + fmt.Println(fn(5)) +} + +// GenDisplaceFn forming a function with initial value except time of displace formulation +func GenDisplaceFn(acceleration, initialVelocity, initialDisplacement float64) func(float64) float64 { + fn := func(time float64) float64 { + return 1.0/2*acceleration*time*time + initialVelocity*time + initialDisplacement + } + return fn +} diff --git a/c2-m2/instruction.md b/c2-m2/instruction.md new file mode 100644 index 0000000..bcffd73 --- /dev/null +++ b/c2-m2/instruction.md @@ -0,0 +1,31 @@ +## Instructions +The goal of this assignment is to create a routine that solves a linear kinematics problem. + +## Review criterialess +This assignment is worth a total of 10 points. + +Test the program by running it twice and testing it with two different sets of values for acceleration, initial velocity, initial displacement, and time. Give 3 points if the program works correctly for one set of values, and give 3 more points if the program works correctly for the other set of values. + +Examine the code. If the code contains a function called GenDisplaceFn() which takes three float64 arguments, acceleration a, initial velocity vo, and initial displacement so and returns a function, then give another 2 points. If the function returned by GenDisplaceFn() is used to compute the time, give another 2 points. + +## Specific Instructions + +Let us assume the following formula for displacements as a function of time t, acceleration a, initial velocity vo, and initial displacement so. + +s =½ a t2 + vot + so + +Write a program which first prompts the user to enter values for acceleration, initial velocity, and initial displacement. Then the program should prompt the user to enter a value for time and the program should compute the displacement after the entered time. + +You will need to define and use a function called GenDisplaceFn() which takes three float64 arguments, acceleration a, initial velocity vo, and initial displacement so. GenDisplaceFn() should return a function which computes displacement as a function of time, assuming the given values acceleration, initial velocity, and initial displacement. The function returned by GenDisplaceFn() should take one float64 argument t, representing time, and return one float64 argument which is the displacement travelled after time t. + +For example, let’s say that I want to assume the following values for acceleration, initial velocity, and initial displacement: a = 10, vo = 2, so = 1. I can use the following statement to call GenDisplaceFn() to generate a function fn which will compute displacement as a function of time. + +fn := GenDisplaceFn(10, 2, 1) + +Then I can use the following statement to print the displacement after 3 seconds. + +fmt.Println(fn(3)) + +And I can use the following statement to print the displacement after 5 seconds. + +fmt.Println(fn(5)) \ No newline at end of file