Hello friends, Below program is for creating two 2D Array and do addition of them. You can do multiplication,substation as same way.
Hope it will be useful for you,
class Program
{
static void Main(string[] args)
{
Console.Write("Enter size of matrix - ");
int MatrixSize1 = Convert.ToInt32(Console.ReadLine());
int[,] Array_1 = new int [MatrixSize1, MatrixSize1];
int[,] Array_2 = new int[MatrixSize1, MatrixSize1];
int[,] Array_3 = new int[100, 100];
int i, j;
Console.WriteLine("\n");
Console.WriteLine("Enter values for matrix 1");
for(i = 0; i < MatrixSize1; i++)
{
for(j = 0; j < MatrixSize1; j++)
{
Console.Write("First Matrix element - [{0},{1}]:", i, j);
Array_1[i,j] = Convert.ToInt32(Console.ReadLine());
}
}
Console.WriteLine("\n");
for (i = 0; i < MatrixSize1; i++)
{
for (j = 0; j < MatrixSize1; j++)
{
Console.Write("Second Matrix element - [{0},{1}]:", i, j);
Array_2[i, j] = Convert.ToInt32(Console.ReadLine());
}
}
Console.WriteLine("\n");
Console.WriteLine("Values inserted in Matrix 1");
for (i = 0; i < MatrixSize1; i++)
{
Console.WriteLine("");
for (j = 0; j < MatrixSize1; j++)
{
Console.Write("{0}\t", Array_1[i, j]);
}
}
Console.WriteLine("\n");
Console.WriteLine("Values inserted in Matrix 2");
for (i = 0; i < MatrixSize1; i++)
{
Console.WriteLine("");
for (j = 0; j < MatrixSize1; j++)
{
Console.Write("{0}\t", Array_2[i, j]);
}
}
Console.WriteLine("\n");
Console.WriteLine("Sum of Two Matrix");
for (i = 0; i < MatrixSize1; i++)
{
for (j = 0; j < MatrixSize1; j++)
{
Array_3[i, j] = Array_1[i, j] + Array_2[i, j];
}
}
for (i = 0; i < MatrixSize1; i++)
{
Console.WriteLine("");
for (j = 0; j < MatrixSize1; j++)
{
Console.Write("{0}\t", Array_3[i, j]);
}
}
Console.ReadLine();
}
}
Output:
Enter size of matrix - 2
Enter values for matrix 1
First Matrix element - [0,0]:1
First Matrix element - [0,1]:2
First Matrix element - [1,0]:3
First Matrix element - [1,1]:4
Second Matrix element - [0,0]:1
Second Matrix element - [0,1]:2
Second Matrix element - [1,0]:3
Second Matrix element - [1,1]:4
Values inserted in Matrix 1
1 2
3 4
Values inserted in Matrix 2
1 2
3 4
Sum of Two Matrix
2 4
6 8
Hope it will be useful for you,
class Program
{
static void Main(string[] args)
{
Console.Write("Enter size of matrix - ");
int MatrixSize1 = Convert.ToInt32(Console.ReadLine());
int[,] Array_1 = new int [MatrixSize1, MatrixSize1];
int[,] Array_2 = new int[MatrixSize1, MatrixSize1];
int[,] Array_3 = new int[100, 100];
int i, j;
Console.WriteLine("\n");
Console.WriteLine("Enter values for matrix 1");
for(i = 0; i < MatrixSize1; i++)
{
for(j = 0; j < MatrixSize1; j++)
{
Console.Write("First Matrix element - [{0},{1}]:", i, j);
Array_1[i,j] = Convert.ToInt32(Console.ReadLine());
}
}
Console.WriteLine("\n");
for (i = 0; i < MatrixSize1; i++)
{
for (j = 0; j < MatrixSize1; j++)
{
Console.Write("Second Matrix element - [{0},{1}]:", i, j);
Array_2[i, j] = Convert.ToInt32(Console.ReadLine());
}
}
Console.WriteLine("\n");
Console.WriteLine("Values inserted in Matrix 1");
for (i = 0; i < MatrixSize1; i++)
{
Console.WriteLine("");
for (j = 0; j < MatrixSize1; j++)
{
Console.Write("{0}\t", Array_1[i, j]);
}
}
Console.WriteLine("\n");
Console.WriteLine("Values inserted in Matrix 2");
for (i = 0; i < MatrixSize1; i++)
{
Console.WriteLine("");
for (j = 0; j < MatrixSize1; j++)
{
Console.Write("{0}\t", Array_2[i, j]);
}
}
Console.WriteLine("\n");
Console.WriteLine("Sum of Two Matrix");
for (i = 0; i < MatrixSize1; i++)
{
for (j = 0; j < MatrixSize1; j++)
{
Array_3[i, j] = Array_1[i, j] + Array_2[i, j];
}
}
for (i = 0; i < MatrixSize1; i++)
{
Console.WriteLine("");
for (j = 0; j < MatrixSize1; j++)
{
Console.Write("{0}\t", Array_3[i, j]);
}
}
Console.ReadLine();
}
}
Output:
Enter size of matrix - 2
Enter values for matrix 1
First Matrix element - [0,0]:1
First Matrix element - [0,1]:2
First Matrix element - [1,0]:3
First Matrix element - [1,1]:4
Second Matrix element - [0,0]:1
Second Matrix element - [0,1]:2
Second Matrix element - [1,0]:3
Second Matrix element - [1,1]:4
Values inserted in Matrix 1
1 2
3 4
Values inserted in Matrix 2
1 2
3 4
Sum of Two Matrix
2 4
6 8
No comments:
Post a Comment
Please let me know if you have any feedback or doubts.