Advertisement
filin

laba_3_z_3_new

Oct 20th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.72 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Z_3
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Console.Title = "Laba3_3";
  14.             Console.WriteLine("Программа заменяет в массиве все вхождения отрицательных элементов на два идущих подряд элемента со значением 0.");
  15.             Console.Write("Введите число элементов массива; n= ");
  16.             int n = int.Parse(Console.ReadLine());
  17.             int[] arr = new int[n];
  18.             FillingArrayWithRule(arr);
  19.             DisplayArray(arr);
  20.             ReplaceOnZero(arr);
  21.             DisplayArray(arr);
  22.             Console.ReadKey();
  23.         }
  24.         static void FillingArrayWithRule(int[] arr)
  25.         {
  26.             Random Rand = new Random();
  27.             for (int i = 0; i < arr.Length; i++)
  28.             {
  29.                 arr[i] = Rand.Next(-50, 100);
  30.             }
  31.         }
  32.         static void ReplaceOnZero(int[] arr)
  33.         {
  34.             for (int i = 0; i < arr.Length; i++)
  35.             {
  36.                 if (arr[i] < 0)
  37.                 {
  38.                     arr[i] = 0;
  39.                     if (i != arr.Length - 1)
  40.                     {
  41.                         arr[i + 1] = 0;
  42.                         i++;
  43.                     }
  44.                 }
  45.             }
  46.         }
  47.         static void DisplayArray(int[] arr)
  48.         {
  49.             for (int i = 0; i < arr.Length; i++)
  50.             {
  51.                 Console.Write("{0} ", arr[i]);
  52.             }
  53.             Console.WriteLine();
  54.         }
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement