Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Windows.Forms;
- namespace SumInTwoDimensionalArray_WinForms {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- try
- {
- int num = Convert.ToInt32(textBox1.Text);
- textBox2.Clear();
- textBox3.Clear();
- if (num < 0)
- {
- MessageBox.Show("The number of elements in array cannot be less than 0!", "Error");
- textBox1.Clear();
- }
- else if (num == 0)
- {
- MessageBox.Show("Your array is empty!", "Error");
- textBox1.Clear();
- }
- else
- {
- int[,] arr1 = new int[num, num];
- Random rand = new Random();
- for (int i = 0; i < num; i++)
- {
- for (int j = 0; j < num; j++)
- {
- arr1[i, j] = rand.Next(-10, 10);
- }
- }
- textBox2.Text = "";
- int rows = arr1.GetUpperBound(0) + 1;
- int columns = arr1.Length / rows;
- for (int i = 0; i < rows; i++)
- {
- for (int j = 0; j < columns; j++)
- {
- textBox2.Text += arr1[i, j] + "t";
- }
- textBox2.Text += Environment.NewLine;
- }
- double[] arr2 = new double[num];
- for (int i = 0; i < num; i++)
- {
- double sum = 0;
- for (int j = 0; j < num; j++)
- {
- sum += Math.Pow(-1, i + j) * arr1[i, j];
- }
- arr2[i] = sum;
- }
- textBox3.Text = "";
- for (int i = 0; i < arr2.Length; i++)
- {
- textBox3.Text += arr2[i] + " ";
- }
- textBox1.Clear();
- }
- }
- catch(FormatException)
- {
- MessageBox.Show("The input is improper!", "Error");
- textBox1.Clear();
- }
- }
- } }
Add Comment
Please, Sign In to add comment