Hello! here c++ program. my problem is i need to find the permutation is even and odd. However the value +1 (even )and -1(odd) is not appear for each permutation.

``````=
#include    <stdio.h>
#include    <stdlib.h>
#include <iostream>
#include <memory.h>
#define     MAX 100
char        num[MAX + 1];
int         n;
using namespace std;

int sign(int n)
{ int num[MAX];
int g,h,l;
for(g=1,l=1;g<n-1; g++)
for (h=g+1;h<n; h++)
{
if(num[h]<num[g])
l*=-1;
}
return (l);
}

void rightRotate (int k, int n)
{
char    temp [2*MAX], *saveptr;
saveptr = num+k; // i change it here
cout << "k" << k << "\n";
memcpy (temp , saveptr, n);
memcpy (temp + n , saveptr, n); // reverse
memcpy (saveptr, temp+n - 1, n); //
}
void createCyclicMatrix ()
{
char    *p[MAX], temp[2*MAX];
int     i, j, m = 1;
memcpy (temp, num + 1, n); // memory function
memcpy (temp + n , num + 1, n);
for (i = 0; i <= n; ++i)
p[i] = temp + n - i;
m *=sign(n)  ;
cout << "m "<< m << "\n";
for (i = 0; i < n; ++i)
{
/* print the ith row */
for (j = 0; j < n; ++j)
printf ("%d ", *(p[i] + j));
printf ("\n");
/* print the ith column */
for (j = 0; j < n; ++j)
printf ("%d ", *(p[j] + i));
printf ("\n");
}
}

void matrixPermute (int n)
{
int     i, temp;
if  (n == 3 )
{
createCyclicMatrix ();
return;
}
temp = n-1 ;
cout<< "temp" <<temp << endl;
for (i = 1; i<=temp ; ++i) // n-1 times
{
//	cout << "i" << i << "\n";
rightRotate (n-temp +1, temp);
matrixPermute (temp);
}
}
void initiate(char *num, int n)
{
for (int i = 1; i <=n; ++i) {   // i is a position
num[i] = i;
}
} // init

int main ()
{
cout << "number must be between 3 and 10 \n";
cin >> n;
cout  << endl;
initiate(num , n);
matrixPermute (n);
}``````
3
Contributors
4
Replies
5
Views
9 Years
Discussion Span
Last Post by shamila08

The best way to do this is with the modulo operator. (%). It returns the remainder of the quotient of 2 integer numbers.

the function 'sign() is not the problem. the problem is value of each permutation is not apper because i use string copy.

>>here c++ program

I would never guess it from what you posted. If you are writing c++ then why are you using prinf() ?

thanks. its ok. i can change it

``````void createCyclicMatrix ()
{
char    *p[MAX], temp[2*MAX];
int     i, j, m=1;
memcpy (temp, num + 1, n); // memory function
memcpy (temp + n , num + 1, n);
for (i = 0; i <= n; ++i)
p[i] = temp + n - i;
m *=sign(n) ;
cout << "m "<< m << "\n";
for (i = 0; i < n; ++i)
{
/* print the ith row */
for (j = 0; j < n; ++j)
cout <<*(p[i] + j);
cout <<"\n";
/* print the ith column */
for (j = 0; j < n; ++j)
cout << *(p[j] + i);
cout <<"\n";
}
}``````

then recall my problem is to find the value (+1 or -1) for each permutation. please help me.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.