Advertisement
AlexPopov

Arrays 4

Jan 10th, 2013
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.16 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace MaxSequence
  5. {
  6.     class MaxSequence
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             //explanation what the program does
  11.             Console.WriteLine("This program finds the maximal sequence of equal elements in an array");
  12.            
  13.             //choice for type of array
  14.             Console.Write("Choose the type of array. Enter str for strings or int for integers: ");
  15.             string choice = Console.ReadLine();
  16.  
  17.             //Input validation
  18.             while (choice != "str" && choice != "int")
  19.             {
  20.                 Console.Write("Invalid input. Please try again: ");
  21.                 choice = Console.ReadLine();
  22.             }
  23.  
  24.             //size of the array
  25.             Console.Write("Enter the number of elements the array will contain: ");
  26.             int len = int.Parse(Console.ReadLine());
  27.  
  28.             //variables for measuring the sequence
  29.             int seqence = 0, maxSequence = 0;
  30.             //variable to hold the element
  31.             object item = 0;
  32.  
  33.             if (choice == "str")
  34.             {
  35.                 //create array
  36.                 string[] str = new string[len];
  37.                 //Input
  38.                 Console.WriteLine("Enter array elements:");
  39.                 for (int i = 0; i < len; i++)
  40.                 {
  41.                     str[i] = Console.ReadLine();
  42.                 }
  43.  
  44.                 //Sort array
  45.                 Array.Sort(str);
  46.  
  47.                 //search for longest sequence
  48.                 for (int i = 0; i < len; i++)
  49.                 {
  50.                     seqence = 1;
  51.                     for (int j = i+1; j < len; j++)
  52.                     {
  53.                         if (str[i] == str[j])
  54.                         {
  55.                             seqence++;
  56.                         }
  57.                         else
  58.                         {
  59.                             break;
  60.                         }
  61.                     }
  62.  
  63.                     if (maxSequence < seqence)
  64.                     {
  65.                         maxSequence = seqence;
  66.                         item = str[i];
  67.                     }
  68.  
  69.                     if (maxSequence > len)
  70.                     {
  71.                         break;
  72.                     }
  73.                 }
  74.  
  75.                 //display max sequence
  76.                 Console.Write("{");
  77.                 for (int i = 0; i < maxSequence-1; i++)
  78.                 {
  79.                     Console.Write("{0}, ", item);
  80.                 }
  81.                 Console.Write(item);
  82.                 Console.WriteLine("}");
  83.  
  84.             }
  85.             else
  86.             {
  87.                 //create array
  88.                 int[] ints = new int[len];
  89.  
  90.                 //input
  91.                 Console.WriteLine("Enter array elements:");
  92.                 for (int i = 0; i < len; i++)
  93.                 {
  94.                     ints[i] = int.Parse(Console.ReadLine());
  95.                 }
  96.  
  97.                 //Sort array
  98.                 Array.Sort(ints);
  99.  
  100.                 //search for longest sequence
  101.                 for (int i = 0; i < len; i++)
  102.                 {
  103.                     seqence = 1;
  104.                     for (int j = i + 1; j < len; j++)
  105.                     {
  106.                         if (ints[i] == ints[j])
  107.                         {
  108.                             seqence++;
  109.                         }
  110.                         else
  111.                         {
  112.                             break;
  113.                         }
  114.                     }
  115.  
  116.                     if (maxSequence < seqence)
  117.                     {
  118.                         maxSequence = seqence;
  119.                         item = ints[i];
  120.                     }
  121.  
  122.                     if (maxSequence > len - i)
  123.                     {
  124.                         break;
  125.                     }
  126.                 }
  127.  
  128.                 //display max sequence
  129.                 Console.Write("{");
  130.                 for (int i = 0; i < maxSequence - 1; i++)
  131.                 {
  132.                     Console.Write("{0}, ", item);
  133.                 }
  134.                 Console.Write(item);
  135.                 Console.WriteLine("}");
  136.             }
  137.  
  138.         }
  139.     }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement