Advertisement
TanyaPetkova

C# Part II Arrays Exercise 4

Dec 20th, 2013
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.52 KB | None | 0 0
  1. //Write a program that finds the maximal sequence of equal elements in an array.
  2. //Example: {2, 1, 1, 2, 3, 3, 2, 2, 2, 1} -> {2, 2, 2}.
  3.  
  4. using System;
  5. using System.Collections.Generic;
  6.  
  7. class MaximalSequence
  8. {
  9.     static void Main()
  10.     {
  11.         uint arrayLength;
  12.         int currentLength = 1;
  13.         int maxLength = 0;
  14.         List<int> elements = new List<int>();
  15.  
  16.         Console.Write("Enter the array's length: ");
  17.  
  18.         while (!uint.TryParse(Console.ReadLine(), out arrayLength) || arrayLength == 0)
  19.         {
  20.             Console.Write("Invalid input. Enter a positive integer number: ");
  21.         }
  22.  
  23.         int[] array = new int[arrayLength];
  24.  
  25.         for (int i = 0; i < arrayLength; i++)
  26.         {
  27.             Console.Write("Enter the {0} element of the  array: ", i);
  28.  
  29.             while (!int.TryParse(Console.ReadLine(), out array[i]))
  30.             {
  31.                 Console.Write("Invalid input. Enter an integer number: ");
  32.             }
  33.         }
  34.  
  35.         int pointer = array[0];
  36.  
  37.         for (int i = 1; i < arrayLength; i++)
  38.         {
  39.             if (pointer == array[i])
  40.             {
  41.                 currentLength++;
  42.             }
  43.             else
  44.             {
  45.                 pointer = array[i];
  46.  
  47.                 if (maxLength < currentLength)
  48.                 {
  49.                     maxLength = currentLength;
  50.                     elements.Clear();
  51.                     elements.Add(array[i - 1]);
  52.                 }
  53.                 else if (maxLength == currentLength)
  54.                 {
  55.                     elements.Add(array[i - 1]);
  56.                 }
  57.                 currentLength = 1;
  58.             }
  59.         }
  60.  
  61.         if (maxLength < currentLength)
  62.         {
  63.             maxLength = currentLength;
  64.             elements.Clear();
  65.             elements.Add(array[arrayLength - 1]);
  66.         }
  67.         else if (maxLength == currentLength)
  68.         {
  69.             elements.Add(array[arrayLength - 1]);
  70.         }
  71.  
  72.         if (elements.Count == 1)
  73.         {
  74.             Console.Write("There is one sequence with maximal length: ");
  75.         }
  76.         else
  77.         {
  78.             Console.WriteLine("There are {0} sequences with maximal length: ", elements.Count);
  79.         }
  80.  
  81.         foreach (var element in elements)
  82.         {
  83.             int[] maxSequence = new int[maxLength];
  84.  
  85.             for (int i = 0; i < maxLength; i++)
  86.             {
  87.                 maxSequence[i] = element;
  88.             }
  89.  
  90.             Console.WriteLine(string.Join(", ", maxSequence));
  91.         }
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement