Section9-ex2

This commit is contained in:
2023-11-08 13:19:00 +01:00
parent d9515dfc6d
commit e98d21c4e1
3 changed files with 98 additions and 0 deletions

47
Section9/76-1.go Normal file
View 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
View 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 {
}
}
}