Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Challenge: Finding the Largest Element in a Grid | Arrays and Slices
course content

Зміст курсу

Introduction to GoLang

Challenge: Finding the Largest Element in a GridChallenge: Finding the Largest Element in a Grid

Task

Below is an unfinished code for finding the largest int element in a 4x4 matrix or grid. Read the code and complete it by replacing the blanks (___) with appropriate code.

go

index.go

Following is an unfinished code for finding the largest int element in a 4x4 matrix / grid. Read the code and complete by replacing the blanks (___) by appropriate code.

package main
import "fmt"

// The `rand` module contains the Intn method which returns a random integer.
import "math/rand"

func findMax(arr [4][4] int) int {
    var max int = arr[0][0]
    for i := 0; i < 4; i++ {
        for j := 0; j < 4; j++ {
            if arr[i][j] > max {
                max = arr[i][j]
            }
        }
    }
    return max
}

func getRandomArray() [4][4] int {
    var arr [4][4] int
    for i := 0; i < 4; i++ {
        for j := 0; j < 4; j++ {
             // Syntax: rand.Intn(max)
             // 0 <= Returned Value < max
             // return value may be 0, cannot be `max`
            arr[i][j] = rand.Intn(100)
        }
    }
    return arr
}

func main() {
    var numbers = getRandomArray()
    var max = findMax(numbers)

    fmt.Println(numbers)
    fmt.Println("Maximum:", max)
}
      

Все було зрозуміло?

Секція 5. Розділ 9
course content

Зміст курсу

Introduction to GoLang

Challenge: Finding the Largest Element in a GridChallenge: Finding the Largest Element in a Grid

Task

Below is an unfinished code for finding the largest int element in a 4x4 matrix or grid. Read the code and complete it by replacing the blanks (___) with appropriate code.

go

index.go

Following is an unfinished code for finding the largest int element in a 4x4 matrix / grid. Read the code and complete by replacing the blanks (___) by appropriate code.

package main
import "fmt"

// The `rand` module contains the Intn method which returns a random integer.
import "math/rand"

func findMax(arr [4][4] int) int {
    var max int = arr[0][0]
    for i := 0; i < 4; i++ {
        for j := 0; j < 4; j++ {
            if arr[i][j] > max {
                max = arr[i][j]
            }
        }
    }
    return max
}

func getRandomArray() [4][4] int {
    var arr [4][4] int
    for i := 0; i < 4; i++ {
        for j := 0; j < 4; j++ {
             // Syntax: rand.Intn(max)
             // 0 <= Returned Value < max
             // return value may be 0, cannot be `max`
            arr[i][j] = rand.Intn(100)
        }
    }
    return arr
}

func main() {
    var numbers = getRandomArray()
    var max = findMax(numbers)

    fmt.Println(numbers)
    fmt.Println("Maximum:", max)
}
      

Все було зрозуміло?

Секція 5. Розділ 9
some-alt