Passing a 2D slice to a Golang function argument - go

I'm trying to scan a matrix from the stdin and simply print it using following code.
package main
import (
"fmt"
)
func print2D(arr [][]int) {
for i:=0; i< len(arr); i++{
for j := 0; j< len(arr[0]); j++{
fmt.Printf("%d ", arr[i][j])
}
fmt.Println()
}
}
func main() {
var arr [6][6]int
for i:= 0 ; i < 6 ;i++ {
for j := 0; j< 6; j++{
fmt.Scanf("%d", &arr[i][j])
}
}
print2D(arr[:])
}
It throws the following error
./main.go:23: cannot use arr[:] (type [][6]int) as type [][]int in
argument to print2D
Is there a way to pass a 2D slice without defining sizes in the function arguments?

Try to write the data directly to the slice and pass it later to the function. Remember array and slices are different types. Moreover, the type [3]int is also different from [4]int (size matters).
package main
import (
"fmt"
)
func print2D(arr [][]int) {
for i := 0; i < len(arr); i++ {
for j := 0; j < len(arr[0]); j++ {
fmt.Printf("%d ", arr[i][j])
}
fmt.Println()
}
}
func main() {
var arr [][]int
for i := 0; i < 6; i++ {
tmp := make([]int, 6)
for j := 0; j < 6; j++ {
fmt.Scanf("%d", &tmp[j])
}
arr = append(arr, tmp)
}
print2D(arr)
}

Related

Generate random values for golange

I need a random password generator for a project, I need to make 10 of them. I found this function (random), but when I run it in a loop, it generates the same passwords. I don't know what's the problem.
func main() {
for i := 0; i < 10; i++ {
a := random()
fmt.Println(a)
}
}
func random() string {
rand.Seed(time.Now().UnixNano())
chars := []rune("abcdefghijklmnopqrstuvwxyz" + "0123456789")
length := 10
var b strings.Builder
for i := 0; i < length; i++ {
b.WriteRune(chars[rand.Intn(len(chars))])
}
str := b.String()
return str
}
Adapting your example code and refactoring it a bit:
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
rp := newRandPass(rand.New(rand.NewSource(time.Now().UnixNano())), 10, 10)
fmt.Println(rp.passwords())
}
type RandPass struct {
r *rand.Rand
num int
len int
}
func newRandPass(r *rand.Rand, num, len int) *RandPass {
return &RandPass{r, num, len}
}
func (r *RandPass) passwords() []string {
chars := []rune("abcdefghijklmnopqrstuvwxyz" + "0123456789")
passwords := make([]string, r.num)
for i := 0; i < r.num; i++ {
s := make([]rune, r.len)
for j := 0; j < r.len; j++ {
s[j] = chars[r.r.Intn(len(chars))]
}
passwords[i] = string(s)
}
return passwords
}
$ go run .
[rt97kzwjwe 5ziim05exh 40dfly93et v5tga5bwv9 avf2p2dpjx hsz4lca0jv 8r7bvvtu5l 9byf3mjq6r sdr2mpo54g cjx4mq6c0t]

Handling command drift on golang

when entering data for the array from the user side. After entering n = 5 the end a[0] is automatically assigned to 0 and is ignored to continue typing a[1]. I tried on other machines and replit but it seems to only happen on my computer. I also tried uninstalling and reinstalling golang but it didn't fix it
package main
import (
"fmt"
)
func main() {
var a = [100]int{}
var n int
fmt.Print("N = ")
fmt.Scanf("%v", &n)
for i := 0; i < n; i++ {
fmt.Printf("a[%v] = ", i)
fmt.Scanf("%v", &a[i])
}
for i := 0; i < n; i++ {
fmt.Printf("%v ", a[i])
}
fmt.Println()
}
This will solve the problem for you, it's important to debug in go language.
var a = make([]int, 100)
var n int = 5
fmt.Print("N = ")
fmt.Scanf("%v \n", &n)
for i := 0; i < n; i++ {
fmt.Printf("a[%v] = ", i)
_, err := fmt.Scanf("%v \n", &a[i])
if err != nil {
fmt.Println("error", err)
}
}
for i := 0; i < n; i++ {
fmt.Printf("%v ", a[i])
}
fmt.Println()
https://pkg.go.dev/fmt#pkg-overview

