plz solve this

ismitevijay's Avatar, Join Date: Aug 2006
Light Poster
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.
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Try the following threads.
Combinations
All possible combination of a string
kingo's Avatar
Go4Expert Member
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.
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
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.
kingo's Avatar
Go4Expert Member
yeah thts true...........