0

I have a 2d array returned from a function (c#) and I want to print it (to show the new matrix) here is the code:

using System;
using System.Dynamic;

namespace Quera
{
    class Matrix
    {
        private int r1;
        private int c1;
        private int[,] mat1;

        public int[,] multiply_matrix(int r2, int c2)
        {
            int[,] mat2 = new int[r2, c2];
            int[,] mat3 = new int[r1, c2];

            // Enter data mat2
            for (int i = 0; i < r2; i++)
            {
                for (int j = 0; j < c2; j++)
                {
                    mat2[i, j] = Console.Read();
                }
            }
            // Multiply
            if (c1 != r2)
            {
               Console.WriteLine("Matrix multiplication not possible");
            }
            else
            {
                for (int i = 0; i < r1; i++)
                {
                    for (int j = 0; j < c2; j++)
                    {
                        mat3[i, j] = 0;
                        for (int k = 0; k < c1; k++)
                        {
                            mat3[i, j] += mat1[i, k] * mat2[k, j];
                        }
                    }
                }
            }

            return mat3;
        }

        public Matrix(int r, int c)
        {
            mat1 = new int[r, c];
            r1 = r;
            c1 = c;

            // Enter data
            for (int i = 0; i < r1; i++)
            {
                for (int j = 0; j < c1; j++)
                {
                    mat1[i, j] = Console.Read();
                }
            }
        }
    }
    class Program
    {
       static void Main(string[] args)
        {
            string[] tokens = Console.ReadLine().Split();
            int[] enteries = new int[2];
            enteries[0] = Convert.ToInt32(tokens[0]);
            enteries[1] = Convert.ToInt32(tokens[1]);

            string[] token = Console.ReadLine().Split();
            int[] entery = new int[2];
            entery[0] = Convert.ToInt32(token[0]); // you have to pass it to multiply_matrix
            entery[1] = Convert.ToInt32(token[1]); // you have to pass it to multiply_matrix
            Matrix matrix = new Matrix(enteries[0], enteries[1]);

            Console.WriteLine(**???**);
        }
    }
}

what should I write in '???' section (last line of code) to print the whole array (mat3 returned from multiply_matrix function)? I've tried to write Console.WriteLine(matrix.multiply_matrix(entery[0], entery[1])[0]); but it can't be done this way..

Elitheria
  • 65
  • 1
  • 12
  • 1
    Printing a 2d array is a problem solved repeatedly in numerous places. Please try searching for a solution, try to code it yourself, and let us know what the problem is if you run into one. – tnw Oct 16 '20 at 16:12
  • 1
    Does this answer your question? [How to print 2D array to console in C#](https://stackoverflow.com/questions/24094093/how-to-print-2d-array-to-console-in-c-sharp) – tnw Oct 16 '20 at 16:13
  • @tnw You got my question wrong – Elitheria Oct 18 '20 at 05:56
  • @tnw I know how to print a 2d array , the problem is how can I get the elements from an array that is returned by a function (the main problem is that I can't get anything from the function) – Elitheria Oct 18 '20 at 05:59
  • To be fair, your question is asked in a way that suggests you need help printing an array, not how to actually access `mat3` in `Main`. – tnw Oct 19 '20 at 14:30

1 Answers1

0

Although I didn't get any answers somebody told me I can create an object(matrix) instead of returning one and it worked

Elitheria
  • 65
  • 1
  • 12