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 C#

2 Answers

0 votes
using System;

internal class Program
{
	private static void changeRowColumn(int[][] matrix, int row, int col) {
		int rows = matrix.Length;
		int cols = matrix[0].Length;

		// -1 = different from the existing zeros

		for (int j = 0; j < cols; j++) {
			if (matrix[row][j] != 0) {
				matrix[row][j] = -1;
			}
		}

		for (int i = 0; i < rows; i++) {
			if (matrix[i][col] != 0) {
				matrix[i][col] = -1;
			}
		}
	}

	private static void changeBinaryMatrix(int[][] matrix) {
		int rows = matrix.Length;
		int cols = matrix[0].Length;

		if (rows == 0 || cols == 0) {
			return;
		}

		for (int i = 0; i < rows; i++) {
			for (int j = 0; j < cols; j++) {
				if (matrix[i][j] == 0) {
					changeRowColumn(matrix, i, j);
				}
			}
		}

		for (int i = 0; i < rows; i++) {
			for (int j = 0; j < cols; j++) {
				if (matrix[i][j] == -1) {
					matrix[i][j] = 0;
				}
			}
		}
	}

	private static void printMatrix(int[][] matrix) {
		int rows = matrix.Length;
		int cols = matrix[0].Length;

		for (int i = 0; i < rows; i++) {
			for (int j = 0; j < cols; j++) {
				Console.Write(matrix[i][j] + " ");
			}
			Console.Write("\n");
		}
	}
	public static void Main(string[] args)
	{
		int[][] matrix = new int[][]
		{
			new int[] {1, 1, 0, 1, 1, 1},
			new int[] {1, 1, 1, 1, 1, 1},
			new int[] {1, 1, 0, 1, 1, 1},
			new int[] {1, 1, 1, 1, 1, 1},
			new int[] {1, 0, 1, 1, 1, 1}
		};

		changeBinaryMatrix(matrix);

		printMatrix(matrix);
	}
}




/*
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
0 votes
using System;

internal class Program
{
	private static void changeRowColumn(int[,] matrix, int row, int col) {
		int rows = matrix.GetLength(0);
		int cols = matrix.GetLength(1);

		// -1 = different from the existing zeros

		for (int j = 0; j < cols; j++) {
			if (matrix[row, j] != 0) {
				matrix[row, j] = -1;
			}
		}

		for (int i = 0; i < rows; i++) {
			if (matrix[i, col] != 0) {
				matrix[i, col] = -1;
			}
		}
	}

	private static void changeBinaryMatrix(int[,] matrix) {
		int rows = matrix.GetLength(0);
		int cols = matrix.GetLength(1);

		if (rows == 0 || cols == 0) {
			return;
		}

		for (int i = 0; i < rows; i++) {
			for (int j = 0; j < cols; j++) {
				if (matrix[i, j] == 0) {
					changeRowColumn(matrix, i, j);
				}
			}
		}

		for (int i = 0; i < rows; i++) {
			for (int j = 0; j < cols; j++) {
				if (matrix[i, j] == -1) {
					matrix[i, j] = 0;
				}
			}
		}
	}

	private static void printMatrix(int[,] matrix) {
		int rows = matrix.GetLength(0);
		int cols = matrix.GetLength(1);

		for (int i = 0; i < rows; i++) {
			for (int j = 0; j < cols; j++) {
				Console.Write(matrix[i, j] + " ");
			}
			Console.Write("\n");
		}
	}
	public static void Main(string[] args)
	{
		int[,] matrix = 
		{
		    {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);
	}
}





/*
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
...