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,990 questions

51,935 answers

573 users

How to change all elements of row i and column j in a binary matrix to 0 if cell[i, j] is 0 with VB.NET

1 Answer

0 votes
Imports System

Public Class Program
    Public Shared Sub changeRowColumn(ByVal matrix As Integer(,), ByVal row As Integer, ByVal col As Integer)
        Dim rows As Integer = matrix.GetLength(0)
        Dim cols As Integer = matrix.GetLength(1)
		
		' // -1 = different from the existing zeros
		
        For j As Integer = 0 To cols - 1
            If matrix(row, j) <> 0 Then
                matrix(row, j) = -1
            End If
        Next

        For i As Integer = 0 To rows - 1
            If matrix(i, col) <> 0 Then
                matrix(i, col) = -1
            End If
        Next
    End Sub

    Public Shared Sub changeBinaryMatrix(ByVal matrix As Integer(,))
        Dim rows As Integer = matrix.GetLength(0)
        Dim cols As Integer = matrix.GetLength(1)

        If rows = 0 OrElse cols = 0 Then
            Return
        End If

        For i As Integer = 0 To rows - 1
            For j As Integer = 0 To cols - 1
                If matrix(i, j) = 0 Then
                    changeRowColumn(matrix, i, j)
                End If
            Next
        Next

        For i As Integer = 0 To rows - 1
            For j As Integer = 0 To cols - 1
                If matrix(i, j) = -1 Then
                    matrix(i, j) = 0
                End If
            Next
        Next
    End Sub

    Public Shared Sub printMatrix(ByVal matrix As Integer(,))
        Dim rows As Integer = matrix.GetLength(0)
        Dim cols As Integer = matrix.GetLength(1)

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

		Console.Write(Environment.NewLine)
        Next
    End Sub

    Public Shared Sub Main(ByVal args As String())
        
		Dim matrix As Integer(,) = {
        	{1, 1, 0, 1, 1, 1},
        	{1, 1, 1, 1, 1, 1},
        	{1, 1, 0, 1, 1, 1},
        	{1, 1, 1, 1, 1, 1},
        	{1, 0, 1, 1, 1, 1}}
        	
			changeBinaryMatrix(matrix)
        	
			printMatrix(matrix)
    End Sub
End Class




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

 



answered Jan 22, 2024 by avibootz
...