hie guys have a textbook that am reading there is a code that i need some help understanding better....can anyone explain to me line by line whats really going on....

No need to explain the main function though.......its a program that calculates permutation of whole numbers...Cheers!

#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;

int count=0;


void dis(char a[],int n)
{
   for(int jk=n-1;jk>=0;jk--)
     {
                 cout<<a[jk];
    }
cout<<" ";
}



void shift(char a[],int n)
{
 char s;
 s=a[n-1];
 for(int i=n-1;i>0;i--)
   a[i]=a[i-1];
 a[0]=s;
}


void per(char a[],int m,int n)
{
for(int i=0;i<m;i++)
  {
   if(m>1)
   {
    shift(a,m);
    per(a,m-1,n);    
   }
   else
    {
    dis(a,n);       
    count++;
    }
   }
}


void main()
{
  int n;
 

  cout<<"Enter a whole number : ";
  cin>>n;

  char *a = new (char[n]);

  for(int i=0;i<n;i++)
     a[i] = n-i+48;



  cout<<"Possible permutations are :  ";
  per(a,n,n);
  cout<<"Total No Of Permutations Is "<<count;
  _getch();
}

Use of the "dis" function (or a series of cout statements) at strategic points in your program will let you trace it through step by step (or step through it using a debugger if you have one). If you then write a couple of sentences, someone can look at it for you and tell you if you are correct.

I know it's not the answer you wanted, but you'll learn a lot more doing it that way.

Also, main() always returns an int, according to the standard.

Edited 6 Years Ago by jonsca: n/a

ok i did what you said i should do and i managed to make out these comments ..could you help me add flesh to them and probably make more??

//header files
#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;

int count=0;

//func to display the sequence
void dis(char a[],int n)
{
   for(int jk=n-1;jk>=0;jk--)
     {
                 cout<<a[jk];
    }
cout<<" ";
}

//function to left shift last n elements
//of the array by one position
void shift(char a[],int n)
{
 char s;
 s=a[n-1];
 for(int i=n-1;i>0;i--)
   a[i]=a[i-1];
 a[0]=s;
}

//func to find all possible arrangements
void per(char a[],int m,int n)
{
for(int i=0;i<m;i++)
  {
   if(m>1)
   {
    shift(a,m);
    per(a,m-1,n);    //genetrating the tree
   }
   else
    {
    dis(a,n);       //displaying the leaf nodes of the tree
    count++;
    }
   }
}

Looks pretty good so far.

1)Which way does "dis" display the elements?

2)Denote the type of function "per" (hint, line 38)

This article has been dead for over six months. Start a new discussion instead.