I just figured out the answer to one of my previous questions but now I have a new problem, before I was just being a tad silly and forgot how to properly write a function but now my question is if I have the following function

int findMobile( string word, int dir[] )

how do I write the function declaration in the header file? If I keep it as int findMobile (string, int); I get an error: invalid conversion from int* to int but if I make it int[] it compiles in code blocks but causes a seg fault. What is the proper way of doing this?

I can't edit my post for some reason but some additional information the seg fault is on line 75, I will attach the code related to that:

int findMobile( string word, int dir[] ){
    int isMobile = -1;
    for( int i = 0; i < word.length(); i++ ){
        if( dir[i] == 1 ){
            if( word[i] > word[i + 1] ){
                if ( word[i] > word[isMobile] ){
                    isMobile = i;
                }
            }
        }else{
            if(i != 0){
                if( word[i] > word[i - 1] ){
                    if ( word[i] > word[isMobile] ){
                    isMobile = i;
                    }
                }
            } //Line 75 is here
        }
    }
    return isMobile;
}

This is part of the johnson-trotter algorithm we are supposed to use where you need to find the largest mobile element.

It should be OK to use it the same as in the actual function block:

int findMobile( string word, int dir[] );

@tinstaafl That's what I ended up doing and I thought the seg fault was related to that syntax change but I don't believe it is anymore, I believe it is something to do with my function to pick mobile elements.

Just from a quick examination of your code, I notioced, that you're setting isMobile to -1, then trying to use that as an index before it is modified. Not sure what kind of error this will produce, but it could be the source of your problem.

I think a potential problem might be that word.length() is greater than the size/length of dir.

@tinstaafl The isMobile being -1 is what I use to check it in the other function, if the isMobile returns -1 then I know there are no mobile elements and there are no more permutations.

@suzie999 I think that dir should be the same length as word. I define it the following way:

void printPermutations( string word ){
  int length = word.length();
  int index = -1;
  int a = 0;
  int b = 0;
  int dir [length]; //This was set to index I just changed it but my web grader seems to be loading for sometime, may be infite loop?
  for( int i = 0; i < length; i++ ){
    dir[i] = -1;
  }

I think dir should have, at least in theory, matching indexes for the word. I can attach more code if needed.

Regardless of why it is there, you are trying to use a negative number as an index to a character in a string, which will definitely throw an exception.

Also your loop limit is the top index. But your code asks for the index plus one which when the loop has reached it's limit, is sure to throw an exception as well.

@tinstaafl I see where you are talking about now, I forgot to change those over when I added a new counter variable. I fixed those and I am now trying it again in the web grader. It seems as though I have an infite loop somewhere though now. I don't know where exactly it is happening as I can't view output because to do so it would need to finish it's run because output gets written to a PDF. Codeblocks is also not allowing me to run it as it says something about the code exiting in a weird way and not using the string constructor but it does this with the code provided to us by out teacher and that code compiles on our server so I am sort of confused.

try submitting the whole code here.

#include "csce310homework01part02.h"
#include <iostream>
#include <string>
#include <vector>

using namespace std;

/*
*
* Direction: Dir goes -1 for left and 1 for right
*
*/

int findMobile( string word, int dir[] );
void reverseDir ( string word, int dir[], int index );

void printPermutations( string word ){
  int length = word.length();
  int index = -1;
  int a = 0;
  int b = 0;
  int dir [length];
  for( int i = 0; i < length; i++ ){
    dir[i] = -1;
  }

  index = findMobile( word, dir );
  while ( index != -1 ){

    if( dir[index] == -1 ){
        a = index;
        b = index - 1;
        swap( word, a, b );
    }else{
        a = index;
        b = index + 1;
        swap( word, a, b );
    }

    reverseDir( word, dir, index );
    cout << word << endl;
    index = findMobile( word, dir );
  }

  cout << word << endl;

  return;
}

void swap( string word, int a, int b ){
char temp;
temp = word[a];
word[a] = word[b];
word[b] = temp;

return;
}

int findMobile( string word, int dir[] ){
    int isMobile = -1;
    int counter = 0;
    for( int i = 0; i < word.length(); i++ ){
        if( dir[i] == 1 ){
            if( word[i] > word[i + 1] ){
                if(counter == 0){
                    counter++;
                    isMobile = i;
                }else if ( word[i] > word[isMobile] ){
                    isMobile = i;
                }
            }
        }else{
            if(i != 0){
                if( word[i] > word[i - 1] ){
                    if(counter == 0){
                        counter++;
                        isMobile = i;
                    }else if ( word[i] > word[isMobile] ){
                        isMobile = i;
                    }
                }
            }
        }
    }
    return isMobile;
}

void reverseDir ( string word, int dir[], int index ){
    for( int i = 0; i < word.length(); i++ ){
        if( word[i] > word[index] ){
            dir[i] = dir[i] * -1;
        }
    }
    return;
}

The error (infinite loop) is happening in the for loop of isMobile. I am trying to implement Johnson-Totter algorithm for permutations

I now know for sure that this chunk of code is causing the while loop to never exit so it is infinitely looping.

int findMobile( string word, int dir[] ){
    int isMobile = -1;
    int counter = 0;
    for( int i = 0; i < word.length(); i++ ){
        if( dir[i] == 1 ){
            if( word[i] > word[i + 1] ){
                if(counter == 0){
                    counter++;
                    isMobile = i;
                }else if ( word[i] > word[isMobile] ){
                    isMobile = i;
                }
            }
        }else{
            if(i != 0){
                if( word[i] > word[i - 1] ){
                    if(counter == 0){
                        counter++;
                        isMobile = i;
                    }else if ( word[i] > word[isMobile] ){
                        isMobile = i;
                    }
                }
            }
        }
    }
    return isMobile;
}

In this code, as tinstaafl mentions, you may be geting the -1 index of word in the else branch. You need a test for isMobile being less than 0.

            if(i != 0){
                if( word[i] > word[i - 1] ){
                    if(counter == 0){
                        counter++;
                        isMobile = i;
                    }else if ( word[i] > word[isMobile] ){
                        isMobile = i;
                    }
                }
            }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.