Thread: how to merge two arrays recursevly..

  1. #106
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    my code is intended

    what part is scrambled??

  2. #107
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    where do i need to add

    str1[index1]!='\0'

    ??

  3. #108
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by transgalactic2
    where do i need to add

    str1[index1]!='\0'
    Somewhere in that long expression where you check for non-alpha char or non-increasing order. You also need to check str1[index1 + 1], but you need to do this carefully.
    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. #109
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i added those checks whenever i access +1 cell
    its not working
    when i put
    1st : abdc
    2nd: fghi
    i get
    abd|!! is valid

    ??

    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])&&(str1[index1+1]!='\0'))&&(((str2[index2]<'a')||(str2[index2]>'z'))&&((str2[index2]<'A')||(str2[index2]>'Z'))&&(str2[index2]>str2[index2+1])&&(str1[index1+1]!='\0')))
        {
            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. #110
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by transgalactic2 View Post
    my code is intended

    what part is scrambled??
    He's referring to the fact that some of your lines are very long, breaking the forum table structure, and requiring scrolling sideways to see it all.

    You should put a limit on the horizontal length of each line, wrapping characters that go over to the next line. I recommend an 80 character limit.
    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.

  6. #111
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by transgalactic2
    i added those checks whenever i access +1 cell
    What exactly are "those checks"? List them in point form, in words.
    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

  7. #112
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    To check for increasing order I'd actually check that the next element in both strings is greater than the last result. Checking the next cell is in order has too many special cases.

    But I recommend focusing on making the function handle ordered strings correctly first.
    Last edited by King Mir; 01-11-2009 at 02:17 PM.
    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.

  8. #113
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    solved

  9. #114
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Just for fun here's an alternative to the one you have
    Code:
    #include <stdio.h>
    
    int merge_strings(char [], char []);
    
    int main(void)
    {
        char input[255];
        char input2[255];
        char res[511];
        int index1, index2, flag, ch;
    
        printf("enter the first string: ");
    
        for (index1 = 0; index1 < 254 && (ch = getchar()) != EOF && ch != '\n'; ++index1)
            input[index1] = ch;
        input[index1] = '\0';
    
        flag = merge_strings(input, NULL);
    
        if (flag)
            printf("%s is not valid.\n", input);
        else
            printf("%s is valid.\n", input);
    
        printf("enter the second string: ");
    
        for (index2 = 0; index2 < 254 && (ch = getchar()) != EOF && ch != '\n'; ++index2)
            input2[index2] = ch;
        input2[index2] = '\0';
    
        flag = merge_strings(input2, NULL);
    
        if (flag)
            printf("%s is not valid.\n", input2);
        else {
            printf("%s is valid.\n", input2);
            flag = merge_strings(input, res);
            flag = merge_strings(input2, res+index1);
            printf(" input:  %s\n", input);
            printf("input2:  %s\n", input2);
            printf("result:  %s\n", res);
        }
        return 0;
    }
    
    int merge_strings(char str[], char res[])
    {
        if (str && !res) {
            if (((*str >= 'a') && (*str <= 'z')) || ((*str >= 'A') && (*str <= 'Z'))) {
                if (*(str+1)) {
                    if (*str <= *(str+1)) return (merge_strings(++str, NULL) ? 1 : 0);
                    else return 1;
                } else return 0;
            } else return 1;
        }
        else if (str && res)
            if (*res = *str)
                merge_strings(++str, ++res);
    }

  10. #115
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by King Mir
    To check for increasing order I'd actually check that the next element in both strings is greater than the last result. Checking the next cell is in order has too many special cases.
    Good idea, though I think that only saves one comparison per recursive call since one would then have to check for the special case where it is the first recursive call.

    Quote Originally Posted by King Mir
    But I recommend focusing on making the function handle ordered strings correctly first.
    It should already be doing that (though slightly less nicely than I would like).
    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. #116
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    Quote Originally Posted by itCbitC View Post
    Just for fun here's an alternative to the one you have
    Code:
    #include <stdio.h>
    
    int merge_strings(char [], char []);
    
    int main(void)
    {
        char input[255];
        char input2[255];
        char res[511];
        int index1, index2, flag, ch;
    
        printf("enter the first string: ");
    
        for (index1 = 0; index1 < 254 && (ch = getchar()) != EOF && ch != '\n'; ++index1)
            input[index1] = ch;
        input[index1] = '\0';
    
        flag = merge_strings(input, NULL);
    
        if (flag)
            printf("%s is not valid.\n", input);
        else
            printf("%s is valid.\n", input);
    
        printf("enter the second string: ");
    
        for (index2 = 0; index2 < 254 && (ch = getchar()) != EOF && ch != '\n'; ++index2)
            input2[index2] = ch;
        input2[index2] = '\0';
    
        flag = merge_strings(input2, NULL);
    
        if (flag)
            printf("%s is not valid.\n", input2);
        else {
            printf("%s is valid.\n", input2);
            flag = merge_strings(input, res);
            flag = merge_strings(input2, res+index1);
            printf(" input:  %s\n", input);
            printf("input2:  %s\n", input2);
            printf("result:  %s\n", res);
        }
        return 0;
    }
    
    int merge_strings(char str[], char res[])
    {
        if (str && !res) {
            if (((*str >= 'a') && (*str <= 'z')) || ((*str >= 'A') && (*str <= 'Z'))) {
                if (*(str+1)) {
                    if (*str <= *(str+1)) return (merge_strings(++str, NULL) ? 1 : 0);
                    else return 1;
                } else return 0;
            } else return 1;
        }
        else if (str && res)
            if (*res = *str)
                merge_strings(++str, ++res);
    }
    i tried your code
    it says that string aZ is not valid
    why??

  12. #117
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by transgalactic2 View Post
    i tried your code
    it says that string aZ is not valid
    why??
    IINM you wanted the input sorted in ascending order?

  13. #118
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by transgalactic2 View Post
    solved
    Congrats.
    Quote Originally Posted by laserlight View Post
    Good idea, though I think that only saves one comparison per recursive call since one would then have to check for the special case where it is the first recursive call.
    True.

    It should already be doing that (though slightly less nicely than I would like).
    From the way transgalactic2 replied it seemed that he had not tested the normal case. I guess I was wrong.
    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