0

I am trying to make a simple average calculator in golang using Slices and For Loops.

But i am having an error in VS code, this one:

panic: runtime error: index out of range [0] with length 0 goroutine 1 [running]: main.main() C:/Desktop/cs50/week2/myarray.go:16 +0x134 exit status 2

I am using VS code on W10.

My code:

package main

import "fmt"

func main() {
    var n int
    scores := []uint{}
    var sumScores float32 = 0

    fmt.Println("How many scores?") //ask user how many values
    fmt.Scan(&n)                    //get how many values

    for i := 0; i < n; i++ {
        fmt.Printf("Scores: ")                     // ask for values
        fmt.Scan(&scores[i])                       // get values
        sumScores = sumScores + float32(scores[i]) // sum values
    }

    fmt.Printf("Average: %f\n", sumScores/float32(n)) //average value
}

Any idea what can be wrong?

I think it could be related to my Slice definition using float32.

Thank you in advanced.

4
  • 1
    scores slice is of length 0. You don't actually use that slice. You can simply scan to an int, and keep adding that to sumScores. Jun 1, 2022 at 22:35
  • 1
    scores = make([]uint, n) Jun 1, 2022 at 22:44
  • You have declared scores as an array of int. As such, sumScores will always be n int as well. You will only get a float as the result of the divide.
    – Gardener
    Jun 1, 2022 at 23:15
  • @RahmatFathoni thanks. Now I know I need to declare variables that way. Thanks.
    – Migue
    Jun 2, 2022 at 14:19

2 Answers 2

2

This works perfectly, you just have to indicate the slice`s inital size:

func main() {
    var n int
    var sumScores float32 = 0

    fmt.Println("How many scores?") //ask user how many values
    fmt.Scan(&n)                    //get how many values
    scores := make([]uint, n, n)

    for i := 0; i < n; i++ {
        fmt.Printf("Scores: ") // ask for values

        fmt.Scan(&scores[i])                       // get values
        sumScores = sumScores + float32(scores[i]) // sum values
    }

    fmt.Printf("Average: %f\n", sumScores/float32(n)) //average value
}

for the learning purposes

The way you've declared the scores slice, it's just an empty slice so you can append to it first and then scan the number in newly generated position. (but this is certainly not the way for this specific problem)

func main() {
    var n int
    scores := []uint{}
    var sumScores float32 = 0

    fmt.Println("How many scores?") //ask user how many values
    fmt.Scan(&n)                    //get how many values

    for i := 0; i < n; i++ {
        fmt.Printf("Scores: ") // ask for values
        scores = append(scores, 0)
        fmt.Scan(&scores[i])                       // get values
        sumScores = sumScores + float32(scores[i]) // sum values
    }

    fmt.Printf("Average: %f\n", sumScores/float32(n)) //average value
}
0
0

scores := []uint{}

This is a slice literal, you should use Golang built-in function append with it.

Like @no0ob's second example does, or this:

func main() {
    var n int
    var tmpVal uint
    scores := []uint{}
    var sumScores float32 = 0

    fmt.Println("How many scores?") //ask user how many values
    fmt.Scan(&n)                    //get how many values

    for i := 0; i < n; i++ {
        fmt.Printf("Scores: ") // ask for values
        fmt.Scan(&tmpVal) // save input to tmpVal
        scores = append(scores, tmpVal) // append tmpVal to scores
        sumScores = sumScores + float32(scores[i]) // sum values
    }

    fmt.Printf("Average: %f\n", sumScores/float32(n)) //average value
}
1
  • Thank you! Yes, i am new to coding. So I am doing some exercices to learn how to use basics functions properly. Thanks!
    – Migue
    Jun 2, 2022 at 14:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.