Thread: how to merge two arrays recursevly..

  1. #91
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,401
    Quote Originally Posted by transgalactic2
    you said
    else if (/* end of str2 or str1's char is less than str2's char */)

    thats what i did
    You need to think instead of blindly implementing a "pseudo-code" comment. If you are at the end of str1, comparing with str1's char with str2's char is wrong because there is no str1 char actually in the substring under consideration.
    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

  2. #92
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i changed it to
    Code:
        else if ((str2[index2]=='\0')||((str1[index1]<str2[index2])&&(str2[index2]!='\0')))
    i get the same result as before

    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_strings(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)
    {
        if ((str1[index1] == '\0') && (str2[index2] == '\0'))
        {
            result[index3] = '\0';
            return 1;
        }
        /* non-alpha char or non-increasing order detected */
        else if ((((str1[index1]<'a')||(str1[index1]>'z'))&&((str1[index1]<'A')||(str1[index1]>'Z'))&&(str1[index1]>str1[index1+1]))&&(((str2[index2]<'a')||(str2[index2]>'z'))&&((str2[index2]<'A')||(str2[index2]>'Z'))&&(str2[index2]>str2[index2+1])))
        {
            return 0;
        }            /* end of str2 or str1's char is less than str2's char */
        else if ((str2[index2]=='\0')||((str1[index1]<str2[index2])&&(str2[index2]!='\0')))
        {
            result[index3] = str1[index1];
            return merge_strings(str1,index1+1,str2,index2,result,index3+1);
        }
        else
        {
            result[index3] = str2[index2];
            return merge_strings(str1,index1,str2,index2+1,result,index3+1);
        }
    }

  3. #93
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,401
    Quote Originally Posted by transgalactic2
    i changed it to
    You are supposed to check str1's char, not str2's char since you already did that in the left hand side of the expression.
    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. #94
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i changed it to
    Code:
     else if ((str2[index2]=='\0')||((str1[index1]<str2[index2])&&(str1[index1]!='\0')))
    now its just sticks those to string together(the result array is not sorted)
    it says that the input is legal even if they are not legal
    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_strings(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)
    {
        if ((str1[index1] == '\0') && (str2[index2] == '\0'))
        {
            result[index3] = '\0';
            return 1;
        }
        /* non-alpha char or non-increasing order detected */
        else if ((((str1[index1]<'a')||(str1[index1]>'z'))&&((str1[index1]<'A')||(str1[index1]>'Z'))&&(str1[index1]>str1[index1+1]))&&(((str2[index2]<'a')||(str2[index2]>'z'))&&((str2[index2]<'A')||(str2[index2]>'Z'))&&(str2[index2]>str2[index2+1])))
        {
            return 0;
        }            /* end of str2 or str1's char is less than str2's char */
        else if ((str2[index2]=='\0')||((str1[index1]<str2[index2])&&(str1[index1]!='\0')))
        {
            result[index3] = str1[index1];
            return merge_strings(str1,index1+1,str2,index2,result,index3+1);
        }
        else
        {
            result[index3] = str2[index2];
            return merge_strings(str1,index1,str2,index2+1,result,index3+1);
        }
    }

  5. #95
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,401
    Quote Originally Posted by transgalactic2
    now its just sticks those to string together(the result array is not sorted)
    it says that the input is legal even if they are not legal
    What test input did you use?
    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. #96
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by transgalactic2 View Post
    now its just sticks those to string together(the result array is not sorted)
    it says that the input is legal even if they are not legal
    what do you mean by the above? can you provide an example of the output you're getting.

  7. #97
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    this is a print screen of the input

    http://img258.imageshack.us/img258/6168/46740647tq4.gif

    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_strings(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)
    {
        if ((str1[index1] == '\0') && (str2[index2] == '\0'))
        {
            result[index3] = '\0';
            return 1;
        }
        /* non-alpha char or non-increasing order detected */
        else if ((((str1[index1]<'a')||(str1[index1]>'z'))&&((str1[index1]<'A')||(str1[index1]>'Z'))&&(str1[index1]>str1[index1+1]))&&(((str2[index2]<'a')||(str2[index2]>'z'))&&((str2[index2]<'A')||(str2[index2]>'Z'))&&(str2[index2]>str2[index2+1])))
        {
            return 0;
        }            /* end of str2 or str1's char is less than str2's char */
        else if ((str2[index2]=='\0')||((str1[index1]<str2[index2])&&(str1[index1]!='\0')))
        {
            result[index3] = str1[index1];
            return merge_strings(str1,index1+1,str2,index2,result,index3+1);
        }
        else
        {
            result[index3] = str2[index2];
            return merge_strings(str1,index1,str2,index2+1,result,index3+1);
        }
    }

  8. #98
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,401
    That implies that your "non-alpha char or non-increasing order detected" code is incorrect. Now is a good time to practice your debugging skills.
    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

  9. #99
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Whoops! forgot to ask you how the output should look like because I can barely make out the image you posted.
    Last edited by itCbitC; 01-11-2009 at 01:25 PM.

  10. #100
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,401
    Quote Originally Posted by itCbitC
    Whoops, forgot to ask you how the output should look like because I can barely make out the image you posted.
    The inputs were "gabc" and "gsfg" and the main output was "dgabcsfg" instead of an invalid input message.

    Basically, transgalactic2 forgot to check for null characters (again).
    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

  11. #101
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by laserlight View Post
    The inputs were "gabc" and "gsfg" and the main output was "dgabcsfg" instead of an invalid input message.

    Basically, transgalactic2 forgot to check for null characters (again).
    Ah! well that would explain why.

  12. #102
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i noticed that it skips this line
    although it should go inside of it

    Code:
     else if ((((str1[index1]<'a')||(str1[index1]>'z'))&&((str1[index1]<'A')||(str1[index1]>'Z'))&&(str1[index1]>str1[index1+1]))&&(((str2[index2]<'a')||(str2[index2]>'z'))&&((str2[index2]<'A')||(str2[index2]>'Z'))&&(str2[index2]>str2[index2+1])))
        {
    i cant see the problem in it
    ??

  13. #103
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    what null characters?

    i specifically told the not legal ascii ranges
    where is my mistake
    regarding the null thing?

  14. #104
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,401
    Quote Originally Posted by transgalactic2
    what null characters?
    As in '\0', the terminating null character for a string. This is basically a variant of the problem that you solved just a while ago by adding the str1[index1]!='\0' expression.
    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

  15. #105
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    I know I'm jumping into this thread late, but it would help if you indented the code so it doesn't scroll off the page making it exceedingly hard to read and understand.

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