Section9-ex2
This commit is contained in:
30
Section8/74.go
Normal file
30
Section8/74.go
Normal file
@@ -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)
|
||||
}
|
||||
47
Section9/76-1.go
Normal file
47
Section9/76-1.go
Normal file
@@ -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)
|
||||
}
|
||||
|
||||
}
|
||||
21
Section9/76-2.go
Normal file
21
Section9/76-2.go
Normal file
@@ -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 {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user