Playing a game, I got stuck on a mini gamne puzzle in which You were required to find numbers, then put them into a five digit code to open a door. I had found the numbers but had no idea what order they should go in. So i made a a C++ code to give me all the possible numerical sequences. This was the end result.

#include <iostream.h>
#include <stdlib.h>
main()
{
 int a,b,c,d,e;
 cout<< "Put in the 5 numbers in the code for all possible solutions (1 digit numbers)";
 cin >>a>>b>>c>>d>>e;                // if the input: 1>>2>>3>>4>>5
 cout<< "These are the possible codes\n";  //then the output would be
 cout<<setw(10)<<a<<b<<c<<d<<e;                   // 12345
 cout<<setw(10) <<a<<b<<c<<e<<d;                  // 12354
 cout<<setw(10)<<a<<b<<d<<c<<e;                   // 12435
 cout<<setw(10) <<a<<b<<d<<e<<c<<'\n';           // 12453
 cout<<setw(10)<<a<<b<<e<<c<<d;                    // 12534
 cout<<setw(10) <<a<<b<<e<<d<<c;                    // 12543
 cout<<setw(10)<<a<<c<<b<<d<<e;                     // 13245
 cout<<setw(10) <<a<<c<<b<<e<<d<<'\n';            // 13254
 cout<<setw(10)<<a<<c<<d<<b<<e;                    // 13425
 cout<<setw(10) <<a<<c<<d<<e<<b;                   // 13452
 cout<<setw(10)<<a<<c<<e<<b<<d;                    // 13524
 cout<<setw(10) <<a<<c<<e<<d<<b<<'\n';            // 13542
 cout<<setw(10)<<a<<d<<b<<c<<e;                    // 14235
 cout<<setw(10) <<a<<d<<b<<e<<c;                   // 14253
 cout<<setw(10)<<a<<d<<c<<b<<e;                    // 14325
 cout<<setw(10) <<a<<d<<c<<e<<b<<'\n';           // 14352
 cout<<setw(10)<<a<<d<<e<<b<<c;                   // 14523
 cout<<setw(10) <<a<<d<<e<<c<<b;                  // 14532
 cout<<setw(10)<<a<<e<<b<<c<<d;                   // 15234
 cout<<setw(10) <<a<<e<<b<<d<<c<<'\n';          // 15243
 cout<<setw(10)<<a<<e<<c<<b<<d;                   // 15324
 cout<<setw(10) <<a<<e<<c<<d<<b;                  // 15342
 cout<<setw(10)<<a<<e<<d<<b<<c;                   // 15423
 cout<<setw(10) <<a<<e<<d<<c<<b<<'\n';          // 15432
 cout<<setw(10)<<b<<a<<c<<d<<e;                   // 21345
 cout<<setw(10) <<b<<a<<c<<e<<d;                  // 21354
 cout<<setw(10)<<b<<a<<d<<c<<e;                   // 21435
 cout<<setw(10) <<b<<a<<d<<e<<c<<'\n';           // 21453
 cout<<setw(10)<<b<<a<<e<<c<<d;                   // 21534
 cout<<setw(10) <<b<<a<<e<<c<<d;                  // 21543
 cout<<setw(10)<<b<<c<<a<<d<<e;                   // 23145
 cout<<setw(10) <<b<<c<<a<<e<<d<<'\n';           // 23154
 cout<<setw(10)<<b<<c<<d<<a<<e;                   // 23415
 cout<<setw(10) <<b<<c<<d<<e<<a;                  // 23451
 cout<<setw(10)<<b<<c<<e<<a<<d;                   // 23514
 cout<<setw(10) <<b<<c<<e<<d<<a<<'\n';           // 23541
 cout<<setw(10)<<b<<d<<a<<c<<e;                   // 24135
 cout<<setw(10) <<b<<d<<a<<e<<c;                  // 24153
 cout<<setw(10)<<b<<d<<c<<a<<e;                   // 24315
 cout<<setw(10) <<b<<d<<c<<e<<a<<'\n';            // 24351
 cout<<setw(10)<<b<<d<<e<<a<<c;                   // 24513
 cout<<setw(10) <<b<<d<<e<<c<<a;                  // 24531
 cout<<setw(10)<<b<<e<<a<<c<<d;                   // 25134
 cout<<setw(10) <<b<<e<<a<<d<<c<<'\n';            // 25143
 cout<<setw(10)<<b<<e<<c<<a<<d;                   // 25314
 cout<<setw(10) <<b<<e<<c<<d<<a;                  // 25341
 cout<<setw(10)<<b<<e<<d<<a<<c;                   // 25413
 cout<<setw(10) <<b<<e<<d<<c<<a<<'\n';            // 25431
 cout<<setw(10)<<c<<a<<b<<d<<e;                   // 31245
 cout<<setw(10) <<c<<a<<b<<e<<d;                  // 31254
 cout<<setw(10)<<c<<a<<d<<b<<e;                   // 31425
 cout<<setw(10) <<c<<a<<d<<e<<b<<'\n';            // 31452
 cout<<setw(10)<<c<<a<<e<<b<<d;                   // 31524
 cout<<setw(10) <<c<<a<<e<<d<<b;                  // 31542
 cout<<setw(10)<<c<<b<<a<<d<<e;                   // 32145
 cout<<setw(10) <<c<<b<<a<<e<<d<<'\n';            // 32154
 cout<<setw(10)<<c<<b<<d<<a<<e;                   // 32415
 cout<<setw(10) <<c<<b<<d<<e<<a;                  // 32451
 cout<<setw(10)<<c<<b<<e<<a<<d;                   // 32514
 cout<<setw(10) <<c<<b<<e<<d<<a<<'\n';            // 32541
 cout<<setw(10)<<c<<d<<a<<b<<e;                   // 34125
 cout<<setw(10) <<c<<d<<a<<e<<b;                  // 34152
 cout<<setw(10)<<c<<d<<b<<a<<e;                   // 34215
 cout<<setw(10) <<c<<d<<b<<e<<a<<'\n';            // 34251
 cout<<setw(10)<<c<<d<<e<<a<<b;                   // 34512
 cout<<setw(10) <<c<<d<<e<<b<<a;                  // 34521
 cout<<setw(10)<<c<<e<<a<<b<<d;                   // 35124
 cout<<setw(10) <<c<<e<<a<<d<<b<<'\n';            // 35142
 cout<<setw(10)<<c<<e<<b<<a<<d;                   // 35214
 cout<<setw(10) <<c<<e<<b<<d<<a;                  // 35241
 cout<<setw(10)<<c<<e<<d<<a<<b;                   // 35412
 cout<<setw(10) <<c<<e<<d<<b<<a<<'\n';           // 35421
 cout<<setw(10)<<d<<a<<b<<c<<e;                   // 41235
 cout<<setw(10) <<d<<a<<b<<e<<c;                  // 41253
 cout<<setw(10)<<d<<a<<c<<b<<e;                   // 41325
 cout<<setw(10) <<d<<a<<c<<e<<b<<'\n';           // 41352
 cout<<setw(10)<<d<<a<<e<<b<<c;                   // 41523
 cout<<setw(10) <<d<<a<<e<<c<<b;                  // 41532
 cout<<setw(10)<<d<<b<<a<<c<<e;                   // 42135
 cout<<setw(10) <<d<<b<<a<<e<<c<<'\n';           // 42153
 cout<<setw(10)<<d<<b<<c<<a<<e;                   // 42315
 cout<<setw(10) <<d<<b<<c<<e<<a;                  // 42351
 cout<<setw(10)<<d<<b<<e<<a<<c;                   // 42513
 cout<<setw(10) <<d<<b<<e<<c<<a<<'\n';           // 42531
 cout<<setw(10)<<d<<c<<a<<b<<e;                   // 43125
 cout<<setw(10) <<d<<c<<a<<e<<b;                  // 43152
 cout<<setw(10)<<d<<c<<b<<a<<e;                   // 43215
 cout<<setw(10) <<d<<c<<b<<a<<e<<'\n';           // 43251
 cout<<setw(10)<<d<<c<<e<<a<<b;                   // 43512
 cout<<setw(10) <<d<<c<<e<<b<<a;                  // 43521
 cout<<setw(10)<<d<<e<<a<<b<<c;                   // 45123
 cout<<setw(10) <<d<<e<<a<<c<<b<<'\n';           // 45132
 cout<<setw(10)<<d<<e<<b<<a<<c;                   // 45213
 cout<<setw(10) <<d<<e<<b<<c<<a;                  // 45231
 cout<<setw(10)<<d<<e<<c<<a<<b;                   // 45312
 cout<<setw(10) <<d<<e<<c<<b<<a<<'\n';           // 45321
 cout<<setw(10)<<e<<a<<b<<c<<d;                   // 41235
 cout<<setw(10) <<e<<a<<b<<d<<c;                  // 41253
 cout<<setw(10)<<e<<a<<c<<b<<d;                   // 41325
 cout<<setw(10) <<e<<a<<c<<d<<b<<'\n';            // 41352
 cout<<setw(10)<<e<<a<<d<<b<<c;                   // 41523
 cout<<setw(10) <<e<<a<<d<<c<<b;                  // 41532
 cout<<setw(10)<<e<<b<<a<<c<<d;                   // 42135
 cout<<setw(10) <<e<<b<<a<<d<<c<<'\n';           // 42153
 cout<<setw(10)<<e<<b<<c<<a<<d;                   // 42315
 cout<<setw(10) <<e<<b<<c<<d<<a;                  // 42351
 cout<<setw(10)<<e<<b<<d<<a<<c;                   // 42513
 cout<<setw(10) <<e<<b<<d<<c<<a<<'\n';           // 42531
 cout<<setw(10)<<e<<c<<a<<b<<d;                   // 43125
 cout<<setw(10) <<e<<c<<a<<d<<b;                  // 43152
 cout<<setw(10)<<e<<c<<b<<a<<d;                   // 43215
 cout<<setw(10) <<e<<c<<b<<d<<a<<'\n';           // 43251
 cout<<setw(10)<<e<<c<<d<<a<<b;                   // 43512
 cout<<setw(10) <<e<<c<<d<<b<<a;                  // 43521
 cout<<setw(10)<<e<<d<<a<<b<<c;                   // 45123
 cout<<setw(10) <<e<<d<<a<<c<<b<<'\n';           // 45132
 cout<<setw(10)<<e<<d<<b<<a<<c;                   // 45213
 cout<<setw(10) <<e<<d<<b<<c<<a;                  // 45231
 cout<<setw(10)<<e<<d<<c<<a<<b;                   // 45312
 cout<<setw(10) <<e<<d<<c<<b<<a<<'\n';          // 45321
      system("PAUSE");
      return 0;
}

Is there anyway to simplify the code and get the same result?

Recommended Answers

All 2 Replies

Maybe something like this.

#include <iostream>
#include <iterator>
#include <algorithm>
using namespace std;

int main()
{
   int array[5];
   cout << "Input " << sizeof array / sizeof *array << " numbers: ";
   for ( size_t i = 0; i < sizeof array / sizeof *array; ++i )
   {
      if ( !(cin >> array[i]) )
      {
         return 0;
      }
   }
   do {
      copy(array, &array[sizeof array / sizeof *array], ostream_iterator<int>(cout,""));
      cout << endl;
   } while ( next_permutation(array, &array[sizeof array / sizeof *array]) );
   return 0;
}

Thanks
I really dont understand it, Im in my 1st year of C++but itll give me something to learn
Thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.