C# 两个二维数组的相加
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15701344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 17:38:53 来源:igfitidea点击:
C# Addition of two 2-Dimensional Arrays
提问by LostStudent
Okay, So as the title says I need help finding a simple way of adding two arrays together. This is my code so far:
好的,正如标题所说,我需要帮助找到一种将两个数组相加的简单方法。到目前为止,这是我的代码:
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());
}
}
So, how would I add these two array together, and print out the result. Thanks.
那么,我如何将这两个数组相加,并打印出结果。谢谢。
采纳答案by Hossein Narimani Rad
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();
}
}
回答by Hemlata Gehlot
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 matrix\n");
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();
回答by BHASKAR DEKA GAU-C-14L-215
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");
}
}
}
}
回答by Sergio Galdino
After summing both arrays you need to go for a new for loop to print the new array
对两个数组求和后,您需要使用新的 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();
}