Skip to content

Commit

Permalink
c1-m2 function defines a function
Browse files Browse the repository at this point in the history
  • Loading branch information
hoangbits committed May 31, 2020
1 parent 349cbcf commit 3e05c20
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
17 changes: 17 additions & 0 deletions c2-m2/displacement-calculator.go
Original file line number Diff line number Diff line change
@@ -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
}
31 changes: 31 additions & 0 deletions c2-m2/instruction.md
Original file line number Diff line number Diff line change
@@ -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))

0 comments on commit 3e05c20

Please sign in to comment.