473,480 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Understanding Recursion

3 New Member
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:
Expand|Select|Wrap|Line Numbers
  1. 1,2,3
  2. 1,3,2
  3. 2,1,3
  4. 2,3,1
  5. 3,1,2
  6. 3,2,1
It is like a binary tree, creating all possible options.

Please help!!!
Jun 13 '13 #1
3 1693
Frinavale
9,735 Recognized Expert Moderator Expert
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
Jun 13 '13 #2
musademirtas
3 New Member
Here is my code using "while" code, but the problem is how can I use recursive method to do the same thing.

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace WindowsFormsApplication13
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         public Form1()
  15.         {
  16.             InitializeComponent();
  17.         }
  18.         private List<string> QuickPerm(int n)
  19.         {
  20.  
  21.             if (n < 1) n = 1;
  22.  
  23.             List<string> perms = new List<string>();
  24.  
  25.             int[] a = new int[n];
  26.  
  27.             int[] p = new int[n]; // all zero by default 
  28.  
  29.             int i, j, tmp;
  30.  
  31.             for (i = 0; i < n; i++) a[i] = i + 1;
  32.  
  33.             perms.Add(DisplayPerm(a));
  34.  
  35.             i = 1;
  36.  
  37.             while (i < n)
  38.             {
  39.  
  40.                 if (p[i] < i)
  41.                 {
  42.  
  43.                     j = i % 2 * p[i];
  44.  
  45.                     tmp = a[j];
  46.  
  47.                     a[j] = a[i];
  48.  
  49.                     a[i] = tmp;
  50.  
  51.                     perms.Add(DisplayPerm(a));
  52.  
  53.                     p[i]++;
  54.  
  55.                     i = 1;
  56.  
  57.                 }
  58.  
  59.                 else
  60.                 {
  61.  
  62.                     p[i] = 0;
  63.  
  64.                     i++;
  65.  
  66.                 }
  67.  
  68.             }
  69.  
  70.  
  71.  
  72.  
  73.             return perms;
  74.  
  75.         }
  76.  
  77.  
  78.  
  79.  
  80.         private string DisplayPerm(int[] a)
  81.         {
  82.  
  83.             var sb = new System.Text.StringBuilder();
  84.  
  85.  
  86.  
  87.  
  88.             for (int i = 0; i < a.Length; i++)
  89.             {
  90.  
  91.                 sb.Append(a[i].ToString());
  92.  
  93.                 if (i < a.Length - 1) sb.Append(",");
  94.  
  95.             }
  96.  
  97.  
  98.  
  99.  
  100.             return sb.ToString();
  101.  
  102.         }
  103.  
  104.         private void button1_Click(object sender, EventArgs e)
  105.         {
  106.             int n = (int)numericUpDown1.Value;
  107.  
  108.             listBox1.DataSource = QuickPerm(n);
  109.         }
  110.  
  111.         private void button2_Click(object sender, EventArgs e)
  112.         {
  113.             const string message = "Are You Sure";
  114.             const string caption = "Cancel Installer";
  115.             var result = MessageBox.Show(message, caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
  116.             if (result == DialogResult.Yes)
  117.                 Environment.Exit(0);
  118.         }
  119.     }
  120. }
Jun 14 '13 #3
Frinavale
9,735 Recognized Expert Moderator Expert
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:

Expand|Select|Wrap|Line Numbers
  1. private void recursiveMethod(int i, int n)
  2. {
  3.     if(i<n)
  4.     {
  5.         i++;
  6.         recursiveMethod(i,n);
  7.     }
  8.  
  9. }
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.
Expand|Select|Wrap|Line Numbers
  1. private StringBuilder recursiveMethod(int i, int n, StringBuilder valuesToPrint)
  2. {
  3.     if(i<n)
  4.     {
  5.         i++;
  6.         valuesToPrint.Add(i.ToString());
  7.         recursiveMethod(i,n);
  8.     }
  9.  
  10.     return valuesToPrint;
  11.  
  12. }
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
Jun 14 '13 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
1201
by: Varun | last post by:
Hi, I wanted to ask if its possible to have a html page embedded into a windows form? I need to create an invoivce in my application and am struggling to use crystal reports to do the job as i...
5
3592
by: me | last post by:
I have a Class Library that contains a Form and several helper classes. A thread gets created that performs processing of data behind the scenes and the Form never gets displayed (it is for debug...
6
3065
by: CJM | last post by:
Can somebody clarify if/how/when a simple form is submitted when the <Enter> key is pressed? As I understood it, if you have a form with a single submit button, if enter is pressed, the form...
1
2390
by: jbroome | last post by:
I have several tables that contain the same information as Identifying records but then with additional info that differs from table to table. e.g. 1 database of school students with their...
4
19037
by: Empire City | last post by:
My windows form has some text boxes and a two buttons, Button1 and Button2. The TAB key goes from field to field. When the user presses the RETURN key I want the focus to be moved to Button1. Is...
4
3033
by: Dave | last post by:
I need to add the ability to drag from a Windows Form and drop into a non dotNet application. For example, having a generated image in my app that I wish to drag out into explorer as a friendly way...
9
2799
by: Rajat Tandon | last post by:
Hello there, I am relatively new to the newsgroups and C#. I have never been disappointed with the groups and always got the prompt replies to my queries.This is yet another strange issue, I am...
0
1234
by: Phil G. | last post by:
Hi, my 'project' requires that I create a form with text info. at set time periods. These time periods are not evenly spaced so I pass a param for the delay(seconds). In order to debug this I have...
6
8354
by: coldude | last post by:
hi there, I need to be able to generate multiple labels for the use in a Windows form, the quantity of labels would depend on a count, is it possible to generate multiple labels in code? If so how...
7
9330
by: Jwe | last post by:
Hi, I've written a program which has both a command line interface and Windows form interface, however it isn't quite working correctly. When run from command line with no arguments it should...
0
7055
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6920
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7059
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7103
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5362
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4499
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3011
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
572
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
203
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.