简体   繁体   English

从函数打印返回的二维数组

[英]printing the returned 2D array from a function

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:我有一个从函数 (c#) 返回的二维数组,我想打印它(以显示新矩阵)这里是代码:

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)? ' 部分(最后一行代码)打印整个数组(从乘法矩阵函数返回的 mat3)? I've tried to write Console.WriteLine(matrix.multiply_matrix(entery[0], entery[1])[0]);我试过写 Console.WriteLine(matrix.multiply_matrix(entery[0], entery[1])[0]); but it can't be done this way..但它不能这样做..

虽然我没有得到任何答案,但有人告诉我我可以创建一个对象(矩阵)而不是返回一个对象,并且它起作用了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM