Advertisement
stoianpp

ArrayTask8

Dec 12th, 2013
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.18 KB | None | 0 0
  1. using System;
  2. class MaxSumConsElements
  3. {
  4.     static void Main()
  5.     {
  6.         Console.Write("Enter the array length: ");
  7.         int length = int.Parse(Console.ReadLine());
  8.         int[] array = new int[length];
  9.         Console.Write("Enter the number of consequtive elements for finding the max sum: ");
  10.         int elements = int.Parse(Console.ReadLine());
  11.         long totalSum = int.MinValue;
  12.         int startElement = 0;
  13.         for (int i = 0; i < length; i++)
  14.         {
  15.             Console.Write("Enter elements[{0}] value: ", i + 1);
  16.             array[i] = int.Parse(Console.ReadLine());
  17.         }
  18.         for (int i = 0; i < length-elements; i++)
  19.         {
  20.             int currSum = 0;
  21.             for (int g = 0; g < elements; g++)
  22.             {
  23.                 currSum += array[i + g];
  24.             }
  25.             if (currSum > totalSum)
  26.             {
  27.                 totalSum = currSum;
  28.                 startElement = i;
  29.             }
  30.         }
  31.         Console.Write("The {0} element with maximal sum in the array are: ",elements);
  32.         for (int i = 0; i < elements; i++)
  33.         {
  34.             Console.Write("{0} ",array[startElement+i]);
  35.         }
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement