Matrix product with goroutines

Hello everyone, I’m coding my first go project and I don’t undersant my code errors
Enter the number of rows the first matrix: 2
Enter the number of columns the first matrix: 2
Enter the number of rows the second matrix: 2
Enter the number of columns the second matrix : 2
Enter the first matrix elements:
1 2 3 4

Enter the second matrix elements:
1 2 3 4

panic: runtime error: index out of range [2] with length 2

goroutine 18 [running]:
main.cas(0xc0000763f0, 0x2, 0x2, 0xc00008a090, 0x2, 0x2, 0x2, 0x2)
C:/Users/sabti/Documents/ProjetGo/code1.go:16 +0x148
created by main.results
C:/Users/sabti/Documents/ProjetGo/code1.go:58 +0x45d
panic: runtime error: index out of range [2] with length 2

goroutine 20 [running]:
main.cas(0xc0000763f0, 0x2, 0x2, 0xc00008a090, 0x2, 0x2, 0x2, 0x2)
C:/Users/sabti/Documents/ProjetGo/code1.go:16 +0x148
created by main.results
C:/Users/sabti/Documents/ProjetGo/code1.go:58 +0x45d
panic: runtime error: index out of range [2] with length 2

goroutine 19 [running]:
main.cas(0xc0000763f0, 0x2, 0x2, 0xc00008a090, 0x2, 0x2, 0x2, 0x2)
C:/Users/sabti/Documents/ProjetGo/code1.go:16 +0x148
created by main.results
C:/Users/sabti/Documents/ProjetGo/code1.go:58 +0x45d
exit status 2

Here is my code

package main

import (

"fmt"

)

var matrixA [][]int

var matrixB [][]int

var result [][]int

var numLigne, numColone int

var i, j, k, nbLine1, nbColumn1, nbLine2, nbColumn2, total, nbLigeR, nbColumnR int

func cas(matrix1 [][]int, matrix2 [][]int, numLine int, numColumn int) {

for i = 0; i < nbColumn1; i++ {

    for j = 0; j < nbLine2; j++ {

        total = total + matrix1[numLine][i]*matrix2[j][numColumn]

    }

}

result[numLine][numColumn] = total

}

func results() {

fmt.Print("Enter the number of rows the first matrix: ")

fmt.Scanln(&nbLine1)

fmt.Print("Enter the number of columns the first matrix: ")

fmt.Scanln(&nbColumn1)

fmt.Print("Enter the number of rows the second matrix: ")

fmt.Scanln(&nbLine2)

fmt.Print("Enter the number of columns the second matrix : ")

fmt.Scanln(&nbColumn2)

if nbColumn1 != nbLine2 {

    fmt.Println("Error: The matrix cannot be multiplied")

} else {

    matrixA = make([][]int, nbLine1)

    fmt.Println("Enter the first matrix elements: ")

    for i = 0; i < nbLine1; i++ {

        matrixA[i] = make([]int, nbColumn1)

        for j = 0; j < nbColumn1; j++ {

            fmt.Scanln(&matrixA[i][j])

        }

    }

    matrixB = make([][]int, nbLine2)

    fmt.Println("Enter the second matrix elements: ")

    for i = 0; i < nbLine2; i++ {

        matrixB[i] = make([]int, nbColumn2)

        for j = 0; j < nbColumn2; j++ {

            fmt.Scanln(&matrixB[i][j])

        }

    }

}

for i = 0; i <= 2; i++ {

    for j = 0; j <= 2; j++ {

        go cas(matrixA, matrixB, nbColumn1, nbLine2)

    }

}

for i = 0; i < 2; i++ {

    for j = 0; j < 2; j++ {

        fmt.Print(result[i][j], "\t")

    }

}

}

func main() {

results()

}

I have two comments here.

  1. Why cas(matrixA, matrixB, nbColumn1, nbLine2) is run concurrently? It doesn’t have to be and it shouldn’t be because you’re using the slice a few lines below and the slice may not (and in almost 100% won’t) be willed with the data you want
  2. in the cas function you’re trying to get data out of the slice. Take a look how it looks in debugger.

The element with index 2 doesn’t exist because indexes are 0 and 1.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.