What is the correct terminating condition for swapping in bubble sort?

I am learning golang, and I am trying to work through writing bubblesort and working with pointers.
package main
import (
"fmt"
"math/rand"
)
func main() {
testTwo := make([]int, 10)
for i := 0; i <= len(testTwo)-1; i++ {
fmt.Print("\n")
testTwo[i] = rand.Intn(10)
}
for i := 0; i <= len(testTwo)-1; i++ {
for j := i + 1; j <= len(testTwo)-1; j++ {
testTwo[i], testTwo[i+1] = swap(testTwo[i], testTwo[i+1])
}
}
}
/*
Swaps the pointers of two adjacent elements in an array
*/
func swap(valOne, valTwo int) (int, int) {
valAddress := &valOne
valAddressTwo := &valTwo
if valOne <= valTwo {
temp_address := *valAddressTwo
*valAddressTwo = valOne
*valAddress = temp_address
} else {
temp_address := *valAddress
*valAddress = valTwo
*valAddressTwo = temp_address
}
return valOne, valTwo
}
This is an example of what is being done so far. The input slice might be
[4 1 2 9 8 4 1 5 7 6]. But it stops short of sorting everything.[1 4 9 2 4 8 5 1 6 7]. Is there another condition I should add or is there something wrong with how I am using the pointers in the swap function?
package main
import (
"fmt"
"math/rand"
)
func main() {
testTwo := make([]int, 10)
for i := 0; i <= len(testTwo)-1; i++ {
fmt.Print("\n")
testTwo[i] = rand.Intn(10)
}
for i := 0; i <= len(testTwo)-1; i++ {
for j := i + 1; j <= len(testTwo)-1; j++ {
if testTwo[i] > testTwo[j] {
// here we swap pointers
testTwo[i], testTwo[j] = swap(testTwo[i], testTwo[j])
}
}
}
fmt.Print(testTwo)
}
/*
GO always sends arguments by value. Pointers cannot be swapped here, unless we use *int
*/
func swap(valOne, valTwo int) (int, int) {
if valOne <= valTwo {
return valOne, valTwo
} else {
return valTwo, valOne
}
}
You forgot to compare 2 variables and you have mistaken using i+1 instead of j.
for i := 0; i <= len(testTwo)-1; i++ {
for j := i + 1; j <= len(testTwo)-1; j++ {
if testTwo[i] < testTwo[j] {
testTwo[i], testTwo[j] = swap(testTwo[i], testTwo[j])
}
}
}

"Matrix multiplication" using goroutines and channels

