Arrays in Go lang
What is Array
- An array is the collection of similar type of elements.
- An array is the collection of fixed length.
- Array elements are stored in contiguous memory locations.
- All the array elements share the common name, but each element is uniquely identified their index or subscript, so the array is also called a subscript variable.
- Array index always starts with 0.
Creating an array
var arr [5]int
Note:
- In the above program snippet, arr is an array of 5 integers.
- By default, all the array elements are initialized with a default value.
Default Values
Data Type | Default Value |
---|---|
Int(s) | 0 |
Float(s) | 0.0 |
bool(s) | false |
Objects | nil |
Note: Following example demonstrate the default values for array elements.
Example-1
package main
import "fmt"
func main() {
var arr1 [5]int
var arr2 [5]float32
var arr3 [5]string
var arr4 [5]bool
fmt.Println(arr1)
fmt.Println(arr2)
fmt.Printf(arr3)
fmt.Println(arr4)
}
Output
[0 0 0 0 0]
[0 0 0 0 0]
[ ]
[false false false false false]
Example-2
package main
import "fmt"
func main() {
var arr [5]int
arr[4] = 100
fmt.Println(arr)
}
Output
[0 0 0 0 100]
Accessing array elements
var arr [5]int
arr[2] = 5
for i := 0; i < len(arr) ; i++ {
fmt.Printf("%d ",arr[i])
}
Output:
0 0 5 0 0
Explanation
- As shown in the above program snippet, array elements can access by index.
- So if you want to access individual elements, then use through the subscript element, and if you want to access the complete array, then you can iterate using a loop as shown in the above example.
- len() function is used to get the length the array elements.
Program to find the sum of array elements
package main
import "fmt"
func main() {
var n int
var arr [20]int
fmt.Print("Enter how many elements,<max:20>: ")
fmt.Scan(&n)
sum := 0
for i := 0 ; i < n; i++ {
fmt.Printf("Enter element %d: ",(i + 1))
fmt.Scan(&arr[i])
sum += arr[i]
}
fmt.Println("Array Sum: ",sum)
}
Output:
Enter how many elements,<max:20>: 5
Enter element 1: 2
Enter element 2: 3
Enter element 3: 5
Enter element 4: 1
Enter element 5: 4
Array Sum: 15
Explanation:
- In the above program snippet, we have created an array of 20 elements with the statement var arr [20]int, because an array is a fixed length collection.
- We have created an array of 20 elements, but a user doesn't want to enter 20 elements each time, so we have to ask user input in a variable n.
- After that, we need to receive user input for n elements in an array, using element. The following program snippet scan n element from the user using the following code snippet.
- for i := 0 ; i < n; i++ {
fmt.Printf("Enter element %d: ",(i + 1))
fmt.Scan(&arr[i])
} - In the above program snippet, we use (i + 1), for the counting of numbers like 1, 2, 3, 4, 5 and so on. Here we don't want to print message start from 0 because user doesn't know that the array index starts with 0.
- Finally while receiving input from the user, At the same time we also start adding the number and store in the variable sum with the following statement.
- sum += arr[i]
- Finally, print the value of the sum variable to display the sum of the array elements.
Program to print all elements above than the average of array elements.
package main
import "fmt"
func main() {
var n int
var arr [20]int
fmt.Print("Enter how many elements,<max:20>: ")
fmt.Scan(&n)
sum := 0
for i := 0 ; i < n; i++ {
fmt.Printf("Enter element %d: ",(i + 1))
fmt.Scan(&arr[i])
sum += arr[i]
}
avg := float32(sum) / float32(n)
fmt.Println("Elements above than average...")
for i:= 0; i < n; i++ {
if(float32(arr[i]) > avg) {
fmt.Print(arr[i]," ")
}
}
}
Output
Enter how many elements,<max:20>: 5Enter element 1: 2
Enter element 2: 4
Enter element 3: 6
Enter element 4: 1
Enter element 5: 3
Elements above than average...
4 6
Explanation
- The above program is much similar to the previous program we have discussed.
- In this program, we have found the sum of array elements as in the previous program.
- After getting the sum, we calculate the average by using the following code snippet.
- avg := float32(sum) / float32(n)
- Above program converts the sum and n, into float32 type because we want to achieve the result in float type, and both should be converted due to divide can't be performed in different types in go lang.
- Finally, we iterate all the array elements and print only those which are above than average.
Comments