Thread: how to merge two arrays recursevly..

  1. #76
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    ooohh sorry
    i do need to sort them in the result array
    Last edited by transgalactic2; 01-11-2009 at 06:05 AM.

  2. #77
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by transgalactic2
    i dont need a sorted sequence in letters as resolt
    So your response to melight remained relevant despite the introduction of increasing sequences - did your teacher also tell you to name the function merge_strings? You know, next time, present your actual requirements up front. Copy your entire homework question.

    Actually, why don't you post your homework question in full right now? Word for word, no paraphrasing, nothing missing.
    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

  3. #78
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    ok.
    write a recursive function called
    int merge_strings(char str1[], int index1,char str2[],int index2, char result[], int index3)
    the function checks if the string contains letters of a-z A-Z type.
    and if the letters in the strings are sorted by ascii value.
    if it does then return 1 and return a sorted merged result string.
    if not return 0 and it doesnt matter whats in the result string.
    you are not aloud to use external helping functions.
    you are not aloud to use numerical functions.
    you are not aloud to use loops ,only in a recursive way.

    and the last update from he teacher: i am not aloud to use pointers
    Last edited by transgalactic2; 01-11-2009 at 07:20 AM.

  4. #79
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    what to do??

  5. #80
    Registered User TriTon's Avatar
    Join Date
    Dec 2008
    Posts
    5
    lol
    if we can't do anything "aloud", can we do it quietly?

  6. #81
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    what do i do after the base case?

  7. #82
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    every recursive sort is filled with while loops

  8. #83
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by transgalactic2
    what do i do after the base case?
    Just to recap: you know that the base case comes in two sub-cases, one where you return 1 and the other where you return 0. Have you fully implemented the base case?

    Quote Originally Posted by transgalactic2
    every recursive sort is filled with while loops
    The idea here is that assuming all goes well, you have two sequences (i.e., the input strings) that are already sorted. Merging two sorted sequences is simpler than sorting an unsorted sequence: you just compare the current element from one sequence with the current element from the other sequence, and then insert them into the result sequence depending on the comparison. If you reach the end of one sequence, you just fill in with the rest of the other sequence.

    Now, your task is to write this using recursion. Since you handle the base case first, you know for sure that at this point at least one of the input strings still has at least one character to process. You also know that the character that is being processed is alphabetic, and it is less than the next character (if any). All that remains is to determine which input string to get the character from. Once you have determined that, you write the character to the result... and then you call merge_strings to continue the processing and return the value from that call.
    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. #84
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i tried to build this again
    exactly like you said
    its not working??

    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)
        {
            int check;
    		if ((str1[index1]=='\0') && (str2[index2]=='\0'))
            {
                result[index3] = '\0';
                return 1;
            }
    		else
    		{
    		  if (( ((str1[index1]>='a')&&(str1[index1]<='z')) || ((str1[index1]>='A') && (str1[index1]<='Z')))&&(((str2[index2]>='a')&&(str2[index2]<='z')) || ((str2[index2]>='A') && (str2[index2]<='Z'))|| ((str1[index1]=='\0') || (str2[index2]=='\0'))))
    			{
    			  if ((str1[index1+1]>=str1[index1])&&(str2[index2+1]>=str1[index2]))
    			  {
    			      if(str1[index1]>=str2[index2])
    			      {
    			         result[index3]=str2[index2];
    			         check=merge_strings(str1,index1,str2,index2+1,result,index3+1);
    
    			      }
    			      else
    			      {
    			          result[index3]=str1[index1];
                                        check=merge_strings(str1,index1+1,str2,index2,result,index3+1);
    			      }
    			 }
    		      else
    		      {
    		         return 0;
    		      }
    		}
    
    	}
    }
    Last edited by transgalactic2; 01-11-2009 at 11:34 AM.

  10. #85
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by transgalactic2
    i tried to build this again
    exactly like you said
    its not working?
    Among other things, you never actually return the check variable. Personally, I would do without it. My own test program is written along these lines:
    Code:
    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;
        }
        else if (/* non-alpha char or non-increasing order detected */)
        {
            return 0;
        }
        else if (/* end of str2 or str1's char is less than str2's char */)
        {
            result[index3] = str1[index1];
            return merge_strings(/* such that the next char for str1 is processed */);
        }
        else
        {
            result[index3] = str2[index2];
            return merge_strings(/* such that the next char for str2 is processed */);
        }
    }
    Though my function signature is slightly different to be const correct:
    Code:
    int merge_strings(const char *str1, int index1, const char *str2, int index2,
        char *result, int result_index);
    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. #86
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i wrote this function
    i get no result
    ??
    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]))
        {
            result[index3] = str1[index1];
            return merge_strings(str1,index1+1,str2,index2,result,index3);
        }
        else
        {
            result[index3] = str2[index2];
            return merge_strings(str1,index1,str2,index2+1,result,index3);
        }
    }

  12. #87
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    One reason is that you forgot to adjust the result index when recursively calling merge_strings.
    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

  13. #88
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i fixed that
    i added +1 on the recursive calls
    its not working

    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]))
        {
            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);
        }
    }

  14. #89
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    How does it not work?

    Note that you have a logic error here:
    Code:
    else if ((str2[index2]=='\0')||(str1[index1]<str2[index2]))
    Suppose that you are not yet at the end of the second string, but are at the end of the first string. Then str2[index2] == '\0' is false, but str1[index1] < str2[index2] is true because str1[index] is '\0'. As such, you should include a check for str1[index1] != '\0' in that 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. #90
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i get this
    http://img171.imageshack.us/img171/2169/84325187bm6.gif

    you said
    else if (/* end of str2 or str1's char is less than str2's char */)

    thats what i did

    Code:
    else if ((str2[index2]=='\0')||(str1[index1]<str2[index2]))
    there is no other way to interpret your words into code

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