C#两个二维数组的加法



好的,正如标题所说,我需要帮助找到一种将两个数组添加在一起的简单方法。这是我迄今为止的代码:

static void Main()
{
    Console.Write("Enter Rows: ");
    int row = Convert.ToInt32(Console.ReadLine());
    Console.Write("Enter Columns: ");
    int col = Convert.ToInt32(Console.ReadLine());
    int[,] a = new int[row, col];
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {
            Console.Write("Enter Matrix({0},{1}): ", i, j);
            a[i, j] = Convert.ToInt32(Console.ReadLine());
        }
    }
    int[,] b = new int[row, col];
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {
            Console.Write("Enter Matrix({0},{1}): ", i, j);
            a[i, j] = Convert.ToInt32(Console.ReadLine());
        }
    }

那么,我该如何将这两个数组相加,并打印出结果呢。谢谢

static void Main()
{
    // Your code
    int[,] result = new int[row, col];
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {
            result [i, j] = a[i,j] + b[i,j];
            Console.Write(result[i, j] + " ");
        }
        Console.WriteLine();
    }
}
int m, n, c, d;
int[,] first = new int[10, 10];
int[,] second = new int[10, 10];
int[,] sum = new int[10, 10];
Console.WriteLine("Enter the number of rows and columns of matrix");
m = Convert.ToInt16(Console.ReadLine());
n = Convert.ToInt16(Console.ReadLine());
Console.WriteLine("nEnter the elements of first matrixn");
for (c = 0; c < m; c++)
    for (d = 0; d < n; d++)
    {
        Console.WriteLine("Enter Element [" + c + " , " + d + "]");
        first[c, d] = Convert.ToInt16(Console.ReadLine());
    }
Console.WriteLine("Enter the elements of second matrix");
for (c = 0; c < m; c++)
    for (d = 0; d < n; d++)
    {
        Console.WriteLine("Enter Element [" + c + " , " + d + "]");
        second[c, d] = Convert.ToInt16(Console.ReadLine());
    }
for (c = 0; c < m; c++)
    for (d = 0; d < n; d++)
        sum[c, d] = first[c, d] + second[c, d];
Console.WriteLine("Sum of entered matrices:-");
for (c = 0; c < m; c++)
{
    for (d = 0; d < n; d++)
        Console.Write(" " + sum[c, d]);
    Console.WriteLine();
}
Console.ReadKey();
using System;
class matrixAdd
{
public static void Main()
{
    int [,] mat1 = new int[3,3];
    int [,] mat2 = new int[3,3];
    int [,] addition = new int[3,3];
    int i, j;
    Console.WriteLine("Enter the elements of the matrix1: ");
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            mat1[i,j] = Convert.ToInt32(Console.ReadLine());
        }
    }
    Console.WriteLine("Entered elements of the matrix1: ");
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            Console.Write("{0} ", mat1[i,j]);
        }
        Console.Write("n");
    }
    Console.WriteLine("Enter the elements of the matrix2: ");
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            mat2[i,j] = Convert.ToInt32(Console.ReadLine());
        }
    }
    Console.WriteLine("Entered elements of the matrix2: ");
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            Console.Write("{0} ", mat2[i,j]);
        }
        Console.Write("n");
    }
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            addition[i,j] = mat1[i,j] + mat2[i,j];
        }
    }
    Console.WriteLine("Addition of the matrix1 and matrix2: ");
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            Console.Write("{0} ", addition[i,j]);
        }
        Console.Write("n");
    }   
}

}

对两个数组求和后,需要进行一个新的for循环来打印新的数组

 // your code
 int[,] result = new int[row, col];
        for (int i = 0; i < row; i++)
        {
            for (int j = 0; j < col; j++)
            {
                result[i, j] = a[i, j] + b[i, j];
            }
        }
        for (int i = 0; i < row; i++)
        {
            for (int j = 0; j < col; j++)
            {
                Console.Write(result[i, j] + " ");
            }
            Console.WriteLine();
        }

相关内容

  • 没有找到相关文章

最新更新