...

View Full Version : Help me solve this c++ problem



MAJORdorMo
05-26-2009, 07:40 AM
Well, I have been working at this for a few days and I just can't figure it out.
Simply add code where it says "//your code here".
There should be around 16 lines of added code.



/*
Name: Vincent Heuken
This program unscrambles words.
*/

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

string sort(string s); // returns alphabetized version of s

struct word {
string sorted; // "ackrt"
string original; // "track"
};


int main() {
const int maxSize = 200000;
static word wordlist[maxSize]; // to hold the words

int numWords; // total number of words read in from file
ifstream infile;
infile.open("words.txt");
int i = 0;
while (infile && i < maxSize) {
// read each word into wordlist until the end of file
//YOUR CODE HERE
}
numWords = i;

string w;
do {
bool found = false;
cout << "Please type in a word to unscramble or 'q' to quit: ";
// find each occurrence of wordslist[i].sorted that matches sort(w}
//YOUR CODE HERE


} while (w != "q");

return 0;
}


string sort(string s) {
// return a string with the characters of s rearranged alphabetically.
// For example sort("track") returns "ackrt"
string t;
while (s != "") {
int minIndex = 0;
for (int i = 1; i< s.length(); i++)
if (s[i] < s[minIndex])
minIndex = i;
t += s[minIndex]; // add the smallest character to t
s.erase(minIndex,1); // remove the smallest character from s
}
return t;
}


I found that by adding this line of code to the first //YOUR CODE HERE:

infile >> wordlist[i].original;
i++; I can select words from the word list via "cout << wordlist[PLACE IN THE LIST].original;"

I have no idea where wordlist[i].sorted comes into play.
This is due tomorrow and I've been agonizing over it!
Thanks!

MAJORdorMo
05-26-2009, 10:03 AM
I figured it out!!! I just started toying with the different things, and I started understanding how it all worked. It wasn't too hard. I think I was just scared because I never worked with structs before.
Here is the finalized code:


/*
Name: Vincent Heuken
This program unscrambles words.
*/

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

string sort(string s); // returns alphabetized version of s

struct word {
string sorted; // "ackrt"
string original; // "track"
};


int main() {
const int maxSize = 200000;
static word wordlist[maxSize]; // to hold the words

int numWords; // total number of words read in from file
ifstream infile;
infile.open("words.txt");
int i = 0;
while (infile && i < maxSize) {
// read each word into wordlist until the end of file
infile >> wordlist[i].original;
wordlist[i].sorted = sort(wordlist[i].original);
i++;
}
numWords = i;

string w;
do {
bool found = false;
cout << "Please type in a word to unscramble or 'q' to quit: ";
// find each occurrence of wordslist[i].sorted that matches sort(w}
cin >> w;

for (i = 0; i < numWords; i++)
{
if (sort(w) == wordlist[i].sorted)
{
cout << wordlist[i].original << " ";
}
}
cout << endl;

} while (w != "q");

return 0;
}


string sort(string s) {
// return a string with the characters of s rearranged alphabetically.
// For example sort("track") returns "ackrt"
string t;
while (s != "") {
int minIndex = 0;
for (int i = 1; i< s.length(); i++)
if (s[i] < s[minIndex])
minIndex = i;
t += s[minIndex]; // add the smallest character to t
s.erase(minIndex,1); // remove the smallest character from s
}
return t;
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum