简体   繁体   中英

The question about three-dimensional array and assigning in Go

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:

  1. 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"
  2. 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!

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]]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM