Advertisement
imedvedev

ООП 4

Mar 9th, 2014
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.83 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace WindowsFormsApplication3
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         public Form1()
  16.         {
  17.             InitializeComponent();
  18.         }
  19.  
  20.         private void button1_Click(object sender, EventArgs e)
  21.         {
  22.             textBox2.Text = "";
  23.             textBox3.Text = "";
  24.  
  25.             Random a = new Random();
  26.  
  27.             int n = Convert.ToInt32(textBox1.Text);
  28.             int[] mas = new int[100];
  29.             int[] mas1 = new int[100];
  30.  
  31.             //  заполняем массив
  32.             for (int i = 0; i < n; i++)
  33.             {
  34.                 if (a.Next(100) > 50) mas[i] = a.Next(5);
  35.                 else mas[i] = -a.Next(5);
  36.                 textBox2.Text = textBox2.Text + Convert.ToString(mas[i]) + "  ";
  37.             }
  38.  
  39.             // считаем наименьший модуль
  40.             int mod = 0;
  41.             int m = 0;
  42.             for (int i = 0; i < n; i++)
  43.             {
  44.                 m = Math.Abs(mas[i]);
  45.                 if (mod == 0)
  46.                 {
  47.                     mod = m;
  48.                 }
  49.                 else
  50.                 {
  51.                     if (m < mod)
  52.                     {
  53.                         mod = m;
  54.                     }
  55.                 }
  56.             }
  57.  
  58.             label2.Text = "минимальный по модулю элемент массива: " + Convert.ToString(mod);
  59.  
  60.             int amod = 0;
  61.             int go = 0;
  62.             foreach (int el in mas)
  63.             {
  64.                 if (go == 0)
  65.                 {
  66.                     if (el == 0)
  67.                     {
  68.                         go = 1;
  69.                     }
  70.                 }
  71.                 else
  72.                 {
  73.                     amod += Math.Abs(el);
  74.                 }
  75.             }
  76.  
  77.             label3.Text = "сумма модулей элементов массива,\nрасположенных после первого нуля: " + Convert.ToString(amod);
  78.  
  79.             // сортируем массив
  80.             int j = 0;
  81.             for (int i = 0; i < n; i++)
  82.             {
  83.                 if (i % 2 == 0)
  84.                 {
  85.                     mas1[j++] = mas[i];
  86.                 }
  87.             }
  88.  
  89.             // нечетные
  90.             for (int i = 0; i < n; i++)
  91.             {
  92.                 if (i % 2 != 0)
  93.                 {
  94.                     mas1[j++] = mas[i];
  95.                 }
  96.             }
  97.  
  98.             for (int i = 0; i < n; i++)
  99.             {
  100.                 textBox3.Text = textBox3.Text + Convert.ToString(mas1[i]) + "  ";
  101.             }
  102.            
  103.         }
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement