c++ anagrams
here's my problem;
need to write a program that can find and printout a list of all the anagrams of a lower case word (inputted by the user) that are in a file. It should also output the number of anagrams found.
HINTS:
Do not attempt to generate all the permutations of a word and look for occurrences of them in the file.
Anagrams have exactly the same number of letters.
Anagrams have exactly the same number of each letter.
Anagrams have the same word value
You may wish to consider sorting the letters in words into alphabetical order
bit tricky in my opinion, as this is my first year of computer programming. Only know structures, strings, classes, loops, arrays, functions, pointers, three-vectors.
Thanks
need to write a program that can find and printout a list of all the anagrams of a lower case word (inputted by the user) that are in a file. It should also output the number of anagrams found.
HINTS:
Do not attempt to generate all the permutations of a word and look for occurrences of them in the file.
Anagrams have exactly the same number of letters.
Anagrams have exactly the same number of each letter.
Anagrams have the same word value
You may wish to consider sorting the letters in words into alphabetical order
bit tricky in my opinion, as this is my first year of computer programming. Only know structures, strings, classes, loops, arrays, functions, pointers, three-vectors.
Thanks
0
Comments
~Cyrix
I need to sort my words in to alphabetical order, then extract all the words that are the same. ???
Or sort the inputted word in to alphabetical order and check for each letter in the dictionary file, then the words with every letter included are the anagrams.
Problem is i'm not very good at putting this in to code!
Someone suggested I use the $ signs, not entirely sure what they're for though?!?
_____________________________________________________
#include <iostream>
#include <fstream>
using namespace std;
class backtrack {
# constructor
function backtrack($input_array)
{
// the array you want to backtrack and get each configuration of
$this->_array = $input_array;
// the current configuration of elements
$this->configuration = array();
// if $this->taken[$pos] == false, $this->_array[$pos] is yet to be used
// in the current configuration
$this->taken = array();
// set all as NOT taken
for ($i=0; $i<count($this->_array); $i++) $this->taken[$i] = false;
}
# recursive function:
# fill each position ($pos) in configuration by trying each element from $this->_array
function do_backtrack($pos)
{
// if configuration is complete
if ($pos == count($this->_array))
{
$this->test_configuration();
return;
}
// try fill each position in the configuration with each element in $_array.
// but we can only use it if it's not in the current configuration (ie its 'taken')
for ($i=0; $i<count($this->_array); $i++)
{
if ($this->taken[$i]) continue;
$this->configuration[$pos] = $this->_array[$i]; // take it
$this->taken[$i] = true; // set it as now taken
$this->do_backtrack($pos+1); // continue backtracking to get rest of configuration
$this->taken[$i] = false; // set as untaken as we try next configuration
}
}
# we can do something with configuration here since configuration is done
# but we currently just print it out
function test_configuration()
{
for ($i=0; $i<count($this->_array); $i++)
print $this->configuration[$i].' ';
print '<br>';
}
}
$btrack = new backtrack(array('1','3','5'));
$btrack->do_backtrack(0);
Here's a couple suggestions. Decide how you want to do it, don't ask other people how they did it. Once you've decided, write psuedocode...for example:
After you've done this, you'll have some idea of how you'll want to do the program. You'll obviously need:
A loop to go through it all
Code to determine if it is an anagram
Another useful tool is an iterative enhancement plan. This is a way of making sure you don't get overwhelmed by the project. Start simple. Here's an example:
Stage 2) Now, try doing this with reading in one word/string from a file.
Stage 3) Move on to reading an entire file
Stage 4) etc.
The smaller the stages are, the easier it will be to do this, and the easier it will be to troubleshoot when something doesn't work. The last thing that you want to do is copy some code you get from someone on the internet and turn it in. Once you've got some ideas, I'll be happy to help you flesh them out, and I'm sure others will as well.