CongMatran
using System;
namespace matran
{
public class matran
{
public void tao(int[,] MT)
{
Random r = new Random();
for (int i = 0; i < rows; i++)
for (int j = 0; j < columns; j++)
MT[i, j] = r.Next(100);
}
public void xuat(int[,] MT)
{
for (int i = 0; i < rows; i++)
for (int j = 0; j < columns; j++)
Console.WriteLine("\t\tPhan tu [ {0},{1} ]= {2} \t", i, j, MT[i, j]);
}
public void cong(int[,] A, int[,] B, int[,] C)
{
for (int i = 0; i < rows; i++)
for (int j = 0; j < columns; j++)
C[i, j] = A[i, j] + B[i, j];
}
public int Rows
{
get { return rows; }
set { rows = value; }
}
public int Columns
{
get { return columns; }
set { columns = value; }
}
private int rows, columns;
}
public class Tester
{
static void Main(string[] args)
{
matran mt = new matran();
Console.Write("Nhap so cot: ");
mt.Columns = int.Parse(Console.ReadLine());
Console.Write("Nhap vao so dong: ");
mt.Rows = int.Parse(Console.ReadLine());
int[,] A = new int[mt.Rows, mt.Columns];
int[,] B = new int[mt.Rows, mt.Columns];
int[,] C = new int[mt.Rows, mt.Columns];
Console.WriteLine(" Khoi tao ma tran A...");
mt.tao(A);
Console.WriteLine(" Ma tran A: ");
mt.xuat(A);
Console.WriteLine(" Khoi tao ma tran B...");
mt.tao(B);
Console.WriteLine(" Ma tran B: ");
mt.xuat(B);
Console.WriteLine("Tong hai ma tran A va B: ");
mt.cong(A, B, C);
mt.xuat(C);
}
}
}
Bạn đang đọc truyện trên: Truyen247.Pro