I need to find all the possible permutations of a list and I am completely stumped. The specification for the function is: -
void allPerms(intListType *toOrigList, intListType *answer)
-
where *toOrigList is a pointer to a list and *answer is an array of lists. intListType has several member functions and I will post them if you want them, but for the most part they are barely used in this function (which is not a member function). I'm having trouble outputting the permutations because the program keeps crashing. I cannot change the specification of the function and if you want the rest of the program I will post it. Anyway, here is what I have of the function so far: -
#include "allPerms.h"
-
#include <string>
-
#include <cstddef>
-
#include <iostream>
-
using namespace std;
-
-
void allPerms(intListType *toOrigList, intListType *answer)
-
//Pre: list is not empty
-
//Post: computes all permutations of a given list
-
{
-
int length = toOrigList->getLength();
-
-
if(length == 1){
-
toOrigList->copyTo(&answer[0]);
-
}
-
else{
-
toOrigList->resetList();
-
-
for(int i = 0; i < length; i++){
-
intListType smallerList;
-
int amount = factorial(length-1);
-
intListType array[amount];
-
int first = toOrigList->getNextInt();
-
-
toOrigList->copyTo(&smallerList);
-
smallerList.deleteInt(first);
-
allPerms(&smallerList, array);
-
-
for(int j = 0; j < amount; j++){
-
array[].copyTo(&answer[]);
-
}
-
}
-
}
-
}
-
-
int factorial(int n)
-
//Pre: enter integer
-
//Post: returns factorial of number
-
{
-
int answer;
-
-
if(n < 0){
-
throw string("no factorial for numbers less than 0");
-
}
-
else if(n == 0){
-
answer = 1;
-
}
-
else{
-
answer = n*factorial(n-1);
-
}
-
-
return answer;
-
}
-
Thank you for any help you may be able to provide!
9 3396
array[].copyTo(&answer[]);<----the arrays are empty because I don't know what to put in them to get it to work properly
First, you are dealing with permutations rather than combinations. So each permutation is an ordered variation of the original set. Like 4321 is a permutation of 1234 but 432 is not.
That said, this code: - intListType array[amount];
requires intListType to have a cnstructor to initialize the array. But each element of the array musy be allocated enought memory to hold a permutation. This implies intListType knows the number of elements in the permutation. This will be hard to do with no argument for it.
Also this code: - intListType array[amount];
shows allocation on the stack. Stack memory is limited an this sort of thing can cause a crash.You should do all of your allocations on the heap using the "new" operator.
There are n! permutations of a set. So your answers can be in an array of n! elements provided each element is allocated (on the heap) a size equal to the size of the original set. You need to do this before copying stuff around.
So you're saying that I should make all the arrays like this? -
intListType array = new intListType[amount];
-
Also, do you want me to post my intListType class or the constructor for it?
Yes, you should do that but also each element of array will hold a permutation so you need to allocate memory for each element. Since i don't have your code look at this:
1) assume my permutation set is 10 integers.
2) So I need an aray of 10! elements where each element is itself an array of 10 int.
3) 10! is 3,628,800 - int (*array)[10] = new int[3628800][10];
Now array[0] is an array of 10 int and array[0] can hold one permutation.
In this scheme individual elements of the permutation would be
array[0][i] where i varies from 0 to 9.
Of course, since this is C++ you should be using vector since vector is implemented as an array. However, I didn't want to muddy the water with refinements when the code is not yet working.
For more info on how this array is allocated read: http://bytes.com/topic/c/insights/77...rrays-revealed
First off, very nice article I wish I knew about that when I was learning multidimensional arrays. Secondly, I'm getting confused because I'm new to programming and very new to using recursion. I'm trying to make variables to do what you suggested and I can't because I'm having trouble tracking what my code is doing. Right now I gave my array constant values, but I don't think that works do to the nature of my program. If I have: (using a list of 1,2,3) - intListType (*array)[3] = new intListType[6][3];
I get a compile error. Here is my code so you can give me a more concrete example/directions if you're up to it. I appreciate your help and thank you if you feel like reading through all this.
intList.h -
#include <string>
-
using namespace std;
-
-
struct nodeType;
-
-
class intListType
-
{
-
public:
-
intListType();
-
int getLength();
-
void putInt(int x);
-
//Pre: x is not already in list
-
//Post: insert x at the head of the list
-
void resetList();
-
//PostL set current location to be off the end of the list
-
int getNextInt();
-
//Pre: list has not been changed since last reset
-
// current location is not the end of the list
-
//Post: advance current location and return int at next location
-
void deleteInt(int x);
-
//Pre: x is on the list
-
//Post: delete x
-
bool isIn(int x);
-
//Pre: list was initialized
-
//Post: returns true or false if x in list
-
void copyTo(intListType *toNew);
-
string toString();
-
~intListType();
-
//Post: free up the memory used by the list
-
// and set the header of the list to NULL
-
private:
-
nodeType* listHead;
-
nodeType* currentLocation;
-
//points to a node in the list, or NULL if off the end of the list
-
int length;
-
};
-
intList.cpp
allPerms.h -
#include "intList.h"
-
-
void allPerms(intListType *toOrigList, intListType *answer);
-
//Pre: list is not empty
-
//Post: computes all permutations of a given list
-
-
int factorial(int n);
-
//Pre: enter integer
-
//Post: returns factorial of number
-
allPerms.cpp -
#include "allPerms.h"
-
#include <string>
-
#include <cstddef>
-
#include <iostream>
-
using namespace std;
-
-
void allPerms(intListType *toOrigList, intListType *answer)
-
//Pre: list is not empty
-
//Post: computes all permutations of a given list
-
{
-
// int length = toOrigList->getLength();
-
//
-
// if(length == 1){
-
// toOrigList->copyTo(&answer[0]);
-
// }
-
// else{
-
// toOrigList->resetList();
-
//
-
// for(int i = 0; i < length; i++){
-
// intListType smallerList;
-
// int amount = factorial(length-1);
-
// intListType array[amount];
-
// int first = toOrigList->getNextInt();
-
//
-
// toOrigList->copyTo(&smallerList);
-
// smallerList.deleteInt(first);
-
// allPerms(&smallerList, array);
-
//
-
// for(int j = 0; j < amount; j++){
-
// array[].copyTo(&answer[]);
-
// }
-
// }
-
// }
-
int length = toOrigList->getLength();
-
-
if(length == 1){
-
toOrigList->copyTo(&answer[0]);
-
}
-
else{
-
toOrigList->resetList();
-
-
for(int i = 0; i < length; i++){
-
intListType smallerList;
-
int amount = factorial(length-1);
-
intListType (*array)[6] = new intListType[3][6];
-
int first = toOrigList->getNextInt();
-
-
toOrigList->copyTo(&smallerList);
-
smallerList.deleteInt(first);
-
allPerms(&smallerList, array);
-
-
for(int j = 0; j < amount; j++){
-
array[i][j].copyTo(&answer[i]);
-
}
-
}
-
}
-
}
-
-
int factorial(int n)
-
//Pre: enter integer
-
//Post: returns factorial of number
-
{
-
int answer;
-
-
if(n < 0){
-
throw string("no factorial for numbers less than 0");
-
}
-
else if(n == 0){
-
answer = 1;
-
}
-
else{
-
answer = n*factorial(n-1);
-
}
-
-
return answer;
-
}
-
allPermsDr.cpp -
#include "allPerms.h"
-
#include <iostream>
-
using namespace std;
-
-
void guts()
-
{
-
int n;
-
cout << "Enter a positive integer: ";
-
cin >> n;
-
cout << endl;
-
-
intListType aList;
-
aList.resetList();
-
for(int i = 1; i <= n; i++){
-
aList.putInt(i);
-
}
-
-
intListType *allPermutations;
-
int f = factorial(n);
-
allPermutations = new intListType[f];
-
allPerms(&aList, allPermutations);
-
-
for(int p = 0; p < f; p++){
-
cout << (allPermutations[p]).toString() << endl;
-
}
-
}
-
-
int main()
-
{
-
try{
-
guts();
-
}catch(string message){
-
cout << "Exception thrown: " << message << endl;
-
}
-
}
-
Thank you
Your code compiles for me on Visual Studio.NET 2008.
But I had to make one change: You are missing a semicolon at the end of the struct nodeType definition.
Plus in C++ to define an array of class intListType objects you need both a default constructor and a destructor. I used your default ctor but had to code a dtor.
Wait so with those changes, if you inputted 3 it would output:
123
213
312
and so on?
I know they program can compile without any errors, but when it runs I'm fairly confident its getting a segmentation fault or something like that because it just crashes (my compiler doesn't tell me)
This would be an excellent time to start using your debugger and stepping through the code.
Use a small permutaion set, like 3 so there are few permutations.
As you step through be sure all your variables contain the expected values.
Nothing "just crashes" and your debugger will lead to right to the problem.
As to the set 123, the permutations are:
123
213
231
321
132
312
There are 3! permutations.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Steve Goldman |
last post by:
Hi,
I am trying to come up with a way to develop all n-length permutations of a
given list of values. The short function below seems to work, but I can't
help thinking there's a better way. ...
|
by: John Trunek |
last post by:
I have a set of X items, but want permutations of length Y (X > Y). I
am aware of the permutation functions in <algorithm>, but I don't
believe this will do what I want. Is there a way, either...
|
by: Girish Sahani |
last post by:
Hi guys,
I want to generate all permutations of a string. I've managed to
generate all cyclic permutations. Please help :)
def permute(string):
l=
l.append(string)
string1 = ''
for i in...
|
by: anurag |
last post by:
hey can anyone help me in writing a code in c (function) that prints
all permutations of a string.please help
|
by: Christian Meesters |
last post by:
Hi,
I'd like to hack a function which returns all possible permutations as lists
(or tuples) of two from a given list. So far, I came up with this solution,
but it turned out to be too slow for...
|
by: JosAH |
last post by:
Greetings,
last week we talked a bit about generating permutations and I told you that
this week will be about combinations. Not true; there's a bit more to tell
about permutations and that's...
|
by: Shraddha |
last post by:
Suppose we are having 3 variables...a,b,c
And we want to print the permutations of these variables...Such
as...abc,acb,bca...all 6 of them...
But we are not supposed to do it mannually...
I...
|
by: Assimalyst |
last post by:
Hi
I have a Dictionary<string, List<string>>, which i have successfully
filled. My problem is I need to create a filter expression using all
possible permutations of its contents.
i.e. the...
|
by: Bill Cunningham |
last post by:
I don't know if I'll need pointers for this or not. I wants numbers
10^16. Like a credit card 16 digits of possible 10 numbers, so I guess that
would be 10^16. So I have
int num ;
These are of...
|
by: alemo91 |
last post by:
Write a recursive function to print all possible permutations of a given string. For
example if the input string is “abc” then the set of permutations is: abc, acb, bac,
bca, cab, cba. Hint:...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |