From 59ffa2443a6946f5910916d5bdc2ebcfb3b40610 Mon Sep 17 00:00:00 2001 From: sebastian Date: Wed, 8 Nov 2023 13:42:52 +0100 Subject: [PATCH] Section9-complete --- Section9/76-2.go | 29 +++++++++++++++++++++++++---- Section9/76-3.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 Section9/76-3.go diff --git a/Section9/76-2.go b/Section9/76-2.go index 14ebfc9..4479894 100644 --- a/Section9/76-2.go +++ b/Section9/76-2.go @@ -6,16 +6,37 @@ nums := [...]int{30, -1, -6, 90, -6} Write a Go program that counts the number of positive even numbers in the array. -*/ package main +import "fmt" + func main() { nums := [...]int{30, -1, -6, 90, -6} - for i := 0; i < len(nums); i++ { - if i > 0 && i%2 { - + for i := 0; i < len(nums); { + if i%2 == 0 && i > 0 { + i++ } + fmt.Println("No. of positive even numbers in nums: ", i) } } +*/ +package main + +import "fmt" + +func main() { + nums := []int{30, -1, -6, 90, -6} + + var count int + + for _, v := range nums { + if v%2 == 0 && v > 0 { + count++ + } + } + + fmt.Println("No. of positive even numbers in nums: ", count) + +} diff --git a/Section9/76-3.go b/Section9/76-3.go new file mode 100644 index 0000000..0efde5b --- /dev/null +++ b/Section9/76-3.go @@ -0,0 +1,42 @@ +/* +Coding Exercise #3 + +There are some errors in the following Go program. Try to identify the errors, +change the code, and run the program without errors. + +package main + +import "fmt" + +func main() { + myArray := [3]float64{1.2, 5.6} + + myArray[2] = 6 + + a := 10 + myArray[0] = a + + myArray[3] = 10.10 + + fmt.Println(myArray) + +} +*/ + +package main + +import "fmt" + +func main() { + myArray := [3]float64{1.2, 5.6} + + myArray[2] = 6 + + a := 10 + myArray[0] = float64(a) + + myArray[2] = 10.10 + + fmt.Println(myArray) + +}