Understanding Recursion

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • musademirtas
    New Member
    • Jun 2013
    • 3

    Understanding Recursion

    I need help on making a windows form application with a NumericUpDown and ListBox button on it.

    When I enter 2 in NumericUpDown button, it should write 1,2 and 2,1 in list box.

    Or when I enter 3 in NumericUpDown , it should create:
    Code:
    1,2,3
    1,3,2
    2,1,3
    2,3,1
    3,1,2
    3,2,1
    It is like a binary tree, creating all possible options.

    Please help!!!
    Last edited by Frinavale; Jun 13 '13, 03:15 PM. Reason: Fixed Grammar
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    What have you attempted so far to solve the problem?

    The experts on this site are more than happy to help you with your problems but they cannot do your assignment/program for you.

    Please attempt the assignment/program yourself first and post questions regarding any difficulties you have or about a particular function of the code that you don't know how to achieve.

    -Frinny

    Comment

    • musademirtas
      New Member
      • Jun 2013
      • 3

      #3
      Here is my code using "while" code, but the problem is how can I use recursive method to do the same thing.

      Code:
      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.Data;
      using System.Drawing;
      using System.Linq;
      using System.Text;
      using System.Windows.Forms;
      
      namespace WindowsFormsApplication13
      {
          public partial class Form1 : Form
          {
              public Form1()
              {
                  InitializeComponent();
              }
              private List<string> QuickPerm(int n)
              {
      
                  if (n < 1) n = 1;
      
                  List<string> perms = new List<string>();
      
                  int[] a = new int[n];
      
                  int[] p = new int[n]; // all zero by default 
      
                  int i, j, tmp;
      
                  for (i = 0; i < n; i++) a[i] = i + 1;
      
                  perms.Add(DisplayPerm(a));
      
                  i = 1;
      
                  while (i < n)
                  {
      
                      if (p[i] < i)
                      {
      
                          j = i % 2 * p[i];
      
                          tmp = a[j];
      
                          a[j] = a[i];
      
                          a[i] = tmp;
      
                          perms.Add(DisplayPerm(a));
      
                          p[i]++;
      
                          i = 1;
      
                      }
      
                      else
                      {
      
                          p[i] = 0;
      
                          i++;
      
                      }
      
                  }
      
      
      
      
                  return perms;
      
              }
      
      
      
      
              private string DisplayPerm(int[] a)
              {
      
                  var sb = new System.Text.StringBuilder();
      
      
      
      
                  for (int i = 0; i < a.Length; i++)
                  {
      
                      sb.Append(a[i].ToString());
      
                      if (i < a.Length - 1) sb.Append(",");
      
                  }
      
      
      
      
                  return sb.ToString();
      
              }
      
              private void button1_Click(object sender, EventArgs e)
              {
                  int n = (int)numericUpDown1.Value;
      
                  listBox1.DataSource = QuickPerm(n);
              }
      
              private void button2_Click(object sender, EventArgs e)
              {
                  const string message = "Are You Sure";
                  const string caption = "Cancel Installer";
                  var result = MessageBox.Show(message, caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                  if (result == DialogResult.Yes)
                      Environment.Exit(0);
              }
          }
      }
      Last edited by Frinavale; Jun 14 '13, 01:18 PM. Reason: Please use code tags when posting code.

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        A recursive method is simply a method that calls itself.

        You have to ensure that there is a point where it will stop calling itself or else you will quickly use up all the memory in your system because of your infinite loop.

        So, currently you are looping while i < n.
        This is your stopping condition.

        It also means that your method is going to take at least 2 parameters: i and n.

        Here is a recursive method that will stop when i is greater than or equal to n:

        Code:
        private void recursiveMethod(int i, int n)
        {
            if(i<n)
            {
                i++;
                recursiveMethod(i,n);
            }
        
        }
        Note how the method calls itself if i<n and does not call itself if i>n. This method will recurse so long as the condition is true and then stop.


        Now if you want to add the values to a string builder in order to return the values to on the screen, you will also have to pass a string builder.
        Code:
        private StringBuilder recursiveMethod(int i, int n, StringBuilder valuesToPrint)
        {
            if(i<n)
            {
                i++;
                valuesToPrint.Add(i.ToString());
                recursiveMethod(i,n);
            }
        
            return valuesToPrint;
        
        }
        Once the function is finished recursing, it will return the StringBuilder to the calling code.


        Take this concept and apply it to your application :)

        -Frinny
        Last edited by Frinavale; Jun 14 '13, 01:35 PM.

        Comment

        Working...