Page under construction!!

Last occurrence - exercise text

Consider the following program last_occurrence.go which reads a sequence of 10 numbers from standard input and a further number and prints the position of the last occurrence of this further number in the sequence of 10 numbers.

package main
import "fmt"
func main() {
    var (
        s [10]int
        n int
    )

    fmt.Println("Enter 10 numbers:")
    for i := 0; i < 10; i++ {
        fmt.Scan(&s[i])
    }

    fmt.Println("Enter a number:")
    fmt.Scan(&n)

    var i int;
    for i = 9; i >= 0; i-- {
        if s[i] == n {
            break;
        }
    }
    fmt.Println(i);
}

Analyze the source code and answer the following questions.

  1. Through which channel does the program receive the data to process?

  2. What type(s) are the input data?

  3. Are the input data ready to be processed or are they first preprocessed in order to compute/extract other data?

  4. Which input data are stored in single variables and which in structures (array, slice, etc.)

  5. When does the program stop reading input data?

  6. Why are some data stored in a structure?