Browse Source

Add first code of chapter 04

master
T. Meissner 5 years ago
parent
commit
62d2558108
8 changed files with 178 additions and 0 deletions
  1. +15
    -0
      chapter_04/activity_4.01/main.go
  2. +21
    -0
      chapter_04/exercise_4.01/main.go
  3. +23
    -0
      chapter_04/exercise_4.02/main.go
  4. +22
    -0
      chapter_04/exercise_4.03/main.go
  5. +22
    -0
      chapter_04/exercise_4.04/main.go
  6. +20
    -0
      chapter_04/exercise_4.05/main.go
  7. +23
    -0
      chapter_04/exercise_4.06/main.go
  8. +32
    -0
      chapter_04/exercise_4.07/main.go

+ 15
- 0
chapter_04/activity_4.01/main.go View File

@ -0,0 +1,15 @@
package main
import "fmt"
func main() {
var arr [10]int
for i := 0; i < len(arr); i++ {
arr[i] = i + 1
}
fmt.Println(arr)
}

+ 21
- 0
chapter_04/exercise_4.01/main.go View File

@ -0,0 +1,21 @@
package main
import "fmt"
func defineArray() [10]int {
var arr [10]int
return arr
}
func main() {
fmt.Printf("%#v\n", defineArray())
// Array with size set from initialization
// []int would be result in a slice
bla := [...]int{1, 2, 3, 4, 5}
fmt.Printf("%#v\n", bla)
}

+ 23
- 0
chapter_04/exercise_4.02/main.go View File

@ -0,0 +1,23 @@
package main
import "fmt"
func compArrays() (bool, bool, bool) {
var arr1 [5]int
arr2 := [5]int{0}
arr3 := [...]int{0, 0, 0, 0, 0}
arr4 := [5]int{0, 0, 0, 0, 9}
return arr1 == arr2, arr1 == arr3, arr1 == arr4
}
func main() {
comp1, comp2, comp3 := compArrays()
fmt.Println("[5]int == [5]int{0} :", comp1)
fmt.Println("[5]int == [...]int{0, 0, 0, 0, 0}:", comp2)
fmt.Println("[5]int == [5]int{0, 0, 0, 0, 9} :", comp3)
}

+ 22
- 0
chapter_04/exercise_4.03/main.go View File

@ -0,0 +1,22 @@
package main
import "fmt"
func compArrays() (bool, bool, [10]int) {
var arr1 [10]int
arr2 := [...]int{9: 0}
arr3 := [10]int{1, 9: 10, 4: 5}
return arr1 == arr2, arr1 == arr3, arr3
}
func main() {
comp1, comp2, arr3 := compArrays()
fmt.Println("[10]int == [...]int{9: 0} :", comp1)
fmt.Println("[10]int == [10]int{1, 9: 10, 4: 5}:", comp2)
fmt.Println("arr3 :", arr3)
}

+ 22
- 0
chapter_04/exercise_4.04/main.go View File

@ -0,0 +1,22 @@
package main
import "fmt"
func message() string {
arr := [...]string{
"ready",
"Get",
"Go",
"to",
}
return fmt.Sprintln(arr[1], arr[0], arr[3], arr[2])
}
func main() {
fmt.Print(message())
}

+ 20
- 0
chapter_04/exercise_4.05/main.go View File

@ -0,0 +1,20 @@
package main
import "fmt"
func message() string {
arr := [4]string{"ready", "Get", "Go", "to"}
arr[1] = "It's"
arr[0] = "time"
return fmt.Sprintln(arr[1], arr[0], arr[3], arr[2])
}
func main() {
fmt.Print(message())
}

+ 23
- 0
chapter_04/exercise_4.06/main.go View File

@ -0,0 +1,23 @@
package main
import "fmt"
func message() string {
m := ""
arr := [4]int{1, 2, 3, 4}
for i := 0; i < len(arr); i++ {
arr[i] *= arr[i]
m += fmt.Sprintf("%v: %v\n", i, arr[i])
}
return m
}
func main() {
fmt.Print(message())
}

+ 32
- 0
chapter_04/exercise_4.07/main.go View File

@ -0,0 +1,32 @@
package main
import "fmt"
func fillArray(arr [10]int) [10]int {
for i := 0; i < len(arr); i++ {
arr[i] = i + 1
}
return arr
}
func opArray(arr [10]int) [10]int {
for i := 0; i < len(arr); i++ {
arr[i] *= arr[i]
}
return arr
}
func main() {
var arr [10]int
arr = fillArray(arr)
arr = opArray(arr)
fmt.Println(arr)
}

Loading…
Cancel
Save