From e98d21c4e1087f76e5711c582bf86d6185405255 Mon Sep 17 00:00:00 2001 From: sebastian Date: Wed, 8 Nov 2023 13:19:00 +0100 Subject: [PATCH] Section9-ex2 --- Section8/74.go | 30 ++++++++++++++++++++++++++++++ Section9/76-1.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ Section9/76-2.go | 21 +++++++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 Section8/74.go create mode 100644 Section9/76-1.go create mode 100644 Section9/76-2.go diff --git a/Section8/74.go b/Section8/74.go new file mode 100644 index 0000000..185db27 --- /dev/null +++ b/Section8/74.go @@ -0,0 +1,30 @@ +package main + +import "fmt" + +func main() { + grades := [3]int{ + 1: 30, + 0: 5, + 2: 7, + } + fmt.Println(grades) + + accounts := [3]int{2: 50} + fmt.Println(accounts) + + names := [...]string{ + 5: "Dan", + } + fmt.Println(names, len(names)) + + cities := [...]string{ + 5: "Paris", + "London", //index 6 + 1: "NYC", + } + fmt.Printf("%#v\n", cities) + + weekend := [7]bool{5: true, 6: true} + fmt.Println(weekend) +} diff --git a/Section9/76-1.go b/Section9/76-1.go new file mode 100644 index 0000000..0fbd107 --- /dev/null +++ b/Section9/76-1.go @@ -0,0 +1,47 @@ +/* +Coding Exercise #1 + +1. Using the var keyword, declare an array called cities with 2 elements of +type string. Don't initialize the array. + +Print out the cities array and notice the value of its elements. + +2. Declare an array called grades of type [3]float64 and initialize only +the first 2 elements using an array literal. + +Print out the grades array and notice the value of its elements. + +3. Declare an array called taskDone using the ellipsis operator. +The elements are of type bool. Print out taskDone. + +4. Iterate over the array called cities using the classical for loop syntax and the len function and print out element by element. + +5. Iterate over grades using the range keyword and print out element +by element. +*/ +package main + +import "fmt" + +func main() { + var cities [2]string + fmt.Printf("%#v\n", cities) + + grades := [3]float64{ + 0: 1.6, + 1: 2.0, + } + fmt.Printf("%#v\n", grades) + + taskDone := [...]bool{} + fmt.Printf("%#v\n", taskDone) + + for i := 0; i < len(cities); i++ { + fmt.Println("index:", i, " value:", cities[i]) + } + + for i, v := range grades { + fmt.Println("index:", i, " value:", v) + } + +} diff --git a/Section9/76-2.go b/Section9/76-2.go new file mode 100644 index 0000000..14ebfc9 --- /dev/null +++ b/Section9/76-2.go @@ -0,0 +1,21 @@ +/* +Coding Exercise #2 + +Consider the following array declaration: +nums := [...]int{30, -1, -6, 90, -6} + +Write a Go program that counts the number of positive even numbers +in the array. +*/ + +package main + +func main() { + nums := [...]int{30, -1, -6, 90, -6} + + for i := 0; i < len(nums); i++ { + if i > 0 && i%2 { + + } + } +}