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 4083836
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:21:49+00:00 2026-05-20T18:21:49+00:00

I have no idea how to get all the possibilities from x sets of

  • 0

I have no idea how to get all the possibilities from x sets of data other than using x nested for loops, which i dont want to do, because i dont know the value of x, which makes it very possible for the number of for loops hard coded not equal to the number of loops needed.

say i have:

A:1,2,3,4
B:2,4,65,2
C:1,3,2
D:2,8

and i wanted to get every combination of 1 item from each group (in my code, im using std::vector <myclass>), how would i do that? can someone please post a general pseudocode i can follow to do this?

  • 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-20T18:21:49+00:00Added an answer on May 20, 2026 at 6:21 pm

    if you don’t know how many ‘groups’ you have, I assume you have at least some collection of all of them? i.e., an array/vector of all your vectors?

    if so, here’s a general idea of what would work

    void iterateAllCombinations(array_of_groups, current_group_index,temp_result,callback_function)
    {
      current_group = array_of_groups[current_group_index]
      for each element in current_group
      {
        temp_result[current_group_index]=element
        if current_group_index>0
          iterateAllCombinations(array_of_groups,current_group_index-1,temp_result,callback_function)
        else
          callback_function(temp_result)
      }
    }
    
    this would be called in some fashion like:
    iterateAllCombinations(my_groups,my_groups.length-1,new vector[my_groups.length],&do_something_with_array)
    

    A basic explanation of what’s going on:
    when you call the function, it will start going through each item on the ‘top’ group (the last one in the array_of_groups). If there’s more groups beyond the ‘top’ one, it will call the same function, passing the current items so far, and telling it to look at the next group down.

    This next level down will start going through each item, and behave the same way. This continues until you get to the bottom group, where, instead of calling another level down, it will pass the collection of all results to your callback function, which will use them.

    When the bottom level has gone through all items, it will return to the next level up, which is still in its ‘for each’ loop. the next level up will go to the next item, and call the bottom level, which will start from the beginning. this continues until eventually even the ‘top’ level has gone through each item.


    That’s the pseudocode, the specifics change depending on your language.
    the ‘temp result’ can be a declared and allocated up-front array, (like I show in the pseudocode), or it can be a stack that gets pushed before each recursive call, and popped after, or it can be an array/vector that you copy and append before each recursive call. You could even have a linked list, with each node just stored in the stack frame for that function call, and you’d just have to pass the pointer of the ‘parent’ to the next level down.

    If you have one specific thing you want to do, you can replace the ‘callback_function’ with that specific thing. Or, if your language allows it, you can use this as an iterator/generator, and let that ‘ callback’ be implicit

    If you really hate recursive code, you can write the same thing as a while loop, but it’s much more straightforward to write it as a recursive function.

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

Sidebar

Related Questions

I am using the following code do get all data records from a MS
Anyone have idea how to pass value from iPhone application to the REST-Webservice using
how can i get all rt_XX with jquery each? I have no idea :/
does someone have an idea how to get the environment variables on Google-AppEngine ?
I have no idea why this is happening. The error I get is the
I get the following Nhiberante error, but have no idea why: could not execute
I have idea to write errors from my application to the Windows Event Viewer
I missed my workspace in which I was working. Any one have idea about
I have no idea how to pass a value from jtable1 to jtable2 .
I am attempting to get all records where and Id field exists more than

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.