![]() |
|
#1
|
|||
|
|||
probably a stupid mistake....lil help appreciatedok this program i am recieve a word via keyboard, and i am to display the permutations. But it won't call my permute function which i haven't finished because of this problem.
CPP / C++ / C Code:
the errors are: error C2664: 'permute' : cannot convert parameter 1 from 'char [10]' to 'char &' ----and--- warning C4267: '=' : conversion from 'size_t' to 'int', possible loss of data any thoughts or anything would be helpful i hope |
|||
|
#2
|
|||
|
|||
|
Quote:
You pass an array as an argument by using its name (that is converted to "pointer to the first element in the array"). I cleaned it up a little. (You don't need any pass-by-reference arguments here.) Note that the C++ standard requires main() to be an int, not void! If you are going to use C-style functions (strlen()), the proper header is <cstring>. CPP / C++ / C Code:
Now, that you can see what your function sees coming in, you are ready to implement its functionality. Regards, Dave |
|
#3
|
|||
|
|||
having the worse trouble with this permutation problemtried ur way dave and thanks, but i also tried another way. I got rid of the errors but now its the actually permuations that are causing a difficulty. i enter in abc and i get like "bcc" and other such things or i'll be missing a letter. I thought something might be wrong with my "temp" variable but i think the entire thing for the else statement in the recursive function should be scrapped
CPP / C++ / C Code:
figured to do this for my algorithm but its not working well if(k i== length of string) display string else{ --for each character position i beetween k and the end of the string { --exchange the characters in position i and k --generate permuatation by calling recursivepermute again with first k+1 characters --restore original string by exachanging position i and k } but i don't think it works like im hoping |
|
#4
|
|||
|
|||
|
Quote:
So: if the string is "this", what is the output that you want to see? If you enter "this", what do you see? There are lots of advantages to using the standard library string:: class, but you must realize that they are not exactly the same as the old-fashioned c-style strings. They are easier to copy and to append, etc., and you never run out of room, but storing chars into string:: positions is risky unless you know exactly where they are going. (You can also use functions like str.length() to find the length of the string., etc.) In your program you find the length of the string, then store something in str[length]. That's not good with c-strings or c++ strings. Whenever you store something in an array, you must be sure that it's within the range of the array. If you really want to manipulate arrays of char rather than use string class member functions, I think you should stick to arrays of char. By the way, void main() is still illegal. Maybe your present compiler lets you get away with it, but some day ... oh, well; never mind. Regards, Dave |
|
#5
|
|||
|
|||
|
Here is some explanation that I threw together. The idea of recursion is a little hard for me to get my brain around, but here's how I look at the permutation problem:
Suppose we had a string with one two chars, say "ab", and we want to print all permutations of the characters. We would print the original string, then swap the characters: "ab" "ba" Now, suppose we had a string of three characters, say "abc". We would consider the original string "abc" Now, it's a little more interesting: with 'a' as the first character, print all permutations of the other characters: "abc" "acb" Now swap 'a' with the next character, 'b', so we have "bac", then print all permutations of the other characters: "bac" "bca" Next, return the string to its original form "abc", then swap "a" with the next character, 'c', so we have "cba". Then print all permutations of the other characters: "cba" "cab" So the algorithm works pretty much as your words, but your implementation didn't quite do the same. In the first place, if the string has length L for example, then the first character is str[0], and the last character is str[L-1], as I mentioned in my last post. In your function recursivepermut(), the argument k is the offset into the string, so that it considers only str[k] up to and including str[length-1]. So the comparison should be CPP / C++ / C Code:
Now, in the loop, you want to go from k to length-1, not 0 to length-1, so it should look something like CPP / C++ / C Code:
I hope this is helpful. Regards, Dave |
|
#6
|
|||
|
|||
|
dave your help is appreciated greatly. Your code helped out a lot and helped me solve my problem rather effieciantly. Thanks for your help and for even taking time you didn't need to take to write the code.
|
|
#7
|
|||
|
|||
|
Quote:
Well, I think you pretty much had things in place. I hope the explanation helped show the basic approach, and the little details of what happens when the program is indexing into an array. Regards, Dave |
|
#8
|
|||
|
|||
|
Quote:
it definitly helped, thanks a lot. |
|
#9
|
||||
|
||||
|
I'm not normally one to give CPR to dead issues, but just in case you were wondering about the compiler error in your first post... it is caused by the fact that arrays are always passed by address. Therefore, you do not need the ampersand.
__________________
-Aaron |
|
#10
|
||||
|
||||
|
Quote:
I for one appreciate the "follow-up" post. On GIDForums™, there are no dead issues - infact, the older the thread, the more alive it is... __________________
J de Silva Learning Journal | GIDForums™ | GIDNetwork™ | GIDWebhosts™ | GIDSearch™ |
Recent GIDBlog
Not selected for officer school by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Need help with a program if anyone can help it would be appreciated | Krc784 | C++ Forum | 1 | 03-Nov-2004 20:55 |
| Minor Problem with my program. Any help greatly appreciated. | agentxx04 | C Programming Language | 6 | 24-Oct-2004 15:04 |
| stupid untalented b****! (no it's not britney or hilary duff) | machinated | Open Discussion Forum | 12 | 22-Jun-2004 07:44 |
| newbie with stupid questions | JUNK KED | MySQL / PHP Forum | 1 | 16-Oct-2003 08:58 |
| Thanx for the sql query, but there must be a little mistake | norok | MySQL / PHP Forum | 13 | 30-Jun-2003 06:30 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The