1

I have a program, where I have to generate all R-digit numbers among N digits in C++, for example for N=3 (all digits from 1 to N inclusive) and R=2 the program should generate 12 13 21 23 31 32. I tried to do this with arrays as follows, but it does not seem to work correctly.

#define nmax 20
#include <iostream>
using namespace std;
int n, r;
void print(int[]);

int main()
{
    cin >> n;
    cin >> r;

    int a[nmax];
    int b[nmax];
    int used[nmax];

    for (int p = 1; p <= n; p++) {
        //Filling the a[] array with numbers from 1 to n
        a[p] = n;
    }

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < r; j++) {
            b[j] = a[i];
            used[j] = 1;
            if (used[j]) {
                b[j] = a[i + 1];
            }
            used[j] = 0;
        }
        print(b);
    }

    return 0;
}

void print(int k[]) {
    for (int i = 0; i < r; i++) {
        cout << k[i];
    }
}
JHBonarius
  • 10,824
  • 3
  • 22
  • 41
A.Petrov
  • 71
  • 6

1 Answers1

0

If I understand your question correctly, you can explore this website where it explains the problem and suggests the solution thoroughly.

Here is a slightly altered code:

Pay attention that time is an issue for bigger N values.

#define N    5   // number of elements to permute.  Let N > 2
#include <iostream>
using namespace std;

// NOTICE:  Original Copyright 1991-2010, Phillip Paul Fuchs

void PrintPerm(unsigned int *a, unsigned int j, unsigned int i){
   for(unsigned int x = 0; x < N; x++)
      cout << " " << a[x];
   cout << "    swapped( " << j << " , " << i << " )\n";
}

void QuickPerm(void){
   unsigned int a[N], p[N+1];
   register unsigned int i, j, PermCounter = 1; // Upper Index i; Lower Index j

   for(i = 0; i < N; i++){   // initialize arrays; a[N] can be any type
      a[i] = i + 1;   // a[i] value is not revealed and can be arbitrary
      p[i] = i;
   }
   p[N] = N; // p[N] > 0 controls iteration and the index boundary for i
   PrintPerm(a, 0, 0);   // remove comment to PrintPerm array a[]
   i = 1;   // setup first swap points to be 1 and 0 respectively (i & j)
   while(i < N){
      p[i]--;             // decrease index "weight" for i by one
      j = i % 2 * p[i];   // IF i is odd then j = p[i] otherwise j = 0
      swap(a[i], a[j]);   // swap(a[j], a[i])
      PrintPerm(a, j, i);   // remove comment to PrintPerm target array a[]
      PermCounter++;
      i = 1;              // reset index i to 1 (assumed)
      while (!p[i]) {      // while (p[i] == 0)
         p[i] = i;        // reset p[i] zero value
         i++;             // set new index value for i (increase by one)
      } // while(!p[i])
   } // while(i < N)
    cout << "\n\n ---> " << PermCounter << " permutations. \n\n\n";
} // QuickPerm()

int main(){
    QuickPerm();
} //main

Here is a list of the modified items from the original code.

  1. N defined to be 5 instead of 12.
  2. A Counter has been added for more informative result.
  3. The original swap instructions reduced by using c++ standard libraries' swap() function.
  4. The getch() has been removed.
  5. The 'Display()' function has been renamed to be 'PrintPerm()'.
  6. The printf() function has been replaced by cout.
  7. Printing number of permutation has been added.
Shadi
  • 1,701
  • 2
  • 14
  • 27
  • @JHBonarius: I can't see where the OP asked for OO. This shows the core problem. In case of need, making it OO should not be a problem. – Shadi Feb 28 '18 at 14:48