Thread: how to merge two arrays recursevly..

  1. #46
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i cant understand your question?

    what do you mean by
    "cant do"

  2. #47
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by transgalactic2 View Post
    i cant understand your question?

    what do you mean by
    "cant do"
    I mean stuff like it can't call external function like isalpha() etc.; can't use globals, why not?? they aren't external functions.

  3. #48
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by laserlight View Post
    Global variables are unnecessary here. In fact, global variables may complicate the recursion.
    they can be helpful to keep track of array indexes since it seems like that the OP doesn't wanna use pointer and offsets.

  4. #49
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,401
    Quote Originally Posted by itCbitC
    can't use globals, why not?? they aren't external functions.
    Why would global variables be useful in the first place? Pointers are already involved.

    Quote Originally Posted by itCbitC
    they can be helpful to keep track of array indexes since it seems like that the OP doesn't wanna use pointer and offsets.
    No, they just make the array indexes harder to track.
    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

  5. #50
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i need to use the signature that i was given
    i am allowed to use pointers
    i am not allowed to use global variables

    what is the next step??

  6. #51
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by laserlight View Post
    Why would global variables be useful in the first place? Pointers are already involved.


    No, they just make the array indexes harder to track.
    maybe not for the input string literals, but for storing 'em in the array result[].

  7. #52
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,401
    Quote Originally Posted by transgalactic2
    what is the next step??
    What have you done so far? You can post the current code of your merge_strings function, but more importantly right now, post the cases that you have thought of and whether they have been implemented or not.

    Quote Originally Posted by itCbitC
    maybe not for the input string literals, but for storing 'em in the array result[].
    What do you mean?
    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. #53
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by transgalactic2 View Post
    i need to use the signature that i was given
    i am allowed to use pointers
    i am not allowed to use global variables

    what is the next step??
    point noted and thanks for clarifying.

  9. #54
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by laserlight View Post
    What do you mean?
    figured it was easy to store string literals, str1 / str2 into result[], otherwise after storing str1 the op has to find its NULL before appending str2 into result[]. just my 2c.

  10. #55
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,401
    Quote Originally Posted by itCbitC
    figured it was easy to store string literals, str1 / str2 into result[], otherwise after storing str1 the op has to find its NULL before appending str2 into result[]
    Eh, but as you rightly pointed out, this is about merging, not concatenation. result will be filled char by char as the two input strings are recursively traversed. There would be no need to find the terminating null character of str1 after it is stored since it can be retrieved immediately using either the index (or the pointer, as the case may be).
    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. #56
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by laserlight View Post
    Eh, but as you rightly pointed out, this is about merging, not concatenation. result will be filled char by char as the two input strings are recursively traversed. There would be no need to find the terminating null character of str1 after it is stored since it can be retrieved immediately using either the index (or the pointer, as the case may be).
    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; and 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[]; did i manage to cause even more confusion now

  12. #57
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    this is my try of the unified

    i dont know where to merge them ??
    Code:
    #include <stdio.h>
    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

  13. #58
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    First of all you are not handling the case where one string is shorter than the other.

    You merge right before your next call to merge_strings.

    Also, your argument in the recursive step are wrong. They should depend on what you merge into the result.
    Last edited by King Mir; 01-10-2009 at 11:36 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.

  14. #59
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    what to do if
    one string is shorter than the other
    ??

  15. #60
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by transgalactic2 View Post
    what to do if
    one string is shorter than the other
    ??
    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.

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