Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7494003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T17:16:38+00:00 2026-05-29T17:16:38+00:00

Possible Duplicate: Generating all Possible Combinations I’m trying to do a little algorithm in

  • 0

Possible Duplicate:
Generating all Possible Combinations

I’m trying to do a little algorithm in C++ or C#

Basically if you have an array:

{"ab","cd"}

output:

ac
ad
bc
bd

(I have two nested for loops)

but what if i have an array of 3 elements? or 4 ?

Thank you guys 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T17:16:39+00:00Added an answer on May 29, 2026 at 5:16 pm

    In order to vary the number of “nested loops” you need to use recursion.

    void printCombination(string[] str, string partial, int p) {
        if (p == str.Length) {
            Console.WriteLine(partial);
            return;
        }
        for (int i = 0 ; i != str[p].Length; i++) {
            printCombination(str, partial + str[p][i], p+1);
        }
    }
    

    Initial call looks like this:

    printCombination(new[] {"ab", "cd", "ef", "gh"}, "", 0);
    

    EDIT (in response to a comment by OP)

    To understand what is going on, you need to understand the meaning of the parameters first:

    • str is the array of strings. Each element of the array corresponds to a nesting level of the imaginary nested loops: element 0 is for the outer loop, element 1 is for the first level of nesting, and so on.
    • partial is the partially constructed result string. It is empty at the initial level, has one character at the first level of nesting, two at the second, and so on.
    • p is the nesting level. It is zero in the initial level, one at the first nesting level, and so on.

    The function has two parts – the stopping condition, and the body of the recursive call. The stopping condition is simple: once we get to the last level, partial result is no longer “partial”: it is complete; we can print it out and exit. How do we know that we’re at the last level? The number of elements of str equals the number of levels, so when p equals the length of the str array, we’re done.

    The body of the recursive call is a loop. It does the same thing that your nested loops do, but for only one level: each iteration adds one letter from its own array, and calls itself recursively for the next level.

    The best way to see this in action is to set a breakpoint on the line with the return statement, and look at the call stack window . Click each invocation level, and inspect the values of function parameters.

    If you are up for an exercise, try modifying this function to take two parameters instead of three. Hint: you can eliminate the last parameter by observing that the length of partial always matches the value of p.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Generating all Possible Combinations Is there a good LINQ way to do
Possible Duplicate: generating random numbers from skewed normal distribution i expect a long array
Possible Duplicate: How does the Google Did you mean? Algorithm work? Suppose you have
Possible Duplicate: Generating random numbers in Javascript I have the following code var randomnumber=Math.floor(Math.random()*101);
Possible Duplicate: Generating random results by weight in PHP? I have a web application
Possible Duplicate: Developing for Android in Eclipse: R.java not generating I have imported a
Possible Duplicate: Developing for Android in Eclipse: R.java not generating I have a one
Possible Duplicate: Java: generating random number in a range I need a little help.
Possible Duplicate: Algorithm for generating a random number Hi i need to assign a
Possible Duplicate: Reliable way of generating unique hardware ID Am trying to generate an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.