aprendago
aprendago copied to clipboard
Exercício: Capítulo 9, Exercício 1 (Nível: 4)
Exercício: Capítulo 9, Exercício 1 (Nível: 4)
Use esta thread para compartilhar sua solução, discutir o exercício com os colegas e pedir ajuda caso tenha dificuldades!
Fiquei um pouco na dúvida quanto a demonstrar os valores do array se isso deveria incluir mostrar os indexes tbm, então decidi só mostrar os valores atribuídos e não mostrar seus respectivos indexes.
https://play.golang.org/p/Av9YE2n0GWp
Fiquei confuso na parte de printar pq usei Printf em tudo. Saiu o resultado mas ficou ruim para ler, não entendia pq n ia com printf. Depois que voltei a explicação entendi que na parte que ele lê o valor ele ja traz em numero e não precisa do printf % para direcionar.
package main
import("fmt")
func main(){
array := [5]int{1,2,3,4,5}
for ind,valor := range array{
fmt.Println(ind,valor)
}
fmt.Printf("%T",array)
}
Demorei um pouco em utilizar o range de maneira correta, mas após forçar um pouco o cérebro e mais algumas pesquisas no Spec e Effective, muita tentativa e erro, cheguei no resultado pedido no exercício:
https://play.golang.org/p/tixnE9HAp9L
package main
import (
"fmt"
)
var array = [5]int{}
func main() {
fmt.Println(`Usando uma literal composta:`)
fmt.Println("")
fmt.Println(`- Crie um array que suporte 5 valores to tipo int: `)
fmt.Println(array)
array[0] = 1
array[1] = 2
array[2] = 1337
array[3] = 345
array[4] = 45353
fmt.Println("")
fmt.Println(`- Atribua valores aos seus índices`)
for i, v := range array {
fmt.Printf("Indice: %v => Valor: %v\n", i, v)
}
fmt.Println("")
fmt.Println(`- Utilize range e demonstre os valores do array.`)
for i := range array {
fmt.Println(array[i])
}
fmt.Println("")
fmt.Println(`- Utilizando format printing, demonstre o tipo do array.`)
fmt.Printf("Type array: %T", array)
}
Output
Usando uma literal composta:
- Crie um array que suporte 5 valores to tipo int:
[0 0 0 0 0]
- Atribua valores aos seus índices
Indice: 0 => Valor: 1
Indice: 1 => Valor: 2
Indice: 2 => Valor: 1337
Indice: 3 => Valor: 345
Indice: 4 => Valor: 45353
- Utilize range e demonstre os valores do array.
1
2
1337
345
45353
- Utilizando format printing, demonstre o tipo do array.
Type array: [5]int
Program exited.
package main
import "fmt"
func main() {
myArray := [5]int{0, 1, 2, 3, 4}
for _, v := range myArray {
fmt.Println(v)
}
fmt.Printf("%T ", myArray)
}
https://play.golang.org/p/BuKA70b-mAf
Crie um array que suporte 5 valores to tipo int, atribua valores aos seus índices, utilize range e demonstre os valores do array, Utilizando format printing demonstre o tipo do array..
package main
import "fmt"
func main() {
arr := [4]int{10, 20, 30, 40}
for i, v := range arr {
fmt.Printf("Indíce %d: %d\n", i, v)
}
fmt.Printf("Tipo do array: %T", arr)
}
Resultado:
Indíce 0: 10
Indíce 1: 20
Indíce 2: 30
Indíce 3: 40
Tipo do array: [4]int%
package main
import (
"fmt"
)
func main() {
var array [5]int
array[0] = 10
array[1] = 20
array[2] = 30
array[3] = 40
array[4] = 50
for _, value := range array {
fmt.Println(value)
}
fmt.Printf("%T\n",array)
}
package main
import ( "fmt" )
func main() {
array := [6]int{0, 1, 2, 3, 4, 5}
for índice, número := range array {
fmt.Println("No índice", índice, "Temos o número: ", número)
}
fmt.Printf("%T", array)
}
https://go.dev/play/p/ahEiq9qxz4V
https://go.dev/play/p/RsPzzrMM3Rs
Cap. 9 – Exercícios: Nível #4 – 1 https://go.dev/play/p/SPZ9HDa5YGj
func main() { array := [5]int{1, 2, 3, 4, 5}
for indice, valor := range array {
fmt.Println(indice, " - ", valor)
}
fmt.Printf("%T", array)
}
package main
import (
"fmt"
)
func main() {
array := [5]int{1, 2, 3, 4, 5}
for indice, valor := range array {
fmt.Println(indice, valor)
}
fmt.Printf("%T", array)
}
https://go.dev/play/p/agOTvbRe4Wa
https://go.dev/play/p/gjbZogaeCeM
https://go.dev/play/p/8IS4kH2cSN2