C#采用双重循环实现A矩阵与B矩阵相加赋给C矩阵

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace practise
{
class Program
{
static void Main(string[] args)
{
//矩阵A
int[,] A = { { 1, 2, 3 }, { 3, 4, 5 }, { 1, 3, 5 } };
//矩阵B
int[,] B = { { 1, 2, 3 }, { 3, 4, 5 }, { 2, 4, 6 } };
//矩阵C
int[,] C = new int[3, 3];
//循环给C赋值
for (int i = 0; i < A.GetLength(0); i++)
{
for (int j = 0; j < B.GetLength(1); j++)
{
C[i, j] = A[i, j] + B[i, j];
}
}
Print(A);
Print(B);
Print(C);
Console.Read();
}
static void Print(int[,] arry)
{
Console.WriteLine("---------");
for (int i = 0; i < arry.GetLength(0); i++)
{
for (int j = 0; j < arry.GetLength(1); j++)
{
Console.Write(arry[i, j]);
Console.Write(" ");
}
Console.WriteLine();
}
Console.WriteLine("---------");
}
}
}

 

C#采用双重循环实现A矩阵与B矩阵相加赋给C矩阵

上一篇:工控随笔_C#连接PLC_之_C#入门_02_程序性结构和注释


下一篇:【8】Zookeeper脚本及API