......................

Showing posts with label Recursion. Show all posts
Showing posts with label Recursion. Show all posts

Program to Generate Possible Unique Combinations of Specific Length for a Given Input

Posted by Unknown On Sunday, May 29, 2011 0 comments

/**************************************************************************
APPLICATION  : Program to Generate Possible Unique Combinations of Specific Length for a Given Input
CODED BY       : Ankit Pokhrel
COMPILED ON : Borland C++ Ver 5.02
DATE             : 2011 - April - 20
**************************************************************************/

#include <iostream.h>
#include <conio.h>
#include <string.h>
#include <fstream.h>

char originalString[15]; //Holds Original String
int n,length;
ofstream outFile; //Output File Pointer

long power(int x,int y) //Function to calculate the Power
{
long retValue = 1;
for(int i = 0;i < y;i++)
   retValue = retValue * x;
return retValue;
}

void Compute(char *str,int pos) //Function to Compute the Possible Unique Combinations
{
static int level = -1; //Current Level or Depth of Recursion
int preLevel; //Previous Level
if(pos >= length) //If the Value of Position(pos) is >= length then Return
  return;

level++; //Increase Level
preLevel = level; //Update Previous Level
for(int i = 0;i < n;i++)
   {
    str[pos] = originalString[i];
    Compute(str,pos+1); //Compute Next Position
    if(preLevel == level)
     {
      cout << str << '\t'; //Print Output
      outFile << str << '\n'; //Save Output to File
     }
   }
}

int main()
{
char temp[15],tempStr[15],fileName[15];
cout << "Enter Characters: ";
cin >> originalString;
cout << "Enter Length: ";
cin >> length; //Length of the String
cout << "\nEnter Filename to Save : ";
cin >> fileName;
outFile.open(fileName); //Open File

n = strlen(originalString); //Find the Length of Original String

cout << endl << "The Number of Possible Unique Combinations : ";
cout << power(length,n) << endl; //Call Functon power()
getch();
int i;
for(i = 0;i < length;i++)
  temp[i] = originalString[0]; //If Input is abc,Start with aaa and Proceed
temp[i] = '\0';

strcpy(tempStr,temp);
cout << endl;
Compute(temp,0); //Find and Print all Unique Combinations
outFile.close(); //Close the File

cout << "\n\n\n\n";
cout.width(38);
cout << "Output saved to : \\" << fileName << endl;
cout.width(45);
cout << "Press any key to halt...";
getch();
return 0;
}

OUTPUT

Lets check this program to generate unique combinations of length 3 for character ABC

Recursion Tree

Output

Download Original File

Unique Combinations

Towers of Hanoi

Posted by Unknown On Monday, August 23, 2010 0 comments

#include "iostream.h"
#include "conio.h"

void Move(int N,char BEG,char AUX,char END)
{
 if(N > 0)
  {
   Move(N - 1,BEG,END,AUX);
   cout << "\nMove top disk from peg " << BEG << " to peg " << END;
   Move(N - 1,AUX,BEG,END);
  }
}

int main()
{
 int n;
 cout << "How many Disks? ";
 cin >> n;
 Move(n,'A','B','C');
 getch();
 return 0;
}


Ackermann Function

Posted by Unknown On Wednesday, August 11, 2010 2 comments

/*******************************************************************************

  --> The Ackermann Function is a very important function originally suggested by Wilhelm Ackermann
in 1928 and later modified by Rozsa Peter.
Wilhelm Ackermann (1896-1942), German mathemat...       Wilhelm Ackermann 
 If m = 0 then A(m,n) = n + 1
 If m != 0 but n = 0 then A(m,n) = A(m - 1,1)
 If m != 0 and n != 0 then A(m,n) = A(m - 1, A(m,n - 1))

 This function is interesting because of its remarkably rapid growth.It grows so fast that it is guaranteed not to have a representation by a formula that uses arithmetic operations such as addition, multiplication, and exponentiation. To illustrate the rate of growth of the Ackermann function, we need only show that with a stack of n 2s in the exponent; A(4,1) = 2^(2 ^ 16) - 3 = (2 ^ 65536) - 3, which exceeds even the number  of atoms in the universe (which is 10^80 according to current theories).

 APPLICATION : Ackermann Function
 CODED BY    : Ankit Pokhrel
 COMPILED ON : Borland C++ Ver 5.02
 DATE     : 2010 - August - 10
*******************************************************************************/

#include "iostream.h"
#include "conio.h"

unsigned long count = 0;
long Ackermann(int m,int n)
Drawing of the analytic extention of the Acker...        Analytical Extension of Ackermann Function
{
 count++;
 if(m == 0)
  return (n + 1);

 else if(m != 0 && n == 0)
  return Ackermann(m - 1,1);

 else
  return Ackermann(m - 1, Ackermann(m,n - 1));
}

long main()
{
 int m,n;
 cout << "Ackermann(M,N)\nEnter values for M and N : ";
 cin >> m >> n;
 if(m < 0 || n < 0)
  {
   cout << "\nEnter Values Greater than Zero";
   getch();
   return 0;
  }

 long result = Ackermann(m,n);
 cout << "\nAckermann(" << m << ',' << n << ") : " << result;
 cout << "\nSteps : " << count;
 getch();
 return 0;
}
Enhanced by Zemanta

Leave Feedback about this BLOG