Advertisement
RamGaal

Homework 3 ex.5

May 19th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.56 KB | None | 0 0
  1. using System;
  2.  
  3. class MainClass
  4. {
  5.     public static void Main(string[] args)
  6.     {
  7.         int[] matrix = new int[30];
  8.         Random rnd = new Random();
  9.         int[] localmax = new int[0];
  10.         bool maxfound = false;
  11.  
  12.         for (int i = 0; i < matrix.Length; i++)
  13.         {
  14.             matrix[i] = rnd.Next(0, 100);
  15.             Console.Write(matrix[i] + " ");
  16.         }
  17.         Console.WriteLine();
  18.         Console.WriteLine("Максимумы: ");
  19.  
  20.         for (int i = 0; i < matrix.Length ; i++)
  21.         {
  22.             switch (i)
  23.             {
  24.                 case 0:
  25.                     if (matrix[i] > matrix[i + 1])
  26.                     {
  27.                         maxfound = true;
  28.                     }
  29.                     break;
  30.                 case 29:
  31.                     if (matrix[i] > matrix[i - 1])
  32.                     {
  33.                         maxfound = true;
  34.                     }
  35.                     break;
  36.                 default:
  37.                     if (matrix[i] > matrix[i - 1] && matrix[i] > matrix[i + 1])
  38.                     {
  39.                         maxfound = true;
  40.                     }
  41.                     break;
  42.             }
  43.             if (maxfound)
  44.             {
  45.                 int m_length = localmax.Length;
  46.                 int[] tempmax = new int[m_length + 1];
  47.                 tempmax[tempmax.Length - 1] = matrix[i];
  48.                 localmax = tempmax;
  49.                 maxfound = false;
  50.                 Console.Write(localmax[m_length] + " ");
  51.             }
  52.         }
  53.         Console.ReadKey();
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement