Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   plz solve this (http://www.go4expert.com/forums/plz-solve-this-t1464/)

ismitevijay 24Sep2006 18:12

plz solve this
 
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 25Sep2006 10:33

Re: plz solve this
 
Try the following threads.
Combinations
All possible combination of a string

kingo 26Sep2006 10:35

Re: plz solve this
 
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]]);
    }
}


shabbir 26Sep2006 10:40

Re: plz solve this
 
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 26Sep2006 10:46

Re: plz solve this
 
yeah thts true...........


All times are GMT +5.5. The time now is 03:47.