aprendago icon indicating copy to clipboard operation
aprendago copied to clipboard

Exercício: Capítulo 9, Exercício 1 (Nível: 4)

Open vkorbes opened this issue 4 years ago • 16 comments

Exercício: Capítulo 9, Exercício 1 (Nível: 4)

Link para o vídeo:

Use esta thread para compartilhar sua solução, discutir o exercício com os colegas e pedir ajuda caso tenha dificuldades!

vkorbes avatar Sep 30 '20 17:09 vkorbes

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

diegoparra avatar Oct 07 '20 01:10 diegoparra

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)
}

haystem avatar Nov 20 '20 02:11 haystem

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.

an4kein avatar Feb 18 '21 12:02 an4kein


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)

}

alansantosmg avatar Apr 21 '21 02:04 alansantosmg

https://play.golang.org/p/BuKA70b-mAf

Lucasmirandar avatar Jun 09 '21 22:06 Lucasmirandar

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%  

Resolução do Exercício


JPauloMoura avatar Aug 08 '21 14:08 JPauloMoura

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)
}

tomashugo avatar Oct 10 '21 15:10 tomashugo

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)

}

CaueFarias avatar Mar 15 '22 16:03 CaueFarias

https://go.dev/play/p/ahEiq9qxz4V

viniciussanchez avatar Mar 17 '22 14:03 viniciussanchez

https://go.dev/play/p/RsPzzrMM3Rs

AlissonAp avatar Apr 15 '22 00:04 AlissonAp

Cap. 9 – Exercícios: Nível #4 – 1 https://go.dev/play/p/SPZ9HDa5YGj

image

wfrsilva avatar May 27 '22 01:05 wfrsilva

func main() { array := [5]int{1, 2, 3, 4, 5}

for indice, valor := range array {
	fmt.Println(indice, " - ", valor)
}

fmt.Printf("%T", array)

}

M3L1M avatar Feb 13 '23 02:02 M3L1M

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)
}

adelsonsljunior avatar May 13 '23 21:05 adelsonsljunior

https://go.dev/play/p/agOTvbRe4Wa

DominMFD avatar May 15 '24 13:05 DominMFD

https://go.dev/play/p/gjbZogaeCeM

thiagoCalazans-dev avatar May 18 '24 11:05 thiagoCalazans-dev

https://go.dev/play/p/8IS4kH2cSN2

Vitor-Zen avatar Jul 04 '24 13:07 Vitor-Zen