Browse Source

Add first code of chapter 04

T. Meissner 1 month ago
parent
commit
62d2558108

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

@@ -0,0 +1,15 @@
1
+package main
2
+
3
+import "fmt"
4
+
5
+func main() {
6
+
7
+  var arr [10]int
8
+
9
+  for i := 0; i < len(arr); i++ {
10
+    arr[i] = i + 1
11
+  }
12
+
13
+  fmt.Println(arr)
14
+
15
+}

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

@@ -0,0 +1,21 @@
1
+package main
2
+
3
+import "fmt"
4
+
5
+func defineArray() [10]int {
6
+
7
+  var arr [10]int
8
+  return arr
9
+
10
+}
11
+
12
+func main() {
13
+
14
+  fmt.Printf("%#v\n", defineArray())
15
+
16
+  // Array with size set from initialization
17
+  // []int would be result in a slice
18
+  bla := [...]int{1, 2, 3, 4, 5}
19
+  fmt.Printf("%#v\n", bla)
20
+
21
+}

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

@@ -0,0 +1,23 @@
1
+package main
2
+
3
+import "fmt"
4
+
5
+func compArrays() (bool, bool, bool) {
6
+
7
+  var arr1 [5]int
8
+  arr2 := [5]int{0}
9
+  arr3 := [...]int{0, 0, 0, 0, 0}
10
+  arr4 := [5]int{0, 0, 0, 0, 9}
11
+
12
+  return arr1 == arr2, arr1 == arr3, arr1 == arr4
13
+}
14
+
15
+func main() {
16
+
17
+  comp1, comp2, comp3 := compArrays()
18
+
19
+  fmt.Println("[5]int == [5]int{0}              :", comp1)
20
+  fmt.Println("[5]int == [...]int{0, 0, 0, 0, 0}:", comp2)
21
+  fmt.Println("[5]int == [5]int{0, 0, 0, 0, 9}  :", comp3)
22
+
23
+}

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

@@ -0,0 +1,22 @@
1
+package main
2
+
3
+import "fmt"
4
+
5
+func compArrays() (bool, bool, [10]int) {
6
+
7
+  var arr1 [10]int
8
+  arr2 := [...]int{9: 0}
9
+  arr3 := [10]int{1, 9: 10, 4: 5}
10
+
11
+  return arr1 == arr2, arr1 == arr3, arr3
12
+}
13
+
14
+func main() {
15
+
16
+  comp1, comp2, arr3 := compArrays()
17
+
18
+  fmt.Println("[10]int == [...]int{9: 0}         :", comp1)
19
+  fmt.Println("[10]int == [10]int{1, 9: 10, 4: 5}:", comp2)
20
+  fmt.Println("arr3                              :", arr3)
21
+
22
+}

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

@@ -0,0 +1,22 @@
1
+package main
2
+
3
+import "fmt"
4
+
5
+func message() string {
6
+
7
+  arr := [...]string{
8
+    "ready",
9
+    "Get",
10
+    "Go",
11
+    "to",
12
+  }
13
+
14
+  return fmt.Sprintln(arr[1], arr[0], arr[3], arr[2])
15
+
16
+}
17
+
18
+func main() {
19
+
20
+  fmt.Print(message())
21
+
22
+}

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

@@ -0,0 +1,20 @@
1
+package main
2
+
3
+import "fmt"
4
+
5
+func message() string {
6
+
7
+  arr := [4]string{"ready", "Get", "Go", "to"}
8
+
9
+  arr[1] = "It's"
10
+  arr[0] = "time"
11
+
12
+  return fmt.Sprintln(arr[1], arr[0], arr[3], arr[2])
13
+
14
+}
15
+
16
+func main() {
17
+
18
+  fmt.Print(message())
19
+
20
+}

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

@@ -0,0 +1,23 @@
1
+package main
2
+
3
+import "fmt"
4
+
5
+func message() string {
6
+
7
+  m := ""
8
+  arr := [4]int{1, 2, 3, 4}
9
+
10
+  for i := 0; i < len(arr); i++ {
11
+    arr[i] *= arr[i]
12
+    m += fmt.Sprintf("%v: %v\n", i, arr[i])
13
+  }
14
+
15
+  return m
16
+
17
+}
18
+
19
+func main() {
20
+
21
+  fmt.Print(message())
22
+
23
+}

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

@@ -0,0 +1,32 @@
1
+package main
2
+
3
+import "fmt"
4
+
5
+func fillArray(arr [10]int) [10]int {
6
+
7
+  for i := 0; i < len(arr); i++ {
8
+    arr[i] = i + 1
9
+  }
10
+
11
+  return arr
12
+
13
+}
14
+
15
+func opArray(arr [10]int) [10]int {
16
+
17
+  for i := 0; i < len(arr); i++ {
18
+    arr[i] *= arr[i]
19
+  }
20
+
21
+  return arr
22
+
23
+}
24
+
25
+func main() {
26
+
27
+  var arr [10]int
28
+  arr = fillArray(arr)
29
+  arr = opArray(arr)
30
+  fmt.Println(arr)
31
+
32
+}