【发布时间】:2020-10-16 16:08:57
【问题描述】:
我有一个从函数 (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(**???**);
}
}
}
我应该在 '???' 部分(最后一行代码)中写什么来打印整个数组(从 multiply_matrix 函数返回的 mat3)? 我试过写 Console.WriteLine(matrix.multiply_matrix(entery[0], entery[1])[0]);但不能这样做..
【问题讨论】:
-
打印二维数组是一个在许多地方反复解决的问题。请尝试寻找解决方案,尝试自己编写代码,如果遇到问题,请告诉我们问题所在。
-
这能回答你的问题吗? How to print 2D array to console in C#
-
@tnw 你问错了
-
@tnw 我知道如何打印二维数组,问题是如何从函数返回的数组中获取元素(主要问题是我无法从函数)
-
公平地说,您的问题是以一种暗示您需要帮助打印数组的方式提出的,而不是如何在
Main中实际访问mat3。
标签: c# function multidimensional-array