Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 两个二维数组的C加法_C#_Arrays_For Loop_Multidimensional Array - Fatal编程技术网

C# 两个二维数组的C加法

C# 两个二维数组的C加法,c#,arrays,for-loop,multidimensional-array,C#,Arrays,For Loop,Multidimensional Array,好的,正如标题所说,我需要帮助找到一种将两个数组相加的简单方法。 这是我目前的代码: static void Main() { Console.Write("Enter Rows: "); int row = Convert.ToInt32(Console.ReadLine()); Console.Write("Enter Columns: "); int col = Convert.ToInt32(Console.ReadLine()); int[,]

好的,正如标题所说,我需要帮助找到一种将两个数组相加的简单方法。 这是我目前的代码:

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());
        }
    }
那么,我如何将这两个数组相加,并打印出结果呢。 谢谢

}


}

对两个数组求和后,需要使用新的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();
        }

对两个数组求和后,需要使用新的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();
        }

您是要将其中一个数组添加到另一个数组中,还是创建第三个数组?如果只是打印出来,是否需要将结果放入另一个数组中?您清楚地知道如何在数组的每个元素之间循环—请考虑需要为加法做些什么。还请注意,目前您根本没有使用b。。。我猜你是想进入第二个循环。你说添加是什么意思?相同位置或数组串联上的值之和?我猜他指的是矩阵加法。在int[,]b=newint[row,col]之后的第二个循环中;您正在将值设置为a。看来你一定是在尝试将其中一个添加到另一个,或者创建第三个数组?如果只是打印出来,是否需要将结果放入另一个数组中?您清楚地知道如何在数组的每个元素之间循环—请考虑需要为加法做些什么。还请注意,目前您根本没有使用b。。。我猜你是想进入第二个循环。你说添加是什么意思?相同位置或数组串联上的值之和?我猜他指的是矩阵加法。在int[,]b=newint[row,col]之后的第二个循环中;您正在将值设置为a。看来一定是博克。不要忘记更改第二个a[i,j]=Convert.ToInt32Console.ReadLine;to b[i,j]=Convert.ToInt32Console.ReadLine;好啊不要忘记更改第二个a[i,j]=Convert.ToInt32Console.ReadLine;to b[i,j]=Convert.ToInt32Console.ReadLine;
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");
    }   
}
 // 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();
        }