|
-
February 22nd, 2008, 01:52 AM
#1
Need help with comparison algorithm
I need to write program that inputs a scrambled word and compares it against a pre-determined list of words. I am first checking for length, then if a match is found, sending them to a function to compare the letters. I can't seem to get the loops right. Here is what I have so far:
Code:
Code:
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
bool check(string, string);
int main()
{
string scrambled;
string PossibleWord [13]={"will", "check", "it", "against", "list", "and", "return", "hello", "True", "or", "False", "Swap"};
cout << "Please enter scrambled word: ";
cin >> scrambled;
for (int i=0; i<13; i++)
{
if (scrambled.size()==PossibleWord[i].size())
{
if (check(PossibleWord[i], scrambled))
{
cout<< "the word is " << PossibleWord[i]<<endl;
break;
}
}
else
cout<< "the word is not found"<<endl;
}
system ("pause");
}
bool check(string p, string s)
{
bool match;
int count=0;
int psize= p.size(), ssize= s.size();
for (int i=0; i< psize; i++)
{
for (int d=0; d< ssize; d++)
{
if (p[i]==s[d])
{
cout << "Match with possible's "<< p[i] << " and scrambled's " << s[d]<< endl;
count++;
}
}
}
cout << "Count is "<< count <<endl;
if (count==psize)
{
match=true;
return match;
}
else
match=false;
return match;
}
I would love any suggestions on how to make this work!
-
February 22nd, 2008, 03:39 AM
#2
Re: Need help with comparison algorithm
Hi there,
it looks to me, like you haven't really figured out the algorithm how to check, if a scrambled word matches the original. I assume finding that algorithm is part of the excercise, so I advise you to take some pen and paper, write down "hello" and "olelh" or "codeguru" and "odugerco" and systematically find out if one is a scramble of the other. Once you've done that, write down the algorithm in words, post it here or implement it yourself.
Last edited by treuss; February 22nd, 2008 at 03:42 AM.
More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf
Premature optimization is the root of all evil --Donald E. Knuth
Please read Information on posting before posting, especially the info on using [code] tags.
-
February 22nd, 2008, 07:45 AM
#3
Re: Need help with comparison algorithm
 Originally Posted by treuss
Hi there,
it looks to me, like you haven't really figured out the algorithm how to check, if a scrambled word matches the original. I assume finding that algorithm is part of the excercise, so I advise you to take some pen and paper, write down "hello" and "olelh" or "codeguru" and "odugerco" and systematically find out if one is a scramble of the other. Once you've done that, write down the algorithm in words, post it here or implement it yourself.
I was going to try out this exercise and post the code because it sounded like a good one for a newbie like me,
but then I decided not to because the advice given here is brilliant and perhaps the best "help" that I have seen given to people like us.
-
February 22nd, 2008, 08:33 AM
#4
Re: Need help with comparison algorithm
 Originally Posted by potatoCode
I was going to try out this exercise and post the code because it sounded like a good one for a newbie like me,
Also, do not post answers to questions that are obviously homework questions.
Regards,
Paul McKenzie
-
February 22nd, 2008, 08:34 AM
#5
Re: Need help with comparison algorithm
I can actually think of two solutions (well, three, but the third one isn´t serious):
#1
Count all letters of both the scrambled and the possible descrambled word. If both consists of the same amount of letters you have found the solution.
#2
Check if the scrambled and possible descrambled word have the same length. If they have, make a copy of the descrambled word and iterate over all letters of the scrambled word. Remove the current letter from the descrambled copy until all letters are processed. If the copy is empty at the end you found the descrambled word.
#3 (don´t take this serious)
generate all permutations of the input and compare each permutation to the possible descrambled word. Return the word that matches an permuation.
- Guido
-
February 22nd, 2008, 08:50 AM
#6
Re: Need help with comparison algorithm
#4 Descramble the scrambled word. If it works return true, else return false.
More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf
Premature optimization is the root of all evil --Donald E. Knuth
Please read Information on posting before posting, especially the info on using [code] tags.
-
February 22nd, 2008, 08:52 AM
#7
Re: Need help with comparison algorithm
#5
Use a hash function that creates an unique hash depending on the input letters (independent of their position). Calculate the hash for both the scrambled and descrambled word and compare both hashes.
#6
Sort both the scrambled and descrambled word´s letters and compare the sorted words.
Last edited by GNiewerth; February 22nd, 2008 at 08:55 AM.
- Guido
-
February 22nd, 2008, 11:51 AM
#8
Re: Need help with comparison algorithm
Thanks to everyone who answered. here is what i have so far:
Code:
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
bool check(string, string);
int main()
{
bool match;
string scrambled;
string PossibleWord [13][13]={"will", "check", "it", "against", "list", "and", "return", "hello", "True", "or", "False", "Swap"};
cout << "Please enter scrambled word: ";
cin >> scrambled;
for (int i=0;i<13;i++)
{
if (scrambled.size()==PossibleWord[i][0].size())
{
match=(check(PossibleWord[i][0], scrambled));
if (match)
{
cout << "Match!\n";
break;
}
}
}
system ("pause");
}
bool check(string p, string s)
{
bool matched;
int count=0;
int ssize= s.size();
char temp[ssize];
temp[ssize]='\0';
for (int r=0;r<ssize;r++)
{
temp[r]=s[r];
}
for (int i=0; i<ssize; i++)
{
for (int d=0; d< ssize; d++)
{
if (temp[i]==p[d])
{
<< temp[i]<< endl;
temp[i]='\0';
++count;
}
}
cout <<" count = "<< count << endl;
}
if (count==ssize)
{
cout<< "the word is " << p <<endl;
matched=true;
}
else
cout<< "The word is not found.\n"<<endl;
matched=false;
return matched;
}
I am trying to compare each letter and count the number of times they match. if the number of matches equals the word length, I assume the word is matched. There are still some problems with the loops. The program crashes now and i can't see why.
-
February 22nd, 2008, 11:57 AM
#9
Re: Need help with comparison algorithm
int ssize= s.size();
char temp[ssize];
temp[ssize]='\0';
Several problems there.
-
February 22nd, 2008, 12:09 PM
#10
Re: Need help with comparison algorithm
 Originally Posted by veronicak5678
