forked from MJK618/100HoursOfGo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhour4.go
More file actions
53 lines (44 loc) · 881 Bytes
/
Copy pathhour4.go
File metadata and controls
53 lines (44 loc) · 881 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package _100HoursOfGo
import (
"fmt"
)
//Slicing
func hour4 () {
fmt.Println("Enter a number")
var n int
fmt.Scanln(&n)
var slice = make([]int, 0)
appendOddsTillNToSlice(n, &slice)
var len int
len = display(slice)
fmt.Println("Size of slice is ", len)
fmt.Println("Enter a number")
var m int
fmt.Scanln(&m)
mutiplyBy2Slice(m, &slice)
display(slice)
var remove5th = append(slice[:5], slice[6:]...)
display(remove5th)
}
//Functions
func appendOddsTillNToSlice(n int, slice *[]int) {
for i := 1; i <= n; i++ {
if i%2 == 1 {
*slice = append(*slice, i)
}
}
}
func display(slice []int) int {
for i := 0; i < len(slice); i++ {
fmt.Print(slice[i], " -> ", i, "| ")
}
fmt.Println(".")
return len(slice)
}
func mutiplyBy2Slice(m int, slice *[]int) {
for i := 0; i < len(*slice); i++ {
if (*slice)[i] >= m {
(*slice)[i] = (*slice)[i] * 2
}
}
}