Thread: how to merge two arrays recursevly..

  1. #61
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    laseright that thats the way to do it
    all the operations on the same time
    thats what i am tryig to do
    Code:
    #include <stdio.h>
    int merge(char str1[], int index1,char str2[],int index2, char result[], int index3);
    int merge_strings(char str1[], int index1,char str2[],int index2, char result[], int index3);
    int main() {
        char input[255];
        char input2[255];
        char result[510];
        int index,flag,ch;
        printf("enter the first string \n");
     for (index = 0; index < 254 && (ch = getchar()) != '\n' && ch >=0; ++index)
        {
            input[index] = ch;
        }
    
        input[index] = '\0';
    
        printf("enter the second string \n");
    
     for (index = 0; index < 254 && (ch = getchar()) != '\n' && ch >=0; ++index)
        {
            input2[index] = ch;
        }
    
        input2[index] = '\0';
    
        flag=merge(input,0,input2,0,result, 0);
    
        if (flag)
        {
            printf("%s is valid.\n",result);
        }
        else
        {
            printf("%s is invalid.\n",result);
        }
    
    return 0;
    }
    
    
    int merge_strings(char str1[], int index1,char str2[],int index2, char result[], int index3)
    {
        int check,check2,check3,check4;
        if ((str1[index1]=='\0') && (str2[index2]=='\0'))
        {
            result[index3] = '\0';
            return 1;
        }
         
     if (((str1[index1]<'a')||(str1[index1]>'z'))&&((str1[index1]<'A')||(str1[index1]>'Z')))
    {
            if(str1[index1]<=str1[index1+1])
            {
                return 0;
            }
    }
    else
    {
        
     check=merge_strings(str1,index1+1,str2,index2+1,result,index3+1);
        
    }
    }//end function

  2. #62
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    I don't think so as you are completely missing the part where str1 and str2 are combined together and stored in the final array result[]. Is this all the code you have for merge_strings()? I thought it had more stuff in it.

  3. #63
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,401
    Quote Originally Posted by itCbitC
    imho the confusion is caused by using same terms for two different things; the op was trying to simply put two functions together and make it into one big function
    Yes, which is why I asked transgalactic2 to clarify since it became clear that somewhere the requirements for merge_strings were changed to what they should have been in the first place. I was rather caught off guard because I thought the problem was still about concatenation

    Quote Originally Posted by itCbitC
    in my last post i was referring to the fact that str1 and str2, after they have passed all checks, are combined together to form one big string and stored in result[]
    Yeah, I think that transgalactic2 thought of this approach originally but did not realise that it will not work, at least not without a flag parameter to determine if merge_strings should perform checking or perform merging. (And the number of recursive calls would be about twice the sum of the input string lengths if there are no errors, which is twice as many as it would be now.)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #64
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by laserlight View Post
    Yeah, I think that transgalactic2 thought of this approach originally but did not realise that it will not work, at least not without a flag parameter to determine if merge_strings should perform checking or perform merging. (And the number of recursive calls would be about twice the sum of the input string lengths if there are no errors, which is twice as many as it would be now.)
    Way back the OP had constructed two functions: one checked if str1 and str2 were alphabetic and in ascending order; the other simply combined the two strings and stored them into an array called result[]. I guess the OP needs to tell us if this is still the case or if the requirements have changed
    Last edited by itCbitC; 01-11-2009 at 01:06 AM.

  5. #65
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,401
    Quote Originally Posted by itCbitC
    Way back the OP had constructed two functions: one checked if str1 and str2 were alphabetic and in ascending order; the other simply combined the two strings and stored them into an array called result[]. I guess the OP needs to tell us if this is still the case or if the requirements have changed
    The requirements have changed to what the OP was originally assigned. Read posts #27 to #29.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #66
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Based on posts 27 - 29 I don't see how the requirements have changed??

  7. #67
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,401
    Quote Originally Posted by itCbitC
    Based on posts 27 - 29 I don't see how the requirements have changed??
    There is only one merge_strings function, not a merge_strings function with a merge helper function. This means that checking the input strings before performing the merge is now a bad approach since recursion must be used. One should check the input strings while performing the merge.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #68
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by laserlight View Post
    There is only one merge_strings function, not a merge_strings function with a merge helper function. This means that checking the input strings before performing the merge is now a bad approach since recursion must be used. One should check the input strings while performing the merge.
    No I don't think I ever had the idea of another function besides merge_strings() to do part of the task recursively. Only when the op split up the processing into two parts I was confused and more so later when the op tried to combine the two functions together. Thanks for the clarification though, guess we are on the same page now.

  9. #69
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    Quote Originally Posted by laserlight View Post
    There is only one merge_strings function, not a merge_strings function with a merge helper function. This means that checking the input strings before performing the merge is now a bad approach since recursion must be used. One should check the input strings while performing the merge.

    what to do??

  10. #70
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by transgalactic2 View Post
    what to do if
    one string is shorter than the other
    ??
    If you get to the end of one string, always insert an element from the other string.

    ....

    Another thing you are missing is that you must return the result of merge_string in the recursive step.

    And by the way, you must realize that since merging must be done in the same step as validating the input, if the input is invalid, then the result will still be partially merged when the function runs.

    Quote Originally Posted by itCbitC View Post
    IMO you are trying to fix too many things at the same time. If you find that there are too many things out-of-whack in your program, start from scratch and this time codify merge_strings() so that both tasks (checking if input is alphabetic / ascending and storing the output in the array result[]) are performed in the same function. Just my 2c.
    He's on the right track. No reason to start over.

    EDIT: but it is necessary to ditch on of the merge functions, and work on the other.
    Last edited by King Mir; 01-11-2009 at 04:06 AM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  11. #71
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i tried to assign the values int he
    Code:
    #include <stdio.h>
    int merge(char str1[], int index1,char str2[],int index2, char result[], int index3);
    int merge_strings(char str1[], int index1,char str2[],int index2, char result[], int index3);
    int main() {
        char input[255];
        char input2[255];
        char result[510];
        int index,flag,ch;
        printf("enter the first string \n");
     for (index = 0; index < 254 && (ch = getchar()) != '\n' && ch >=0; ++index)
        {
            input[index] = ch;
        }
    
        input[index] = '\0';
    
        printf("enter the second string \n");
    
     for (index = 0; index < 254 && (ch = getchar()) != '\n' && ch >=0; ++index)
        {
            input2[index] = ch;
        }
    
        input2[index] = '\0';
    
        flag=merge(input,0,input2,0,result, 0);
    
        if (flag)
        {
            printf("%s is valid.\n",result);
        }
        else
        {
            printf("%s is invalid.\n",result);
        }
    
    return 0;
    }
    
    
    int merge_strings(char str1[], int index1,char str2[],int index2, char result[], int index3)
    {
        int check,check2,check3,check4;
        if ((str1[index1]=='\0') && (str2[index2]=='\0'))
        {
            result[index3] = '\0';
            return 1;
        }
         
     if (((str1[index1]<'a')||(str1[index1]>'z'))&&((str1[index1]<'A')||(str1[index1]>'Z')))
    {
            if(str1[index1]<=str1[index1+1])
            {
                return 0;
            }
    }
    else
    {
        result[index3]=str[index1];
     check=merge_strings(str1,index1+1,str2,index2+1,result,index3+1);
        
    }
    }//end function

  12. #72
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    str is not a variable that you declare. You need to merge from either str1[index1] or str2[index2], such that the result is an sorted sequence of letters.
    Last edited by King Mir; 01-11-2009 at 05:05 AM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  13. #73
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i dont need a sorted sequence in letters as resolt
    i just need
    for example:
    str1 :"abcf"
    str2:"def"

    result:"abcfdef"

  14. #74
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i dont need a sorted sequence in letters as resolt
    i just need
    for example:
    str1 :"abcf"
    str2:"def"

    result:"abcfdef"

    is that what you meant?

  15. #75
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Oh, then your not merging at all, your concatenating. That's even easier, though the check on if the input is ordered is out of place, since the two things are completely unrelated.

    So you merge from the first string untill str1[index1]=='\0', then merge from str2, untill str2[index2]=='\0'.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Merge Module Problems
    By mercury529 in forum Windows Programming
    Replies: 0
    Last Post: 11-29-2006, 03:30 PM
  2. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  3. Do you know...
    By davejigsaw in forum C++ Programming
    Replies: 1
    Last Post: 05-10-2005, 10:33 AM
  4. How to merge 2 arrays?
    By planet_abhi in forum C Programming
    Replies: 3
    Last Post: 02-16-2003, 12:23 AM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM