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.

40,026 questions

51,982 answers

573 users

How to initialize matrix in C#

5 Answers

0 votes
using System;

public class Program
{
    static void print_matrix(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.WriteLine();
        }
    }
    
    public static void Main(string[] args)
    {
        int[,] matrix = new int[,] {
            { 1, 2, 3 },
            { 4, 5, 6 }
        };
        
        print_matrix(matrix);
    }
}



/*
run:

1 2 3 
4 5 6 

*/

 



answered Feb 24, 2024 by avibootz
0 votes
using System;
 
public class Program
{
    static void print_matrix(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.WriteLine();
        }
    }
     
    public static void Main(string[] args)
    {
        int[,] matrix = new int[3, 3];
        int rows = matrix.GetLength(0);
        int cols = matrix.GetLength(1);

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                matrix[i, j] = i * 3 + j + 1; 
            }
        }
         
        print_matrix(matrix);
    }
}
 
 
 
/*
run:
 
1 2 3 
4 5 6 
7 8 9 
 
*/

 



answered Feb 24, 2024 by avibootz
0 votes
using System;
 
public class Program
{
    static void print_matrix(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("{0, 3}", matrix[i, j]);
            }
            Console.WriteLine();
        }
    }
     
    public static void Main(string[] args)
    {
        int[,] matrix = new int[3, 4];
        int rows = matrix.GetLength(0);
        int cols = matrix.GetLength(1);

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                matrix[i, j] = i * 3 + j + 1; 
            }
        }
         
        print_matrix(matrix);
    }
}
 
 
 
/*
run:
 
  1  2  3  4
  4  5  6  7
  7  8  9 10
 
*/

 



answered Feb 24, 2024 by avibootz
0 votes
using System;
 
public class Matrix
{
    private int[,] _matrix;
    private int rows, cols;

    public Matrix(int _rows, int _cols) {
        _matrix = new int[_rows, _cols];
        rows = _matrix.GetLength(0);
        cols = _matrix.GetLength(1);
    }

    public void InitializeWithZeros() {
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < rows; j++) {
                _matrix[i, j] = 0;
            }
        }
    }
    
    public void printMatrix() {
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                Console.Write("{0, 2}", _matrix[i, j]);
            }
            Console.WriteLine();
        }
    }

    public static void Main(string[] args)
    {
        Matrix m = new Matrix(3, 4);
        
        m.InitializeWithZeros();
        
        m.printMatrix();
    }
}
 
 
 
/*
run:
 
 0 0 0 0
 0 0 0 0
 0 0 0 0
 
*/

 



answered Feb 24, 2024 by avibootz
0 votes
using System;
 
public class Program
{
    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.WriteLine();
        }
    }

    public static void Main(string[] args)
    {
        int[][] matrix = new int[][]
        {
            new int[] {7, 9, 4, 3, 1},
            new int[] {4, 2, 9, 1, 3},
            new int[] {4, 7, 8, 6, 2},
            new int[] {0, 6, 9, 5, 4}
        };
        
        printMatrix(matrix);
    }
}
 
 
 
/*
run:
 
7 9 4 3 1 
4 2 9 1 3 
4 7 8 6 2 
0 6 9 5 4 
 
*/

 



answered Feb 24, 2024 by avibootz

Related questions

1 answer 82 views
82 views asked Feb 24, 2024 by avibootz
2 answers 194 views
1 answer 53 views
1 answer 124 views
...