Arrays in Go lang

arrays in go lang

What is Array

  1. An array is the collection of similar type of elements.
  2. An array is the collection of fixed length.
  3. Array elements are stored in contiguous memory locations.
  4. 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.
  5. Array index always starts with 0.

Creating an array

var arr [5]int

Note: 

  1. In the above program snippet, arr is an array of 5 integers.
  2. 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

  1.  As shown in the above program snippet, array elements can access by index.
  2. 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.
  3. 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:

  1. 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.
  2. 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.
  3.  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.
    1. for i := 0 ; i < n; i++ {
      fmt.Printf("Enter element %d: ",(i + 1))
      fmt.Scan(&arr[i])
      }
  4. 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.
  5. 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.
    1. sum += arr[i]
  6. 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>: 5
Enter 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 

  1. The above program is much similar to the previous program we have discussed.
  2. In this program, we have found the sum of array elements as in the previous program.
  3. After getting the sum, we calculate the average by using the following code snippet.
    1. avg := float32(sum) / float32(n)
  4. 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.
  5. Finally, we iterate all the array elements and print only those which are above than average.

Comments

Popular posts from this blog

String in golang

Arrays in C Language

Literals in Java

Pointers in C Language

Inline V/S Block Level Element

Reserved Words in Java

Identifiers

Data Types in Go language

Printing in C programming

Variable Naming & Scope of Variable