Thread: Need help with logic algorithm...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    8

    Need help with logic algorithm...

    This is an assignment but I'm not stumped by the coding of it. I'm stumped by the logical algorithm I've tried several approaches and none of them are working.

    The premise is this allow the user to input a word and then output all permutations of the word in which the first letter stays in it's default position. Also we cannot use string objects only arrays of chars.

    i.e. word would output:

    word wodr wdor wrod wdro wrdo

    The parts of the approach I'm sure are correct are:

    Get user input and store in an Array(inputArray).
    Store first letter of user input in a variable(firstLetter).
    Store initial word in an Array(outputArray).
    Remove First Letter of input from inputArray).
    Shuffle remaining members of the inputArray
    Store firstLetter concatenated with inputArray as a new member of outputArray.

    My issue is figuring out how to shuffle the remaining inputArray characters and only arrive at the remaining permutations.

    I tried dynamically refering to indexes of characters based on incrementation and the length of inputArray and couldn't get it to work.

    I tried simply rotating the position of each letter forward in a loop but this results in only a few of the permutations.

    Any advice would be appreciated. Honestly I'm a little over my head here, I've done a lot of scripting(javascript, actionscript, PHP) and some JAVA but no C++ and this is the second half of the introductory class.

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Hint.. Recursion... (At least I think so)

  3. #3

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    To get all the permutations of a sequence, break it into a head (first symbol) and tail (all the rest). Generate the permutations of the tail. Then iterate through all positions of all the permutations and insert the head in those positions.

    A sequence with only one symbol in it has only itself as a permutation.

    Example: xyz

    Head = x, tail = yz
    Generate all permutations of yz:
    Head = y, tail = z
    Generate all permutations of z --> z
    Insert y in all positions: yz zy
    Insert x in all position: xyz xzy yxz zxy yzx zyx
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    8
    Thank you all for the responses.
    I'm a bit bogged down by work today but on my lunch break I'm going to look in to Recursion.

    Brewbuck I think I understand the concept of what your doing, but I'm not entirely sure how to do it dynamically and account for words of any length.

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by fenixink View Post
    Thank you all for the responses.
    I'm a bit bogged down by work today but on my lunch break I'm going to look in to Recursion.
    I wouldn't worry about a recursive solution right now.

    Quote Originally Posted by fenixink View Post
    Brewbuck I think I understand the concept of what your doing, but I'm not entirely sure how to do it dynamically and account for words of any length.
    Use a std::string for your input and iterate through that.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #7
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by AndrewHunter View Post
    I wouldn't worry about a recursive solution right now.
    Won't it be *much* simpler than iterating the inner portion too ?
    Last edited by manasij7479; 09-28-2011 at 10:37 AM.

  8. #8
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by manasij7479 View Post
    Won't it be *much* simpler than iterating the inner portion too ?
    Define "simpler". For a beginner, I would say no.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  9. #9
    Registered User
    Join Date
    Sep 2011
    Posts
    8
    Quote Originally Posted by fenixink View Post
    Also we cannot use string objects only arrays of chars.
    I would use string if I could but I can't I was reading up on Recursion and it seems like a viable solution but I'm still not quite sure how to make it work dynamically.

    Thank you all again for the input.

  10. #10
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by fenixink View Post
    I would use string if I could but I can't I was reading up on Recursion and it seems like a viable solution but I'm still not quite sure how to make it work dynamically.

    Thank you all again for the input.
    I hate these BS assignments. Ok, well in that case you need to dynamically allocate a char array to obtain user input - or - and what you are probably suppose to do- is make an input array that is large enough to handle all inputs: say char[100] for instance.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  11. #11
    Registered User
    Join Date
    Sep 2011
    Posts
    8
    Here is what I have so far:

    Code:
    #include <iostream>
    using namespace std;
    
    int main ()
    {
    char word[100]; cout << "Enter a word: " << endl; cin >> word char firstLetter[1] = {word[0]}; cout << "The first letter of the word is " << firstLetter[0]; << endl; return 0;
    }
    Not entirely sure how to get the permutations of the remaining members of word.

  12. #12
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Well, let's start by making sure your starting point is correct:
    Code:
    #include <iostream>
    
    int main(void){
    
    	//initialize array
    	char myword[100]={0};
    
    	std::cout<<"Enter word:";
    	//cin is not safe with char arrays
    	std::cin.getline(myword,sizeof(myword)-1);
    	//you don't need an extra char variable here
    	std::cout<<"first char: "<<myword[0];
    	return(0);
    }
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  13. #13
    Registered User
    Join Date
    Sep 2011
    Posts
    8
    Is there a reason why

    std::cin

    is better than:

    using namespace std;

    cin

    Just curious I honestly don't know.
    I was using the second variable because I intended to pull the first letter out of it into another variable that way myword would only contain the char we wanted permutations of.

  14. #14
    Registered User
    Join Date
    Sep 2011
    Posts
    8
    Did some research and here is my current code:

    Code:
    #include <iostream>
    
    int main (void)
    {
    using std::cin; //using declaration for cin using std::cout; //using declaration for cout using std::endl; //using declaration for endl char myword[100]={0}; //initialize array cout << "Enter word:" << endl; cin.getline(myword,sizeof(myword)-1); cout << "first char: " << myword[0] << endl; return(0)
    }

  15. #15
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Ok, so think about Bewbuck's solution in post #4. Could you describe how to manipulate an array that way using words? Perhaps psuedocode. You are going to need to use loops here.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. XOR Logic Help
    By Yabssato in forum C Programming
    Replies: 8
    Last Post: 09-27-2011, 01:29 PM
  2. Help with logic
    By Ducky in forum C++ Programming
    Replies: 5
    Last Post: 12-05-2009, 11:49 AM
  3. help with logic please
    By Tryin in forum C Programming
    Replies: 7
    Last Post: 01-28-2003, 02:54 AM
  4. How abt an algorithm and logic contest
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 08-12-2002, 11:06 AM
  5. Pcx? What's the logic????
    By kaygee in forum C++ Programming
    Replies: 1
    Last Post: 01-14-2002, 10:18 AM