#include <iostream>
#define     MAX 100
int       num[MAX];
int         n; 
int nrows = 0;
using namespace std;

void print()
{
  if (num != 0) {
		  for (int j = 0; j <n; j++) 
  cout << num[j];
	cout <<"\n";
		  
		  }
}
void leftRotate ( int n)
{int  tmp; 
for(int k =1; k<n-1; ++k)
{
tmp = num[k];
	num[k]= num[k+1];
	num[k+1] = tmp;}
}

void Permute (int k)
{
    int     i,temp;
if (k == 2) 
      {nrows++;
	   print();
       return ;
     }
	temp = k-1 ;
    for (i = 0; i <temp; ++i) 
        {	
		leftRotate (temp);
		Permute (temp);

        } 
}


int main ()
{
        cout << "number must be between 3 and 10 \n";
        cin >> n;
		cout  << endl;
 for (int j = 0; j < n; ++j) 
  {  
    num[j] = j+1;
  } 
    Permute (n);
cout << "nrows=" <<nrows << "\n";
}

i just wondering how to change to output from 1D to 2D array. This is because i need to use 2D output to another cases. anyone who could help me?

Recommended Answers

All 3 Replies

You need a conversion equation , look below as an example :

#include<iostream>
using namespace std;

int main(){
	const int R = 4;
	const int C = 4;
	int A[R * C];

	for(int i = 0; i < R*C; i++)
		A[i] = i;

	//print out as row and colum 
	for(int i = 0; i < R; i ++)
	{
		for(int j = 0; j < C; j++)
		{
			cout.width(3);
			cout<<A[j + R*i ] << " ";
		}
		cout << endl;
	}

}

ok. try change at the print function such as follows:

void print()
{
  if (num != 0) {
	  for(int i = 0; i < n; i++)
		  for (int j = 0; j <n; j++) 

  cout << num[j + n*i]<<"";
	cout <<"\n";
		  
		  }
}

but the the output:
number must be between 3 and 10
4
1324000000000000
1324000000000000
1234000000000000
1234000000000000
1324000000000000
1324000000000000
nrows=6
Press any key to continue

there is something wrong with the column numbers. suppose
the column numbers = 4 ( equal to n). i dont know where the mistake.

Not sure what you just said but use brackets :

void print()
{
  if (num != 0) {
	  for(int i = 0; i < n; i++)
      {
		  for (int j = 0; j <n; j++) 
                 {
                      cout << num[j + n*i]<<"";
                 }
   	cout <<"\n";		  
	  }

   }
}
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.