I have a university project for testing time difference for matrix multiplication when I use 1 goroutine, 2 goroutines, 3 and so on. I must use channels. My problem is that doesn't matter how many go routines I add time of compilation is almost always the same. Maybe some one can tell where is the problem. Maybe that sending is very long and it gives all the time. Code is given below
package main
import (
"fmt"
"math/rand"
"time"
)
const length = 1000
var start time.Time
var rez [length][length]int
func main() {
const threadlength = 1
toCalcRow := make(chan []int)
toCalcColumn := make(chan []int)
dummy1 := make(chan int)
dummy2 := make(chan int)
var row [length + 1]int
var column [length + 1]int
var a [length][length]int
var b [length][length]int
for i := 0; i < length; i++ {
for j := 0; j < length; j++ {
a[i][j] = rand.Intn(10)
b[i][j] = rand.Intn(10)
}
}
for i := 0; i < threadlength; i++ {
go Calc(toCalcRow, toCalcColumn, dummy1, dummy2)
}
start = time.Now()
for i := 0; i < length; i++ {
for j := 0; j < length; j++ {
row[0] = i
column[0] = j
for k := 0; k < length; k++ {
row[k+1] = a[i][j]
column[k+1] = b[i][k]
}
rowSlices := make([]int, len(row))
columnSlices := make([]int, len(column))
copy(rowSlices, row[:])
copy(columnSlices, column[:])
toCalcRow <- rowSlices
toCalcColumn <- columnSlices
}
}
dummy1 <- -1
for i := 0; i < length; i++ {
for j := 0; j < length; j++ {
fmt.Print(rez[i][j])
fmt.Print(" ")
}
fmt.Println(" ")
}
<-dummy2
close(toCalcRow)
close(toCalcColumn)
close(dummy1)
}
func Calc(chin1 <-chan []int, chin2 <-chan []int, dummy <-chan int, dummy1 chan<- int) {
loop:
for {
select {
case row := <-chin1:
column := <-chin2
var sum [3]int
sum[0] = row[0]
sum[1] = column[0]
for i := 1; i < len(row); i++ {
sum[2] += row[i] * column[i]
}
rez[sum[0]][sum[1]] = sum[2]
case <-dummy:
elapsed := time.Since(start)
fmt.Println("Binomial took ", elapsed)
dummy1 <- 0
break loop
}
}
close(dummy1)
}
You don't see a difference because preparing the data to pass to the go routines is your bottleneck. It's slower or as fast as performing the calc.
Passing a copy of the rows and columns is not a good strategy. This is killing the performance.
The go routines may read data directly from the input matrix that are read only. There is no possible race condition here.
Same for output. If a go routine computes the multiplication of a row and a column, it will write the result in a distinct cell. There is also no possible race conditions here.
What to do is the following. Define a struct with two fields, one for the row and one for the column to multiply.
Fill a buffered channel with all possible combinations of row and columns to multiply from (0,0) to (n-1,m-1).
The go routines, consume the structs from the channel, perform the computation and write the result directly into the output matrix.
You then also have a done channel to signal to the main go routine that the computation is done. When a go routine has finished processing the struct (n-1,m-1) it closes the done channel.
The main go routine waits on the done channel after it has written all structs. Once the done channel is closed, it prints the elapsed time.
We can use a waiting group to wait that all go routine terminated their computation.
You can then start with one go routine and increase the number of go routines to see the impact of the processing time.
See the code:
package main
import (
"fmt"
"math/rand"
"sync"
"time"
)
type pair struct {
row, col int
}
const length = 1000
var start time.Time
var rez [length][length]int
func main() {
const threadlength = 1
pairs := make(chan pair, 1000)
var wg sync.WaitGroup
var a [length][length]int
var b [length][length]int
for i := 0; i < length; i++ {
for j := 0; j < length; j++ {
a[i][j] = rand.Intn(10)
b[i][j] = rand.Intn(10)
}
}
wg.Add(threadlength)
for i := 0; i < threadlength; i++ {
go Calc(pairs, &a, &b, &rez, &wg)
}
start = time.Now()
for i := 0; i < length; i++ {
for j := 0; j < length; j++ {
pairs <- pair{row: i, col: j}
}
}
close(pairs)
wg.Wait()
elapsed := time.Since(start)
fmt.Println("Binomial took ", elapsed)
for i := 0; i < length; i++ {
for j := 0; j < length; j++ {
fmt.Print(rez[i][j])
fmt.Print(" ")
}
fmt.Println(" ")
}
}
func Calc(pairs chan pair, a, b, rez *[length][length]int, wg *sync.WaitGroup) {
for {
pair, ok := <-pairs
if !ok {
break
}
rez[pair.row][pair.col] = 0
for i := 0; i < length; i++ {
rez[pair.row][pair.col] += a[pair.row][i] * b[i][pair.col]
}
}
wg.Done()
}
Your code is quite difficult to follow (calling variables dummy1/dummy2 is confusing particularly when they get different names in Calc) and adding some comments would make it more easily understood.
Firstly a bug. After sending data to be calculated you dummy1 <- -1 and I believe you expect this to wait for all calculations to be complete. However that is not necessarily the case when you have multiple goroutines. The channel will be drained by ONE of the goroutines and the timing info printed out; other goroutines will still be running (and may not have finnished their calculations).
In terms of timing I suspect that the way you are sending data to the go routines will slow things down; you send the row and then the column; because the channels are not buffered the goroutine will block while waiting for the column (switching back to the main goroutine to send the column). This back and forth will slow the rate at which your goroutines get data and may well explain why adding extra goroutines has a limited impact (it also becomes dangerous if you use buffered channels).
I have refactored your code (note there may be bugs and its far from perfect!) into something that does show a difference (on my computer 1 goroutine = 10s; 5 = 7s):
package main
import (
"fmt"
"math/rand"
"sync"
"time"
)
const length = 1000
var start time.Time
var rez [length][length]int
// toMultiply will hold details of what the goroutine will be multiplying (one row and one column)
type toMultiply struct {
rowNo int
columnNo int
row []int
column []int
}
func main() {
const noOfGoRoutines = 5
// Build up a matrix of dimensions (length) x (length)
var a [length][length]int
var b [length][length]int
for i := 0; i < length; i++ {
for j := 0; j < length; j++ {
a[i][j] = rand.Intn(10)
b[i][j] = rand.Intn(10)
}
}
// Setup completed so start the clock...
start = time.Now()
// Start off threadlength go routines to multiply each row/column
toCalc := make(chan toMultiply)
var wg sync.WaitGroup
wg.Add(noOfGoRoutines)
for i := 0; i < noOfGoRoutines; i++ {
go func() {
Calc(toCalc)
wg.Done()
}()
}
// Begin the multiplication.
start = time.Now()
for i := 0; i < length; i++ {
for j := 0; j < length; j++ {
tm := toMultiply{
rowNo: i,
columnNo: j,
row: make([]int, length),
column: make([]int, length),
}
for k := 0; k < length; k++ {
tm.row[k] = a[i][j]
tm.column[k] = b[i][k]
}
toCalc <- tm
}
}
// All of the data has been sent to the chanel; now we need to wait for all of the
// goroutines to complete
close(toCalc)
wg.Wait()
fmt.Println("Binomial took ", time.Since(start))
// The full result should be in tz
for i := 0; i < length; i++ {
for j := 0; j < length; j++ {
//fmt.Print(rez[i][j])
//fmt.Print(" ")
}
//fmt.Println(" ")
}
}
// Calc - Multiply a row from one matrix with a column from another
func Calc(toCalc <-chan toMultiply) {
for tc := range toCalc {
var result int
for i := 0; i < len(tc.row); i++ {
result += tc.row[i] * tc.column[i]
}
// warning - the below should work in this case but be careful writing to global variables from goroutines
rez[tc.rowNo][tc.columnNo] = result
}
}

