Jump to content

Permutations loop not working

- - - - -

  • Please log in to reply
3 replies to this topic

#1
tjh001

tjh001

    Newbie

  • Members
  • Pip
  • 2 posts
Hi everyone,

Just started programming (generally and in C++) and wanted to make a program that would calculate every permutation of numbers 1 and 9 (i.e. 1,2 1,3 1,4 ....). Ive come up with the following code :


#include <iostream>

#include <cstdlib>

#include <ctime>

using namespace std;



int main()

{

    int x [2]={0,0};

int g=9;

while (g == 9){

    for (int z=0;z <=8;z++){                                   // 2nd element increases by 1 everytime


        x[1]= x[1]+1;


        if (x[1]==9){                                           //when 2nd element  reaches 9:

                                                                // first element increases by 1

        x[0] = x[0]+1;                                          // counter in z reset to 0

        z=0;

        x[1] = 0;                                                // second element of array restarts at 0


                    }


if (x[0] > 9){                                                  // at the point when the first element becomes greater than 9:


g=10;                                                             // g = 10 and so the loop stops


}


                           }



cout << x[0] << x[1] << endl;

}


}




All I get is:
18
28
38
48
58
68
78
88
98
108

I feel my code is quite close to working (but then I'm a complete beginner at this). Any help most appreciated!

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Try moving your cout inside the for loop like this:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;


int main()
{
  int x [2]={0,0};
  int g=9;
  while (g == 9){
    for (int z=0;z <=8;z++){                                   // 2nd element increases by 1 everytime
      x[1]= x[1]+1;
      if (x[1]==9){                                           //when 2nd element  reaches 9:
                                                              // first element increases by 1
        x[0] = x[0]+1;                                          // counter in z reset to 0
        z=0;
        x[1] = 0;                                                // second element of array restarts at 0
       }
      if (x[0] > 9){                                                  // at the point when the first element becomes greater than 9:
        g=10;                                                             // g = 10 and so the loop stops
      }
      cout << x[0] << x[1] << endl;
    }
  }
}


Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
tjh001

tjh001

    Newbie

  • Members
  • Pip
  • 2 posts
Ah great it works, thanks. Is my method the best way to keep finding more permutations (i.e. 1,2,3 1,2,4 1,2,5 ...)?

#4
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
In STL algorithm header you have next_permutation and prev_permutation functions. You can use those to test your code. Or to just play around. :)
A conclusion is where you got tired of thinking.
#define class struct    // All is public.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users