Guest User

Untitled

a guest
Feb 25th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. using System;
  2. using System.Windows.Forms;
  3.  
  4. namespace SumInTwoDimensionalArray_WinForms {
  5. public partial class Form1 : Form
  6. {
  7. public Form1()
  8. {
  9. InitializeComponent();
  10. }
  11.  
  12. private void button1_Click(object sender, EventArgs e)
  13. {
  14. try
  15. {
  16. int num = Convert.ToInt32(textBox1.Text);
  17. textBox2.Clear();
  18. textBox3.Clear();
  19. if (num < 0)
  20. {
  21. MessageBox.Show("The number of elements in array cannot be less than 0!", "Error");
  22. textBox1.Clear();
  23. }
  24. else if (num == 0)
  25. {
  26. MessageBox.Show("Your array is empty!", "Error");
  27. textBox1.Clear();
  28. }
  29. else
  30. {
  31. int[,] arr1 = new int[num, num];
  32. Random rand = new Random();
  33. for (int i = 0; i < num; i++)
  34. {
  35. for (int j = 0; j < num; j++)
  36. {
  37. arr1[i, j] = rand.Next(-10, 10);
  38. }
  39. }
  40. textBox2.Text = "";
  41. int rows = arr1.GetUpperBound(0) + 1;
  42. int columns = arr1.Length / rows;
  43. for (int i = 0; i < rows; i++)
  44. {
  45. for (int j = 0; j < columns; j++)
  46. {
  47. textBox2.Text += arr1[i, j] + "t";
  48. }
  49. textBox2.Text += Environment.NewLine;
  50. }
  51.  
  52. double[] arr2 = new double[num];
  53. for (int i = 0; i < num; i++)
  54. {
  55. double sum = 0;
  56. for (int j = 0; j < num; j++)
  57. {
  58. sum += Math.Pow(-1, i + j) * arr1[i, j];
  59. }
  60. arr2[i] = sum;
  61. }
  62.  
  63. textBox3.Text = "";
  64. for (int i = 0; i < arr2.Length; i++)
  65. {
  66. textBox3.Text += arr2[i] + " ";
  67. }
  68. textBox1.Clear();
  69. }
  70. }
  71. catch(FormatException)
  72. {
  73. MessageBox.Show("The input is improper!", "Error");
  74. textBox1.Clear();
  75. }
  76. }
  77. } }
Add Comment
Please, Sign In to add comment