添加两个矩阵

问题描述 投票:0回答:1

我遇到一个问题“索引超出了数组的范围。”并且不明白问题出在哪里

static void Main(string[] args)
{
Console.WriteLine("If you want add elemenets of matrix, it should 
have same number of rows and columns\n");
Console.Write("Enter number of rows FOR FIRST NATRIX: ");
int rows = Convert.ToInt32(Console.ReadLine());

Console.Write("Enter number of columns FOR FIRST NATRIX: ");
int columns = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("---------------------------");

int[,] matrix = new int[rows, columns];

Random random = new Random();

for (int i = 0; i < matrix.GetLength(0); i++)
{
    for (int j = 0; j < matrix.GetLength(1); j++)
    {
        matrix[i, j] = random.Next(111);
    }
}

for (int i = 0; i < matrix.GetLength(0); i++)
{
    for (int j = 0; j < matrix.GetLength(1); j++)
    {
        Console.Write(matrix[i, j] + "\t");
    }
    Console.WriteLine();
}

Console.Write("Enter number of rows FOR SECOND NATRIX: ");
rows = Convert.ToInt32(Console.ReadLine());

Console.Write("Enter number of columns FOR SECOND NATRIX: ");
columns = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("---------------------------");

int[,] matrix1 = new int[rows, columns];

Random random1 = new Random();

for (int i = 0; i < matrix.GetLength(0); i++)
{
    for (int j = 0; j < matrix.GetLength(1); j++)
    {
        matrix[i, j] = random1.Next(111);
    }
}

for (int i = 0; i < matrix.GetLength(0); i++)
{
    for (int j = 0; j < matrix.GetLength(1); j++)
    {
        Console.Write(matrix[i, j] + "\t");
    }
    Console.WriteLine();
}

double[,] Sum = new double[rows, columns];

for (int i = 0; i < rows; i++)
{
    for (int j = 0; j < columns; j++)
    {
        Sum[i, j] = matrix[i, j] + matrix1[i, j];
    }
    Console.WriteLine();
}

for (int i = 0; i < rows; i++)
{
    for (int j = 0; i < columns; j++)
    {
        Console.WriteLine($"Sum: {Sum[i, j]}");
    }
    Console.WriteLine();
}

}

c# arrays .net matrix multidimensional-array
1个回答
0
投票

请不要重复自己,提取方法;我们想要创建、求和并打印矩阵:

private static int[,] CreateRandomMatrix(int rows, int columns, int scale) {
  int[,] matrix = new int[rows, columns];

  for (int r = 0; r < matrix.GetLength(0); ++r)
    for (int c = 0; c < matrix.GetLength(1); ++c)
      matrix[r, c] = Random.Shared.Next(scale);

  return matrix;
}

private static double[,] SumMatrix(int[,] left, int[,] right) {
  double[,] result = new double[
    Math.Min(left.GetLength(0), right.GetLength(0)), 
    Math.Min(left.GetLength(1), right.GetLength(1))];

  for (int r = 0; r < result.GetLength(0); ++r)
    for (int c = 0; c < result.GetLength(1); ++c)
      result = (double) left[r, c] + right[r, c];

  return result;
}

private static void PrintMatrix<T>(T[,] matrix) {
  for (int r = 0; r < matrix.GetLength(0); ++r) {
    if (r > 0) Console.WriteLine();

    for (int c = 0; c < matrix.GetLength(1); ++c) {
      if (c > 0) Console.Write("\t");

      Console.Write(matrix[r, c]);
    }
  } 
}

然后就可以简单地玩

...
int[,] matrix = CreateRandomMatrix(rows, columns, 111);

PrintMatrix(matrix);

...

int[,] matrix1 = CreateRandomMatrix(rows, columns, 111);

PrintMatrix(matrix1);

var Sum = SumMatrix(matrix, matrix1);

PrintMatrix(Sum);
© www.soinside.com 2019 - 2024. All rights reserved.