Advertisement
svetoslavhl

Untitled

May 21st, 2014
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. //5. Write a program that finds the maximal increasing sequence in an array.
  2. //Example: {3,2, 3, 4, 2, 2, 4} -> {2, 3, 4}
  3. using System;
  4.  
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. //We enter the size of the array
  10. Console.WriteLine("Enter the size of the array:");
  11. int size = int.Parse(Console.ReadLine());
  12. int[] arr = new int[size];
  13.  
  14. //We are entering the value of each element of the array
  15.  
  16. for (int i = 0; i < arr.Length; i++)
  17. {
  18.  
  19. Console.Write("arr[{0}] = ", i);
  20. arr[i] = int.Parse(Console.ReadLine());
  21.  
  22.  
  23. }
  24.  
  25.  
  26. //We are copying the array in a new array 1 position bigger
  27.  
  28. int[] arrCopy = new int[size + 1];
  29.  
  30. for (int i = 0; i < arr.Length; i++)
  31. {
  32.  
  33. arrCopy[i] = arr[i];
  34.  
  35.  
  36. }
  37.  
  38. arrCopy[size] = 20; // This means nothing because I wouldn't go to the last value
  39.  
  40.  
  41.  
  42.  
  43. int f = 0;
  44. bool isBigger = true;
  45. int sequence = 0;
  46. int maxSequence = 0;
  47. int firstElemSequence = 0;
  48. do
  49. {
  50. sequence = 0;
  51. do
  52. {
  53. isBigger = arrCopy[f] < arrCopy[f + 1];
  54. f++;
  55. sequence++;
  56.  
  57. } while (isBigger && f < arrCopy.Length - 1); //Calculating the current sequence
  58.  
  59. if (sequence > maxSequence) //Comparing the curent sequence with the max at the moment
  60. {
  61. maxSequence = sequence;
  62. firstElemSequence = f - (maxSequence);
  63. }
  64.  
  65. } while (f < arrCopy.Length - 1);
  66.  
  67.  
  68. for (int k = 0; k < arrCopy.Length - 1; k++)
  69. {
  70.  
  71. Console.Write("{0}", arrCopy[k]);
  72. if (k != arrCopy.Length - 2)
  73. {
  74. Console.Write(", ");
  75. }
  76.  
  77.  
  78.  
  79.  
  80.  
  81. }
  82.  
  83. Console.Write(" ----> ");
  84.  
  85.  
  86. for (int j = firstElemSequence; j < maxSequence + firstElemSequence; j++)
  87. {
  88.  
  89.  
  90. Console.Write("{0}", arrCopy[j]);
  91. if (j != maxSequence + firstElemSequence - 1)
  92. {
  93. Console.Write(", ");
  94. }
  95.  
  96. }
  97.  
  98. Console.WriteLine();
  99.  
  100.  
  101. }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement