Objectives: To better understand how simple operations work in the new language
ID: 3863893 • Letter: O
Question
Objectives: To better understand how simple operations work in the new language Go, developed by some of the designers of the C language Assignment: You need to write two Go functions, mcompute() and 1compute(). When used, such as by the program below: package main import "fmt"//Your code for mcompute() and 1compute() go here func main() {fmt.Println(mcompute1("sayhello", 30, 20)) fmt.Println(mcompute2("add", mcompute2("sub", 70, 40), 20)) fmt.Println(mcompute2("sub", 30, 20)) fmt.Println(mcompute3("equalOrLarger", 10, 5)) fmt.Println(mcompute3("equalOrLarger", 5, 5)) fmt.Println(mcompute3("equalOrLarger", 5, 15)) 1st:=[3]int{l, 2, 3} fmt.Println(1compute1("find", 1, 1st)) fmt.Println(1compute1("find", 3, 1st)) fmt.Println(1compute2("append", 7, 1st)) fmt.Println(1compute2("insert", 7, 1st))} The result is: hello 30 and 20, nice to meet you. 50 10 true true false 0 2 [1, 2, 3, 7] [7, 1, 2, 3]Explanation / Answer
func mcompute1(op string, x int, y int) {
fmt.Printf("Hello %d and %d , nice to meet you. ",x,y)
}
func mcompute2(op string, x int, y int) int {
if(op == "add"){
return x + y
}
if(op == "sub"){
return x - y
}
return 0
}
func mcompute3(op string, x int, y int) bool{
if(x >= y){
return true
}
return false
}
func lcompute1( n string, e int, s [10]int) int {
var pos int = -1
for _, a := range s {
if a == e {
pos:= pos+1
return pos
}
}
return -1
}
func lcompute2( op string, e int, s [10]int) [10]int {
if(op == "insert"){
s[0] = e
return s
}
if(op == "appand"){
s[9] = e
return s
}
return s
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.