ooohh sorry
i do need to sort them in the result array
ooohh sorry
i do need to sort them in the result array
Last edited by transgalactic2; 01-11-2009 at 06:05 AM.
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.Originally Posted by transgalactic2
Actually, why don't you post your homework question in full right now? Word for word, no paraphrasing, nothing missing.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
what to do??
lol
if we can't do anything "aloud", can we do it quietly?
what do i do after the base case?
every recursive sort is filled with while loops
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?Originally Posted by transgalactic2
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.Originally Posted by transgalactic2
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
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:Originally Posted by transgalactic2
Though my function signature is slightly different to be const correct: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 */); } }
Code:int merge_strings(const char *str1, int index1, const char *str2, int index2, char *result, int result_index);
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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); } }
One reason is that you forgot to adjust the result index when recursively calling merge_strings.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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); } }
How does it not work?
Note that you have a logic error here:
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.Code:else if ((str2[index2]=='\0')||(str1[index1]<str2[index2]))
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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
there is no other way to interpret your words into codeCode:else if ((str2[index2]=='\0')||(str1[index1]<str2[index2]))