How does a Go function without return values modify data?

I don't understand how the bubbleSort() slice a be applied at the main().
I don't give a return a and don't write a global variable.
package main
import (
"fmt"
)
func bubbleSort(a []int) {
var temp int
for j := 0; j < len(a); j++ {
for i := 0; i < (len(a) - 1); i++ {
if a[i] > a[i+1] {
temp = a[i]
a[i] = a[i+1]
a[i+1] = temp
}
}
}
}
func inputNums() []int {
var input int
var number int
fmt.Scan(&input)
s := make([]int, input)
for i := 0; i < input; i++ {
fmt.Scan(&number)
s[i] = number
}
return s
}
func outputNums(b []int) {
for i := 0; i < len(b); i++ {
fmt.Print(b[i])
fmt.Print(" ")
}
}
func main() {
nums := inputNums()
bubbleSort(nums)
outputNums(nums)
}
src/runtime/slice.go
type slice struct {
array unsafe.Pointer
len int
cap int
}
In Go all arguments are passed by value. The slice descriptor, a struct, is passed by value as if by assignment. The slice descriptor contains a pointer to its underlying array.
func bubbleSort(a []int) receives the a argument by value but uses the pointer a.array to modify the underlying array elements.
References:
The Go Blog: Go Slices: usage and internals
The Go Programming Language Specification

Resources