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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:43:21+00:00 2026-05-31T02:43:21+00:00

I’m trying to figure out how I can use recursion to do n-level nested

  • 0

I’m trying to figure out how I can use recursion to do n-level nested for loops.
For example, if n=3, there would be 3 ‘levels’

for(z=0;z<6;z++){
   for(y=0;y<6;y++){
      for(x=0;x<6;x++){
         if (z+y+x==f){
            //do something
         } 
      }
   }
}

and so on.

I can’t seem to figure out how I would be able to place the if loop in the last for loop and how I can access the variables of previous for loops from the if statement. I know that the question of variable nested loops has been asked alot of times, and I have looked through all of them. But none seem to help me.

Could someone present an easy way of using recursion to achieve this, keeping in mind that I’m still a beginner in c++, to point me in the right direction?

The use case is as follows:

Write a program to input the number of dice m. The program will output the total number of possible cases, the number of possible cases for each possible n and the n with the highest probability. Note: only one input m is read in. n is computed by the program

Example if user enters m=2 then program should output

The total number of possible cases is 36.
The possibilities are
2 1
3 2
4 3
.
.
.
12 1

  • 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-31T02:43:22+00:00Added an answer on May 31, 2026 at 2:43 am

    Here’s an example in plain old C++. First I make a vector of the ranges for each dimension called maxes. if the sum of all indices are 2 then I print did something.
    In the example I loop z from 0 to 1, y from 0 to 2, x from 0 to 3

    You can for sure make this more neat.

    Here goes:

    #include <iostream>
    #include <vector>
    using namespace std;
    
    int f(){ 
        return 2 ;
    }
    
    void inner(int depth,vector<int> & numbers,vector<int> & maxes){
      if (depth>0){
         for(int i=0;i<maxes[depth-1];i++){
            numbers[depth-1]=i;
            inner(depth-1, numbers,maxes) ;
         }
      }else{
         // calculate sum of x,y,z:
         cout << "values are ";
         for(int i=0;i<numbers.size();i++){
            cout <<numbers[i]<<" ";
         }
         int thesum(0);
         for(int i=0;i<numbers.size();i++){
            thesum+=numbers[i];
         }
         if (thesum==f()){
            cout << "did something! ";
         }
         cout<<endl;
       }
    }
    
    void donest(){
       vector<int>  numbers;
       numbers.resize(3);
       vector<int>  maxes;
       maxes.push_back(4);
       maxes.push_back(3);
       maxes.push_back(2);
       inner(numbers.size(),numbers,maxes);
    }
    
    int main(){
       donest();
    }
    

    result:

    values are 0 0 0 
    values are 1 0 0 
    values are 2 0 0  did something! 
    values are 3 0 0 
    values are 0 1 0 
    values are 1 1 0  did something! 
    values are 2 1 0 
    values are 3 1 0 
    values are 0 2 0  did something! 
    values are 1 2 0 
    values are 2 2 0 
    values are 3 2 0 
    values are 0 0 1 
    values are 1 0 1  did something! 
    values are 2 0 1 
    values are 3 0 1 
    values are 0 1 1  did something! 
    values are 1 1 1 
    values are 2 1 1 
    values are 3 1 1 
    values are 0 2 1 
    values are 1 2 1 
    values are 2 2 1 
    values are 3 2 1 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I want use html5's new tag to play a wav file (currently only supported

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.