GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 05-Dec-2004, 11:47
wbsquared03 wbsquared03 is offline
New Member
 
Join Date: Oct 2004
Posts: 14
wbsquared03 is on a distinguished road

probably a stupid mistake....lil help appreciated


ok 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:
#include <iostream>
#include <string>
#include<stdio.h>

using namespace std;


void permute(char&, int&);

void main()
{
	char string[10];
	int length=0;
	cout<<"Enter in the string: ";
	cin>>string;
	length=strlen(string);
	cout<<length;
	permute(string, length);   //won't call dunno WHY??

}

void permute(char& string, int& length)
{
	/*int current=0;
	for(int i=0;i<length;i++)
	{
		if(current==length-1)
		{
			cout<<string;
		}
	}*/

}

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  
Old 05-Dec-2004, 12:01
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,496
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by wbsquared03
ok 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:
#include <iostream>
#include <string>
#include<stdio.h>

using namespace std;


void permute(char&, int&);

void main()
{
	char string[10];
	int length=0;
	cout<<"Enter in the string: ";
	cin>>string;
	length=strlen(string);
	cout<<length;
	permute(string, length);   //won't call dunno WHY??

}

void permute(char& string, int& length)
{
	/*int current=0;
	for(int i=0;i<length;i++)
	{
		if(current==length-1)
		{
			cout<<string;
		}
	}*/

}

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

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:

#include <iostream>
#include <cstring>

using namespace std;

void permute(char *, int);

int main()
{
  char string[100];
  int length;

  cout << "Enter in the string: ";
  cin >> string;
  length = strlen(string);
  cout << "length = " << length << endl;
  permute(string, length);

  return 0;
}

void permute(char *string, int length)
{
  cout << "In permute: string: <" << string << ">, length = " << length << endl;
}

Now, that you can see what your function sees coming in, you are ready to implement its functionality.

Regards,

Dave
  #3  
Old 05-Dec-2004, 13:41
wbsquared03 wbsquared03 is offline
New Member
 
Join Date: Oct 2004
Posts: 14
wbsquared03 is on a distinguished road

having the worse trouble with this permutation problem


tried 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:
void main()
{
	string str;
	cout<<"Enter in the string: ";
	cin>>str;
	listpermut(str);
	
	
}

void listpermut(string str)
{
	recursivepermut(str, 0);
}

void recursivepermut(string str, int k)
{
	int length=0;
	for (int i=0;str[i]!='\0';i++)
    {
        length++;
    }
	//cout<<length;

	if(k==length)
	{
		cout<<str;
		
	}
	else
	{
		char temp;
		temp=str[0];

		for(int i=0;i<length;i++)
		{			
			temp=str[length];
			str[length]=str[i];             //dunno what to do 
			str[i]=temp;                   //won't do what i want it  to lol               
			recursivepermut(str,k+1);
			temp=str[length];
			str[length]=str[i];
			str[i]=temp;
			

		}	
		cout<<str<<endl;
	}
	
}

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  
Old 05-Dec-2004, 14:05
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,496
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by wbsquared03
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

...

but i don't think it works like im hoping


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  
Old 05-Dec-2004, 16:39
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,496
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
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:
if (k == length-1) {
  cout << str;
}

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:
  for (i = k; i < length; i++) {
    temp = str[k]; // swap k with next in loop
    str[k] = str[i];
    str[i] = temp;

    print_permutations(str, k+1);

    temp = str[k]; // swap back for next iteration
    str[k] = str[i];
    str[i] = temp;
  }
  return;

I hope this is helpful.

Regards,

Dave
  #6  
Old 05-Dec-2004, 18:45
wbsquared03 wbsquared03 is offline
New Member
 
Join Date: Oct 2004
Posts: 14
wbsquared03 is on a distinguished road
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  
Old 05-Dec-2004, 19:06
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,496
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by wbsquared03
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.

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  
Old 05-Dec-2004, 22:59
wbsquared03 wbsquared03 is offline
New Member
 
Join Date: Oct 2004
Posts: 14
wbsquared03 is on a distinguished road
Quote:
Originally Posted by davekw7x
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

it definitly helped, thanks a lot.
  #9  
Old 05-Dec-2004, 23:34
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
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  
Old 06-Dec-2004, 15:43
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough
Quote:
Originally Posted by aaroncohn
I'm not normally one to give CPR to dead issues...

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...
 
 

Recent GIDBlogNot selected for officer school by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 18:43.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.