好的,所以标题说我需要帮助找到一个简单的方法来将两个数组加在一起。 到目前为止,这是我的代码:
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());
}
}
那么,我如何将这两个数组加在一起,并打印出结果。 感谢。
答案 0 :(得分:4)
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();
}
}
答案 1 :(得分:0)
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();
答案 2 :(得分:0)
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");
}
}
}
答案 3 :(得分:0)
两个数组求和后,您需要进行新的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();
}