1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

plz solve this

Discussion in 'C' started by ismitevijay, Sep 24, 2006.

  1. ismitevijay

    ismitevijay New Member

    Joined:
    Aug 26, 2006
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
  3. kingo

    kingo New Member

    Joined:
    Aug 30, 2006
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    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 a moderator: Sep 26, 2006
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    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.
     
  5. kingo

    kingo New Member

    Joined:
    Aug 30, 2006
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    yeah thts true...........
     

Share This Page