how to merge two arrays recursevly..

This is a discussion on how to merge two arrays recursevly.. within the C Programming forums, part of the General Programming Boards category; i tried to pass the end index of the first to the start index of the second one. but i ...

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    how to merge two arrays recursevly..

    i tried to pass the end index of the first
    to the start index of the second one.
    but i get an endless loop
    why??

    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,lnd;
        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,check2,check3,check4;
    
        if(str1[index1]=='\0')
        {
          return index1;
        }
        else
        {
            result[index1]=str1[index1];
            check=merge_strings(str1,index1+1,str2,index2,result,index3);
        }
    //printf("index1 is:%d str1[index1] is : %c\n ",index1,str1[index1]);
        if(str2[index2]=='\0')
        {
          return 1;
        }
        else
        {
            result[index2]=str2[index2];
            check=merge_strings(str1,index1,str2,check+1,result,index3);
    
        }
       return 1;
    }//end function

  2. #2
    Registered User
    Join Date
    Dec 2008
    Posts
    15
    Do you absolutely have to use recursion to do that?
    What exactly do you mean by 'merge'?
    Last edited by melight; 01-10-2009 at 06:01 AM. Reason: hasty reader

  3. #3
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    yes!

  4. #4
    Registered User
    Join Date
    Dec 2008
    Posts
    15
    you simply need to cat two strings? lets say "abc" merged with "bcd" will be "abcbcd", right?

  5. #5
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    yep
    into the big result array

  6. #6
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i got really close
    it adds them together but i get weird symbols in the end

    http://img58.imageshack.us/my.php?image=41124160tz2.gif

    i dont know how to stop it from accessing cells which beyond the array

    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,lnd;
        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,check2,check3,check4;
    
        if(str2[index2]=='\0')
        {
          return  1;
        }
        else
        {
            result[index3]=str2[index2];
            check=merge_strings(str1,index1,str2,index2+1,result,index3+1);
        }
    
        if(str1[index1]=='\0')
        {
          return 1;
        }
        else
        {
            result[index3]=str1[index1];
            check=merge_strings(str1,index1+1,str2,index2,result,index3+1);
    
        }
       return 1;
    }//end function

  7. #7
    Registered User
    Join Date
    Dec 2008
    Posts
    15
    You should be checking which string you are copying right now
    Code:
    if(str1[index1]=='\0')
        {
          return index1;
        }
    else
        {
            result[index1]=str1[index1];
            check=merge_strings(str1,index1+1,str2,index2,result,index3);
        }
    the first "if" prevents you from reaching the second if after you're done with the first string. Because after you're done with the first string your str1[index1] == '\0' is always true. To prevent this you want to change your ifs in general.
    You also want to go beyond that piece of code only in after you've finished copying the first string in to the result string, and only in ONE recursion (otherwise you'll be copying the second string in many locations many times == mess), not all of them. So make a check for that also

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

  9. #9
    Registered User
    Join Date
    Dec 2008
    Posts
    15
    that's easy, in the second string copy the '\0' in to the result array also, and only then return
    Last edited by melight; 01-10-2009 at 06:34 AM. Reason: bah, too late WTG!

  10. #10
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i dont know how to combine them

    i got this function which checks if the string are alpha type
    and if they are in ascending order regarding their ascii valuen
    1 - if the strings are following those rules.
    0- if not
    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", input);
        }
        else
        {
            printf("%s is invalid.\n",input);
        }
    
    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')
        {
              return 1;
        }
    
        if (((str1[index1]>='a')&&(str1[index1]<='z'))||((str1[index1]>='A')&&(str1[index1]<='Z')))
        {
             check=merge_strings(str1,index1+1,str2,index2,result,index3);
             if (check==0)
             {
                 return 0;
             }
    		 else
    		 {
                  check=1;
    		 }
        }
        else
        {
             return 0;
        }
    ////////////////////////////////////////////////////////
    if (str2[index2]=='\0')
        {
              return 1;
        }
    
        if (((str2[index2]>='a')&&(str2[index2]<='z'))||((str2[index2]>='A')&&(str2[index2]<='Z')))
        {
             check2=merge_strings(str1,index1,str2,index2+1,result,index3);
             if (check2==0)
             {
                 return 0;
             }
    		 else
    		 {
                  check2=1;
    		 }
        }
        else
        {
             return 0;
        }
    ////////////////////////////////////////////////////////////////////////
    if (str1[index1+1]=='\0')
        {
              return 1;
        }
    
        if (str1[index1]>=str1[index1-1])
        {
             check=merge_strings(str1,index1+1,str2,index2,result,index3);
             if (check==0)
             {
                 return 0;
             }
    		 else
    		 {
                  check3=1;
    		 }
        }
        else
        {
             return 0;
        }
    ////////////////////////////////////////////////////////////////
    if (str2[index2+1]=='\0')
        {
              return 1;
        }
    
        if (str2[index2]>=str2[index2-1])
        {
             check=merge_strings(str1,index1,str2,index2+1,result,index3);
             if (check==0)
             {
                 return 0;
             }
    		 else
    		 {
                 return 1;
    		 }
        }
        else
        {
             return 0;
        }
    /////////////////////////////////////////////////////////////////////////////
    
    
    
    }//end function
    i got this function which merges them

    Code:
    int merge_strings(char str1[], int index1,char str2[],int index2, char result[], int index3)
    {
        int check,check2,check3,check4;
    
        if(str2[index2]=='\0')
        {
            result[index3+1]='\0';
          return  1;
        }
        else
        {
            result[index3]=str2[index2];
            check=merge_strings(str1,index1,str2,index2+1,result,index3+1);
        }
    
        if(str1[index1]=='\0')
        {
          return 1;
        }
        else
        {
            result[index3]=str1[index1];
            check=merge_strings(str1,index1+1,str2,index2,result,index3+1);
    
        }
    
       return 1;
    }//end function
    when i put them together its not working
    but separately they work fine.

    how to combine them
    ?

  11. #11
    Registered User
    Join Date
    Dec 2008
    Posts
    15
    again what exactly you need to do with those two? You need to check while copying? Or you can first check, then copy (or vice versa)?

  12. #12
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i need to check first then copy

  13. #13
    Registered User
    Join Date
    Dec 2008
    Posts
    15
    it's kind of hard to tell, you have
    Code:
    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);
    in prototype, but you have only one function in the code, and the other one you show is named the same as the first one. Can you show us the whole file, with both functions?

  14. #14
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by transgalactic2 View Post
    i need to check first then copy
    can you not invoke merge_strings() multiple times from main()? let's say first time you call merge_strings() it checks whether str1 is valid (it's alphabetic and ascending); next time invoke it to check whether input2 is valid and finally invoke it to combine str1 and str2 into result; so the complete algorithm and / or pseudocode might look something like:
    Code:
    start of main()
        read data for str1
        call merge_strings() to check its validity
            while inside merge_strings()
            recursively check if str1 is alphabetic and ascending
        read data for str2
        call merge_strings() to check its validity
            while inside merge_strings()
            recursively check if str2 is alphabetic and ascending
        call merge_strings() to copy str1 into result
            while inside merge_strings()
            recursively copy str1 into result
        call merge_strings() to copy str2 into result
            while inside merge_strings()
            recursively copy str2 into result
    end of main()

  15. #15
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i dont have a unified function
    i have two functions each doing only half
    i need to unify them into one function that does both things

Page 1 of 8 12345678 LastLast
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, 02:30 PM
  2. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 09: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-15-2003, 11:23 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 08:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21