I am trying to compare each letter and count the number of times they match. if the number of matches equals the word length, I assume the word is matched. There are still some problems with the loops. The program crashes now and i can't see why.
The program does not even compile, so how can it crash?
- Guido
-
February 22nd, 2008, 12:25 PM
#11
Re: Need help with comparison algorithm
I think I have this part figured out now. i thought this would be the easy part! Here's what i got:
Code:
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
bool check(string, string);
int main()
{
bool match;
string scrambled;
string PossibleWord[12]={"will", "check", "it", "against", "list", "and", "return", "hello", "True", "or", "False", "Swap"};
cout << "Please enter scrambled word: ";
cin >> scrambled;
for (int i=0;i<13;i++)
{
if (scrambled.size()==PossibleWord[i].size())
{
match=(check(PossibleWord[i], scrambled));
}
if (match)
{
cout<< "Match!\nThe word is " << PossibleWord[i] << ".\n";
break;
}
}
system ("pause");
}
bool check(string p, string s)
{
bool match;
int count=0;
int ssize= s.size();
char temp[ssize];
temp[ssize]='\0';
for (int r=0;r<ssize;r++)
{
temp[r]=s[r];
}
for (int i=0; i<ssize; i++)
{
for (int d=0; d< ssize; d++)
{
if (temp[i]==p[d])
{
temp[i]='\0';
count++;
}
}
}
if (count==ssize)
{ match = true;
return match;
}
else
match=false;
return match;
}
Step 2 in my asignment is to modify this to use a class called "scramble" that looks like this:
Code:
class Scramble
{
private:
string PossibleWord[];
int size;
bool Check (string T,string S){} // Is this the word?
public:
Scramble (string List[], int Size); // constructor
string Descramble(string Scrambled); // the game player
};
I am also supposed to make a "driver"? Does that just refer to the main()?
-
February 22nd, 2008, 01:05 PM
#12
Re: Need help with comparison algorithm
Yes, a driver is just a main() function in this context.
-
February 22nd, 2008, 01:08 PM
#13
Re: Need help with comparison algorithm
OP's method is O(N^2).
 Originally Posted by GNiewerth
#6
Sort both the scrambled and descrambled word´s letters and compare the sorted words.
This one is good and simpler. 1) Sorting would be of the order of O(NlogN) and 2) comparison would be O(N).
 Originally Posted by GNiewerth
#5
Use a hash function that creates an unique hash depending on the input letters (independent of their position). Calculate the hash for both the scrambled and descrambled word and compare both hashes.
This one is even better. With maps, this one would be 1) order of insert into multimap O(logN) and 2) comparison of multimaps O(N).
With unordered_multimap (which is what you are suggesting - hash implementations, right?), 1) order of inserts O(1) (worst case O(N)) and 2) comparison O(N).
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
February 22nd, 2008, 01:09 PM
#14
Re: Need help with comparison algorithm
 Originally Posted by veronicak5678
I think I have this part figured out now.
You think so? Your current code prints out that "abc" is a scramble of "will".
I can only repeat my advice: Write the algoritm in English first, then translate to C++.
More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf
Premature optimization is the root of all evil --Donald E. Knuth
Please read Information on posting before posting, especially the info on using [code] tags.
-
February 22nd, 2008, 01:16 PM
#15
Re: Need help with comparison algorithm
 Originally Posted by treuss
You think so? Your current code prints out that "abc" is a scramble of "will".
I can only repeat my advice: Write the algoritm in English first, then translate to C++.
You're right. That check algorithm makes no kind of sense. He still has the problem with the temp buffer allocation and initialization too.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|