locked
creating a matrix class to randomly fill in a matrix RRS feed

  • Question

  • I've made a class that should make a matrix and fill it in with random number 1-20. And made call for it in my main program but all that gets displayed is the type. I'm new to programming, and honestly don't see where i made the mistake.

           static Random random = new Random();
            const int MAX_W = 20;
            public int [,] Matrix1 { get; set; }

            public Matrix(int[,] matrix )
            {
                this.Matrix1 = matrix;
            }

            public Matrix(int aantalRijen, int aantalKolommen)
            {
                int[,] matrix = new int[aantalRijen, aantalKolommen];
                for (int i = 0; i < aantalRijen; i++)
                {
                    for (int j = 0; j < aantalKolommen; j++)
                    {
                        matrix[i, j] = random.Next(MAX_W);
                    }
                }
                this.Matrix1 = matrix;
            }

       
            public override string ToString()
            {
                string matrix = "";
                for (int i = 0; i <Matrix1.GetLength(0) ; i++)
                {
                    for (int j = 0; j <Matrix1.GetLength(1); j++)
                    {
                        matrix+= this.Matrix1[i,j].ToString()+"\t";
                    }
                    matrix += "\n";
                }
                matrix += "\n";
                return matrix;
            }

    main program

     static void Main(string[] args)
            {
                Matrix[,] matrix2 = new Matrix[2,3];
                Console.WriteLine(matrix2);

            }

    thanks in advance.

    Thursday, December 19, 2019 1:02 PM

Answers

  • Your declaration of the Matrix is incorrect. `Matrix[2,3]` creates a rectangular array 2x3 of Matrix instances. So the type of `matrix2` is `Matrix[,]` as you declared, hence a 2D array of Matrix elements. When you call ToString on types that don't override ToString (most) then you just get the type name.

    In your case your `Matrix` class is trying to wrap the 2D array so when you create instances you'll just be using `Matrix`.

    //This is a 2D array of Matrix elements
    var matrix2 = new Matrix[2, 3];
    
    //This just prints the type because that is what 2D arrays do
    Console.WriteLine(matrix2);
    
    //This declares an instance of your Matrix class
    Matrix matrix3 = new Matrix(2, 3);
    
    //This calls ToString on your Matrix instance
    Console.WriteLine(matrix3);
    
    I recommend that you take a look at one of the many sample matrix implementations floating around for C#. They can help guide you on how to implement this custom structure. It isn't as trivial as you might image and ultimately it tends to mimic the behavior of a 2D array so most people don't bother wrapping it unless you need to add functionality like transforms and whatnot.


    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, December 19, 2019 2:55 PM
  • Greetings Nauweaelrt.

    CoolDadTx has described the problem, which is that arrays don't implement ToString, so if you don't format the output yourself you will get the default which is the type name.

    The solution is to format the output yourself. There are many ways to do that. Here's one.

                for (int i = 0; i < aantalRijen; i++)
                {
                    for (int j = 0; j < aantalKolommen; j++)
                    {
                        Console.WriteLine("matrix[{0},{1}] = {2}", i, j, matrix[i, j]);
                    }
                }

    Thursday, December 19, 2019 10:11 PM

All replies

  • Your declaration of the Matrix is incorrect. `Matrix[2,3]` creates a rectangular array 2x3 of Matrix instances. So the type of `matrix2` is `Matrix[,]` as you declared, hence a 2D array of Matrix elements. When you call ToString on types that don't override ToString (most) then you just get the type name.

    In your case your `Matrix` class is trying to wrap the 2D array so when you create instances you'll just be using `Matrix`.

    //This is a 2D array of Matrix elements
    var matrix2 = new Matrix[2, 3];
    
    //This just prints the type because that is what 2D arrays do
    Console.WriteLine(matrix2);
    
    //This declares an instance of your Matrix class
    Matrix matrix3 = new Matrix(2, 3);
    
    //This calls ToString on your Matrix instance
    Console.WriteLine(matrix3);
    
    I recommend that you take a look at one of the many sample matrix implementations floating around for C#. They can help guide you on how to implement this custom structure. It isn't as trivial as you might image and ultimately it tends to mimic the behavior of a 2D array so most people don't bother wrapping it unless you need to add functionality like transforms and whatnot.


    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, December 19, 2019 2:55 PM
  • Greetings Nauweaelrt.

    CoolDadTx has described the problem, which is that arrays don't implement ToString, so if you don't format the output yourself you will get the default which is the type name.

    The solution is to format the output yourself. There are many ways to do that. Here's one.

                for (int i = 0; i < aantalRijen; i++)
                {
                    for (int j = 0; j < aantalKolommen; j++)
                    {
                        Console.WriteLine("matrix[{0},{1}] = {2}", i, j, matrix[i, j]);
                    }
                }

    Thursday, December 19, 2019 10:11 PM
  • Hi nauwelaert,

    Has your problem been solved?

    If so, please click "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, December 24, 2019 2:52 AM