Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,923 questions

51,856 answers

573 users

How to fill a matrix with 1 and 0 in random locations with VB.NET

1 Answer

0 votes
Imports System

Public Class FillMatrixWith1And0InRandomLocations
    Const ROWS As Integer = 5
    Const COLS As Integer = 4

    Public Shared Sub Main(ByVal args As String())
        Dim matrix As Integer(,) = New Integer(4, 3) {}
		
        FillMatrixWithRandom0and1(matrix, ROWS, COLS)
		
        PrintMatrix(matrix, ROWS, COLS)
    End Sub

    Public Shared Sub FillMatrixWithRandom0and1(ByVal matrix As Integer(,), ByVal rows As Integer, ByVal cols As Integer)
        Dim rand As Random = New Random()

        For i As Integer = 0 To rows - 1
            For j As Integer = 0 To cols - 1
                matrix(i, j) = rand.Next(2)
            Next
        Next
    End Sub

	Public Shared Sub PrintMatrix(ByVal matrix As Integer(,), ByVal rows As Integer, ByVal cols As Integer)
        For i As Integer = 0 To rows - 1
            For j As Integer = 0 To cols - 1
                Console.Write(matrix(i, j) & " ")
            Next

            Console.WriteLine()
        Next
    End Sub
End Class



' run:
'
' 1 1 0 0 
' 1 0 1 0 
' 0 0 0 0 
' 1 1 0 0 
' 1 1 0 1 
'

 



answered Jan 25, 2025 by avibootz

Related questions

1 answer 62 views
1 answer 83 views
1 answer 79 views
1 answer 66 views
1 answer 71 views
1 answer 64 views
...