suppose i hav 4 numbers in a array a[4]=(0,5,6,7);
now i want to create a newarray 4x3 havin elements as:
0,5,6
0,5,7
0,6,7
5,6,7
plz write a code for the conversion n help me;
thanx
vijay k.
|
Go4Expert Founder
|
![]() |
| 25Sep2006,10:33 | #2 |
|
Go4Expert Member
|
|
| 26Sep2006,10:35 | #3 |
|
Code:
#include <stdio.h>
#include <stdlib.h>
void ComputePermutations(char string[]);
void Permutations(char string[],int stringLength, int level,int stack[]);
void IncrementAndAdjust(int level,int stack[],int stringLength,char string[]);
void DisplayStack(int stack[],int stringLength,char string[]);
int main(int argc, char *argv[])
{
char *string;
printf("\nEnter the string ..\n");
string = (char *)malloc(100 * sizeof(char));
if(!string)
{
printf("\nmemory allocation for string failed exiting ..\n");
goto EXIT;
}
gets(string);
printf("\n\nPermutations..\n");
ComputePermutations(string);
system("PAUSE");
EXIT:
return 0;
}
void ComputePermutations(char string[])
{
int stringLength = 0;
int *stack;
int index = 0;
stringLength = strlen(string);
stack = (int *)calloc(stringLength,sizeof(int));
if(!stack)
{
printf("\nmemory allocation for stack failed exiting ..\n");
goto EXIT;
}
for(index = 0; index < stringLength;index++)
{
stack[0] = index;
Permutations(string,stringLength,0,stack);
}
EXIT:
return;
}
void Permutations(char string[],int stringLength, int level,int stack[])
{
int i = 0;
if(level == stringLength - 1)
{
DisplayStack(stack,stringLength,string);
}
level++;
for(i = 0;i < (stringLength - level);i++)
{
IncrementAndAdjust(level,stack,stringLength,string);
Permutations(string,stringLength,level,stack);
}
printf("\n");
}
void IncrementAndAdjust(int level,int stack[],int stringLength,char string[])
{
int index = 0;
//increment the value of the level by 1
stack[level] += 1;
if(stack[level] >= stringLength)
{
stack[level] = stack[level] % stringLength;
}
// make sure the incremented value is not same as in the stack levels above it
for(index = 0; index < level;index++)
{ if(stack[level] == stack[index]) // a level above the current one has a similar value
{
stack[level] += 1;
if(stack[level] >= stringLength)
{
stack[level] = stack[level] % stringLength;
}
index = -1;
}
}
//stack[level] is appropriately set
}
void DisplayStack(int stack[],int stringLength,char string[])
{
int index = 0;
for(index = 0; index < stringLength;index++)
{
printf(" %c ",string[stack[index]]);
}
}
Last edited by shabbir; 26Sep2006 at 10:39.. Reason: Code formating. |
|
Go4Expert Founder
|
![]() |
| 26Sep2006,10:40 | #4 |
|
I would suggest Kingo you used goto try using the return instead.
Instead of goto EXIT; write return 0; goto is not a good programming practice. |
|
Go4Expert Member
|
|
| 26Sep2006,10:46 | #5 |
|
yeah thts true...........
|

