Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
2.09/5 (4 votes)
See more:
Dear Friends,

Here i want to Write Recurssive Function the Format is Like This

UserID , UnitName , P.ID

1 ANIL 0

2 KUMAR 1

3 Phani 1

4 Shruthi 2


If i Search Through P.ID =1 The data Should Filter like

UserID , UnitName , P.ID

2 KUMAR 1

3 Phani 1

Like this But i searched Google and i Found and i made changes still iam failed

C#
static void Main(string[] args)
{
    List<Array> myList = new List<Array>();
    myList.Add(new string[0]);
    myList.Add(new string[0]);
    myList.Add(new string[0]);
    myList[0] = new string[] { "1", "2","3","4"};
    myList[1] = new string[] { "Anil", "Kumar","Phani","Shruthi"};
    myList[2] = new string[] { "0", "1" ,"1","2"};
    foreach (string x in foo(0, myList))
    {
        Console.WriteLine(x);
    }

    Console.ReadKey();

C#
static List<string> foo(int a, List<Array> x)
        {
            List<string> retval = new List<string>();
            if (a == x.Count)
            {
                retval.Add("");
                return retval;
            }
            foreach (Object y in x[a])
            {
                foreach (string x2 in foo(a + 1, x))
                {
                    retval.Add(y.ToString() + " " + x2.ToString());
                }

            }
            return retval;
        }


Please anybody can solve this.


Regards,

AnilKumar.D
Posted
Updated 6-Sep-13 3:46am
v2
Comments
ZurdoDev 6-Sep-13 9:56am    
What's the problem? A recursive function is one which calls itself so put a breakpoint and figure out what is going wrong.

1 solution

You are WAY off my friend. First, you don't ever get the P.ID you are looking for. Second, you are simply concatenating the contents of 3 arrays to get your output... don't make it more complicated than it has to be. Thirdly, there is no reason for a recursive function.

C#
static void Main(string[] args)
{
    List<string> userID = new List<string>() = { "1", "2","3","4"};
    List<string> unitName = new List<string>() = { "Anil", "Kumar","Phani","Shruthi"};
    List<string> pID = new List<string>() = { "0", "1" ,"1","2"};

    for(int i=0; i < pID.Count; i++)
    {
        if (pID[i] == args[0])
        {
             Console.WriteLine(userID[i] + " " + unitName[i] + " " + pID[i]);
        }
    }

Console.ReadKey();


Call your program passing the desired P.ID on the command line. You will get the output you want.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900