View Full Version : c++ anagrams
mike03
6 Dec 2004, 11:52am
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
CyrixInstead
6 Dec 2004, 1:27pm
Sounds like a homework assignment lol
~Cyrix
it's part of my dissertation, on c++, my lecturer gave me the hints
Do you have ideas on how you would want to do it? It's often easier to start by writing pseudocode and then turn that into c++.
so i need to count the number of letters in the inputted word, then extract all the words in the dictionary with the same number of letters.
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!
Here are some ideas???
Someone suggested I use the $ signs, not entirely sure what they're for though?!? :confused:
_____________________________________________________
#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);
The first thing I would do is get rid of that code (if you didn't write it). Not because it's wrong (I haven't even looked at it), but because you'll need to learn the proper coding process. These assignments aren't really to help you learn how to manipulate strings, but to teach you the proper methods of coding.
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:
Step 1) read in the first word
Steps 2-10) Determine if it is an anagram (or use however many steps you need).
Step 11) read in next word
Step 12) goto step 2
After you've done this, you'll have some idea of how you'll want to do the program. You'll obviously need:
Some way to read in data
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 1) Start with two words/strings in memory that are anagrams. Write code that will compare them, however you may want to.
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.
vBulletin® v3.8.1, Copyright ©2000-2009, Jelsoft Enterprises Ltd.