I recently started to learn Go. I have a task from JetBrains: "Declare a three-dimensional array of the float32 type with the size of 4 by 4 by 4 elements, assign 88.6 to its [1][0][2] element, and finally print the array to the console." I declared an array like this:
var array = [4][4][4]float32{}
Now i have few problems:
- When i'm trying to assign a value 88.6 to the array i get an error: "'88.6' (type untyped float) cannot be represented by the type [4]float32"
- I can't understand task. Do I need to assign [0][1][2] elements in every array to 88.6 or [0][1][2] dimensional arrays should be assigned to "88.6"
I hope my question is clear!
CodePudding user response:
I think the task is asking you to assign to only one element in the array:
package main
import "fmt"
func main() {
var array [4][4][4]float32
array[1][0][2] = 88.6
fmt.Println(array)
}
Output:
[[[0 0 0 0] [0 0 0 0] [0 0 0 0] [0 0 0 0]] [[0 0 88.6 0] [0 0 0 0] [0 0 0 0] [0 0 0 0]] [[0 0 0 0] [0 0 0 0] [0 0 0 0] [0 0 0 0]] [[0 0 0 0] [0 0 0 0] [0 0 0 0] [0 0 0 0]]]