Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

permutations using recursion

Can anyone point out where I went wrong. It compiles but I keep getting a segmentation fault.
======================================
#include
#include

using namespace std;

int permutations(int [], int *, int);

int main()
{
int size;
int nums[size];
int *start;
int i;

start = nums;
cout << "Please enter a number." << endl;
cin >> size;

for(i=0; i < size; i++)
{
nums[i] = i + 1;
}
cout << "Permutations:" << endl;
permutations(nums, start, size);
}



int permutations(int nums[], int *start, int size)
{
int i,j;

if(*start == size)
{
for(i = 0; i < size; i++)
{
cout << nums[i];
}
cout << endl;


for(j = *start; j < size-1; j++)
{
swap(*start,nums[j]);
permutations(nums, start+1, size);
swap(*start,nums[j]);
}
}
return 0;
}


Comments

  • oralcaloralcal Member Posts: 1

    Can you compile it?
    [root@dev-rh9 /]# g++ 2.c -o 2
    2.c: In function `int main()':
    2.c:15: invalid conversion from `int' to `int*'
    2.c:21: invalid types `int[int]' for array subscript
    2.c:24: invalid conversion from `int' to `int*'


    : Can anyone point out where I went wrong. It compiles but I keep getting a segmentation fault.
    : ======================================
    : #include
    : #include
    :
    : using namespace std;
    :
    : int permutations(int [], int *, int);
    :
    : int main()
    : {
    : int size;
    : int nums[size];
    : int *start;
    : int i;
    :
    : start = nums;
    : cout << "Please enter a number." << endl;
    : cin >> size;
    :
    : for(i=0; i < size; i++)
    : {
    : nums[i] = i + 1;
    : }
    : cout << "Permutations:" << endl;
    : permutations(nums, start, size);
    : }
    :
    :
    :
    : int permutations(int nums[], int *start, int size)
    : {
    : int i,j;
    :
    : if(*start == size)
    : {
    : for(i = 0; i < size; i++)
    : {
    : cout << nums[i];
    : }
    : cout << endl;
    :
    :
    : for(j = *start; j < size-1; j++)
    : {
    : swap(*start,nums[j]);
    : permutations(nums, start+1, size);
    : swap(*start,nums[j]);
    : }
    : }
    : return 0;
    : }
    :
    :
    :

Sign In or Register to comment.