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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:45:46+00:00 2026-06-08T18:45:46+00:00

I have these for loops. // output all possible combinations for ( int i1

  • 0

I have these for loops.

// output all possible combinations
for ( int i1 = 0; i1 <= 2; i1++ )
     {
         for ( int i2 = 0; i2 <= 2; i2++ )
             {
                 for ( int i3 = 0; i3 <= 2; i3++ )
                     {
                         for ( int i4 = 0; i4 <= 2; i4++ )
                             {
                                 for ( int i5 = 0; i5 <= 2; i5++ )
                                     {
                                         for ( int i6 = 0; i6 <= 2; i6++ )
                                             {
                                                 for ( int i7 = 0; i7 <= 2; i7++ )
                                                     {
                                                         //output created words to outFile
                                                         outFile
                                                         << phoneLetters[n[0]][i1]<< phoneLetters[n[1]][i2]
                                                         << phoneLetters[n[2]][i3]<< phoneLetters[n[3]][i4]
                                                         << phoneLetters[n[4]][i5]<< phoneLetters[n[5]][i6]
                                                         << phoneLetters[n[6]][i7]
                                                         << " ";

                                                         if ( ++count % 9 == 0 ) // form rows
                                                             outFile << std::endl;
                                                         }
                                                 }
                                         }
                                 }
                         }
                 }
         }

It looks awful but I’m too much of a newb to know where to begin in regards to condensing them.

Can someone give me a pointer or two so I can make this code a little neater?

  • 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-06-08T18:45:47+00:00Added an answer on June 8, 2026 at 6:45 pm

    You’re indexing 0, 1, and 2 on seven levels. This may not be terribly efficient, but how about this:

    int i1, i2, i3, i4, i5, i6, i7;
    int j;
    
    for (int i = 0; i < 2187; i++)
    {
        // 0 through 2186 represent all of the ternary numbers from
        //    0000000 (base 3) to 2222222 (base 3).  The following
        //    pulls out the ternary digits and places them into i1
        //    through i7.
    
        j = i;
    
        i1 = j / 729;
        j = j - (i1 * 729);
    
        i2 = j / 243;
        j = j - (i2 * 243);
    
        i3 = j / 81;
        j = j - (i3 * 81);
    
        i4 = j / 27;
        j = j - (i4 * 27);
    
        i5 = j / 9;
        j = j - (i5 * 9);
    
        i6 = j / 3;
        j = j - (i6 * 3);
    
        i7 = j;
    
        // print your stuff
    }
    

    Or, based on user315052’s suggestion in the comments:

    int d[7];
    
    for (int i = 0; i < 2187; i++)
    {
        int num = i;
        for (int j = 6; j >= 0; j--)
        {
            d[j] = num % 3;
            num = num / 3;
        }
    
        // print your stuff using d[0] ... d[6]]
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to output all the possible unique integer combinations from 1 to
I have a while loop that loops through 3 results and echo's these out
Possible Duplicate: Generating all Possible Combinations Is there a good LINQ way to do
I've created a form which has a loop to display a number of rows.
More than once now I have needed to generate all possible pairs of two
I have seen several sites where these social share buttons looks perfectly horizontal aligned.
I have created a loop, inside there is an if statment but it doesnt
I have an array like this (output from console.log): [122.42.123.1, 122.42.123.1, 122.42.123.1, awdawd, awdawd]
A class went to a school trip. And, as usually, all N kids have
Very simplified, I have two tables Source and Target. declare @Source table (SourceID int

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.