Program to find the sum of two 2D matrices.. Each element of the matrix to be entered by the user using textbox.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Two_D_Array
{
public partial class Form1 : Form
{
int row, col;// size of first array
int row1, col1; // size of second array
int i = 0, j = 0;
int k = 0, l = 0;
int m = 0, n = 0;
int[,] a;
int[,] a1;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
}
private void label4_Click(object sender, EventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
row1 = Convert.ToInt32(textBox7.Text);
col1 = Convert.ToInt32(textBox6.Text);
a1 = new int[row1, col1];
}
private void button3_Click(object sender, EventArgs e)
{
try
{
if (m < row1)
{
if (n < col1)
{
a1[m, n] = Convert.ToInt32(textBox5.Text);
textBox5.Text = "";
listBox2.Items.Add(a1[m, n]);
textBox8.Text += a1[m, n].ToString() + "\t";
n++;
}
else
{
m++;
n = 0;
textBox8.Text += "\n";
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void button2_Click_1(object sender, EventArgs e)
{
try
{
if (i < row)
{
if (j < col)
{
a[i, j] = Convert.ToInt32(textBox3.Text);
textBox3.Text = "";
listBox1.Items.Add(a[i, j]);
textBox4.Text += a[i, j].ToString() + "\t";
j++;
}
else
{
i++;
j = 0;
textBox4.Text += "\n";
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void button1_Click_1(object sender, EventArgs e)
{
row = Convert.ToInt32(textBox1.Text);
col = Convert.ToInt32(textBox2.Text);
a = new int[row, col];
//MessageBox.Show("size is set");
}
private void textBox9_TextChanged(object sender, EventArgs e)
{
}
private void button5_Click(object sender, EventArgs e)
{
try
{
int[,] sum = new int[row, col];
if (row == row1 && col == col1)
{
if (k < row)
{
if (l < col)
{
sum[k, l] = a[k, l] + a1[k, l];
textBox9.Text += sum[k, l].ToString() + "\t";
l++;
}
else
{
k++;
l = 0;
textBox9.Text += "\n";
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Two_D_Array
{
public partial class Form1 : Form
{
int row, col;// size of first array
int row1, col1; // size of second array
int i = 0, j = 0;
int k = 0, l = 0;
int m = 0, n = 0;
int[,] a;
int[,] a1;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
}
private void label4_Click(object sender, EventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
row1 = Convert.ToInt32(textBox7.Text);
col1 = Convert.ToInt32(textBox6.Text);
a1 = new int[row1, col1];
}
private void button3_Click(object sender, EventArgs e)
{
try
{
if (m < row1)
{
if (n < col1)
{
a1[m, n] = Convert.ToInt32(textBox5.Text);
textBox5.Text = "";
listBox2.Items.Add(a1[m, n]);
textBox8.Text += a1[m, n].ToString() + "\t";
n++;
}
else
{
m++;
n = 0;
textBox8.Text += "\n";
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void button2_Click_1(object sender, EventArgs e)
{
try
{
if (i < row)
{
if (j < col)
{
a[i, j] = Convert.ToInt32(textBox3.Text);
textBox3.Text = "";
listBox1.Items.Add(a[i, j]);
textBox4.Text += a[i, j].ToString() + "\t";
j++;
}
else
{
i++;
j = 0;
textBox4.Text += "\n";
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void button1_Click_1(object sender, EventArgs e)
{
row = Convert.ToInt32(textBox1.Text);
col = Convert.ToInt32(textBox2.Text);
a = new int[row, col];
//MessageBox.Show("size is set");
}
private void textBox9_TextChanged(object sender, EventArgs e)
{
}
private void button5_Click(object sender, EventArgs e)
{
try
{
int[,] sum = new int[row, col];
if (row == row1 && col == col1)
{
if (k < row)
{
if (l < col)
{
sum[k, l] = a[k, l] + a1[k, l];
textBox9.Text += sum[k, l].ToString() + "\t";
l++;
}
else
{
k++;
l = 0;
textBox9.Text += "\n";
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
No comments:
Post a Comment