Advertisement
TanyaPetkova

C# Part II Arrays Exercise 6 consecutive elements

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