473,548 Members | 2,683 Online
Bytes | Software Development & Data Engineering Community
+ 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 1700
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
1210
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 dont think its possible to create reports based on datasets that are created at runtime!! (unless anyone knows otherwise??) So as a work around i...
5
3605
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 puposes only and is not normally visable to the user.) The Thread function is actually in the Form class. Now.. What I am seeing is that when I...
6
3087
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 should be submitted as if the button is pressed. Is this correct? Does this behaviour vary across browsers? Chris
1
2399
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 relevent info Another dbase with subject info and grades etc assigned to the students info I wish to use one form to enter data into all tables. The...
4
19041
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 there an easy way to do this or would I have to check for all keypresses in an event somewhere and manually set the focus on Button1 if the RETURN key...
4
3038
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 to save it. I have tried creating the object that I place into the DoDragDrop() by inheriting the COM interfaces IDropSource and IDataObject...
9
2806
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 facing. Please please help me to solve this as soon as possible. So here we go ... I am not able to take the screen shot of the windows form...
0
1242
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 created a form with a button, textbox and NumericUpDown. Textbox holds the message to be displayed on the newly created form and the NumericUpDown...
6
8361
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 do I define and declare a label? Could just make a pile of blank labels and set them to invisible ready at my disposal, but I thought surly there...
7
9340
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 display the Windows form. The form is being displayed but the command only returns when the form is closed. I want the command line to return...
0
7707
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7951
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6036
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5362
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5082
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3495
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1926
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1051
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
751
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.