Advertisement
Guest User

Untitled

a guest
Dec 17th, 2013
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.81 KB | None | 0 0
  1. /*
  2.  * Write a program that finds in given array of integers a sequence of given sum S (if present).
  3.  * Example:  {4, 3, 1, 4, 2, 5, 8}, S=11  {4, 2, 5}
  4.  */
  5. using System;
  6.  
  7. class SequenceOfSum
  8. {
  9.    
  10.     static void Main()
  11.     {
  12.         int sizeN;
  13.         do
  14.         {
  15.             Console.Write("Enter size of the array: ");
  16.         } while (!int.TryParse(Console.ReadLine(), out sizeN) && sizeN > 0);
  17.  
  18.         long sumWeSeek;
  19.         do
  20.         {
  21.             Console.Write("Enter size of the array: ");
  22.         } while (!long.TryParse(Console.ReadLine(), out sumWeSeek));
  23.  
  24.         Console.WriteLine(new string('-', 20));
  25.         int[] numArray = new int[sizeN];
  26.  
  27.         for (int index = 0; index < sizeN; index++)
  28.         {
  29.             do
  30.             {
  31.                 Console.Write("Enter element{0}: ", index);
  32.             } while (!int.TryParse(Console.ReadLine(), out numArray[index]));
  33.         }
  34.        
  35.         //int[] numArray = { 10, 10, 10, 10, 10 };
  36.         //long sumWeSeek = 10;
  37.  
  38.         //print array elements
  39.         Console.WriteLine("Array elements: ");
  40.         Console.WriteLine("{0}", String.Join(" ", numArray));
  41.        
  42.         Console.WriteLine("\n" + new string('-', 40));
  43.         Console.WriteLine("Sequences with sum {0}: ", sumWeSeek);
  44.        
  45.         long currentSum = 0;
  46.         int begin = 0;
  47.         int end = 0;
  48.         bool noSuchSum = true;
  49.         //loop entire array
  50.         for (int index = 0; index < numArray.Length; index++)
  51.         {
  52.             if (numArray[index] == sumWeSeek) //number is exactly the sum
  53.             {
  54.                 end = index;
  55.                 begin = index;
  56.                 PrintSequece(begin, end, numArray);
  57.                 noSuchSum = false;
  58.                 continue;
  59.             }
  60.  
  61.             currentSum = numArray[index]; //start
  62.             begin = index;
  63.            
  64.            
  65.             for (int j = index+1; j < numArray.Length; j++)
  66.             {
  67.                 currentSum = currentSum + numArray[j];
  68.                 if (currentSum == sumWeSeek)
  69.                 {
  70.                     end = j;
  71.                     PrintSequece(begin, end, numArray);
  72.                     noSuchSum = false;
  73.                     break;
  74.                 }
  75.                 else if (currentSum > sumWeSeek)
  76.                 {
  77.                     break;
  78.                 }
  79.                
  80.             }
  81.         }
  82.  
  83.         if (noSuchSum)
  84.         {
  85.             Console.WriteLine("There are no such sequences Sorry!");
  86.         }
  87.        
  88.     }
  89.     //Helper method to Print Sequences of original array
  90.     private static void PrintSequece(int begin, int end, int[] array)
  91.     {
  92.         for (int i = begin; i <= end; i++)
  93.         {
  94.             Console.Write(array[i] + " ");
  95.         }
  96.         Console.WriteLine();
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement