Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
ZeroMatrix
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { static int[,] mnArray1 = { { 1, 2, 3, 4 }, { 5, 6, 0, 4 }, { 0, 4, 9, 4 } }; static int[,] mnArray2 = { { 1, 2, 3, 4, 5, 6, 7}, { 5, 6, 0, 4, 4, 5, 6 }, { 0, 4, 9, 4, 5, 6, 9}, { 8, 4, 9, 4, 5, 6, 9}, { 8, 4, 9, 4, 5, 6, 9}}; static int[,] mnArray3 = { { 1, 2 }, { 0, 6 }}; public static void Main(string[] args) { Console.WriteLine("Initial Array:\n"); Print2DArray<int>(mnArray1); ZeroMatrix(mnArray1); Console.WriteLine("Zeroed Array:\n"); Print2DArray<int>(mnArray1); Console.WriteLine("Initial Array:\n"); Print2DArray<int>(mnArray2); ZeroMatrix(mnArray2); Console.WriteLine("Zeroed Array:\n"); Print2DArray<int>(mnArray2); Console.WriteLine("Initial Array:\n"); Print2DArray<int>(mnArray3); ZeroMatrix(mnArray3); Console.WriteLine("Zeroed Array:\n"); Print2DArray<int>(mnArray3); } static void ZeroMatrix(int[,] mnArray) { int mSize = mnArray.GetLength(0); int nSize = mnArray.GetLength(1); //Console.WriteLine($"mSize {mSize}, nSize {nSize}"); bool[] mArray = new bool[mSize]; // default false / no need to initialize bool[] nArray = new bool[nSize]; // default false / no need to initialize PopulatemArrayAndnArray(mnArray, mSize, nSize, mArray, nArray); ZeroMatrix(mnArray, mSize, nSize, mArray, true); ZeroMatrix(mnArray, nSize, mSize, nArray, false); } private static void ZeroMatrix(int[,] mnArray, int boolArraySize1, int boolArraySize2, bool[] boolArray, bool rowTraverse) { for (int i = 0; i < boolArraySize1; i++) { for (int j = 0; j < boolArraySize2; j++) { if (boolArray[i]) { if (rowTraverse) mnArray[i, j] = 0; else mnArray[j, i] = 0; } else { j = boolArraySize2; // early exit inner loop } } } } private static void PopulatemArrayAndnArray(int[,] mnArray, int mSize, int nSize, bool[] mArray, bool[] nArray) { for (int i = 0; i < mSize; i++) { for (int j = 0; j < nSize; j++) { mArray[i] = mArray[i] == true || mnArray[i, j] == 0 ? true : false; nArray[j] = nArray[j] == true || mnArray[i, j] == 0 ? true : false; } } } public static void Print2DArray<T>(T[,] matrix) { for (int i = 0; i < matrix.GetLength(0); i++) { for (int j = 0; j < matrix.GetLength(1); j++) { Console.Write(matrix[i, j] + "\t"); } Console.WriteLine(); } } } }
run
|
edit
|
history
|
help
0
test 1
random number generator using array
Příklad
LCM Q 3
Fibonacci recursion
test
regex: rozważania nad powielaniem grup za pomocą kwantyfikatora
Can a == true && a == false be true in C#?
sdfrgthyjuytrfdf
trees