0

Trying to solve "Continuous Inputs" problem in GOlang

The problem ask to create a user input for the number of elements in an array. I am to then create user inputs for the array elements based on the number of elements they inout for the array to contain. For example: 5 8 7 6 5 9 The first number "5" represents the number of elements the array will contain and the subsequent numbers, "8", "7", "6", "5", "9" represent the user inputfor the actual elements in the array resulting in: [8, 7, 6, 5, 9]. Using Go, I know how to ask for the first user input: var n int, fmt.Scanln(&n), but I am not sure how to get the element inputs of the array in Go. In python it would go like: n = int(input()) num = [] for i in range(n): num.append(int(input()) print(num) I am not sure how to do this in Go.

4th Dec 2021, 12:06 AM
Robert Haugland
Robert Haugland - avatar
2 Antworten
+ 1
Paul, Got it. Thanks. The. missing part I needed was: fmt.Scanln(&a[i]) Where a is rhe array then I just printed the array. It worked.
4th Dec 2021, 11:10 AM
Robert Haugland
Robert Haugland - avatar
0
Say the first input is called n. I made a for loop that runs n times. i is the counter. The first statement in the for loop reads a number and puts it in m The second one puts the number in the array. With a[i] = m
4th Dec 2021, 7:47 AM
Paul
Paul - avatar