Actually it was one of the assignments given to me in lab.. here is the code
#include <iostream.h>
#include <conio.h>
using namespace std;
int a[20][20][20]; // Global declration to make every cell's default value
to zero
// 3D matrix to implement the algorithm
void combinations(int number) // function that accepts the number whose
combination , you
{
int i, j, k, larg, m, n, flag;
for(i=1; i<=number; i++)
a[i][0][0]=i;
i=1;
while(i<=number)
{
// first row of each number is copied by that number
// external loop to access no. whose combination are inserted
flag=1;
j=1;
do{
//position of rows that is being modified
// inner loop that will run through i
for(k=0; a[j][k][0]!=0; k++) // innermost loop that will check for
largest/last no.
{
for(larg=0; a[j][k][larg]!=0; larg++); //to check last value
larg--;
if(i-j>a[j][k][larg])
{
for(m=0; m<=larg; m++)
a[i][flag][m] = a[j][k][m];
a[i][flag][m] = i-j;
flag++;
}
// comapring last value with difference
// copy all existing values to current number
// insert difference between numbers
//increase the pointer to insert next combination
}
j++;
}while(j<=i);
i++;
}
k=number;
for(i=0; a[k][i][0]!=0; i++) {
cout<<a[k][i][0];
for(j=1; a[k][i][j]!=0; j++)
cout<<"+"<<a[k][i][j];
// printing of all combination of number
cout<<"\n";
}
}
int main()
{
int number;
cout<<"Enter the natural number: ";
cin>>number;
combinations(number);
getch();
return 0;
}
//input the number whose combination you want
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/algogeeks/-/CsTCslXuC88J.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.