Advertisement
stak441

Arrays - problem 21

Jan 13th, 2013
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.27 KB | None | 0 0
  1. static void Main(string[] args)
  2.         {
  3.             Console.WriteLine("Please enter a value for K:");
  4.             int k = int.Parse(Console.ReadLine());
  5.             Console.WriteLine("Please enter a value for N:");
  6.             int n = int.Parse(Console.ReadLine());
  7.             int[] array = new int[k];
  8.            
  9.             GetVariations(array, 0, n, 1);            //Input given consists of: array(with defined size), starting index 0 and the max number
  10.  
  11.         }
  12.  
  13.         private static void GetVariations(int[] array, int k, int n, int start)
  14.         {
  15.             if (k == array.Length)          //Recursion bottom - when k reaches max the result is printed
  16.             {
  17.                 PrintResult(array);
  18.             }
  19.             else
  20.             {
  21.                 for (int i = start; i <= n; i++)
  22.                 {
  23.                     array[k] = i;
  24.                     GetVariations(array, k + 1, n, i + 1);     //The recursive element increases the index k with 1 on each call
  25.                 }
  26.             }
  27.            
  28.         }
  29.  
  30.         private static void PrintResult(int[] array)
  31.         {
  32.             foreach (var number in array)
  33.             {
  34.                 Console.Write(number + " ");
  35.             }
  36.             Console.WriteLine();
  37.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement