Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    May 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help me solve this c++ problem

    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.

    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:
    Code:
    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!
    Last edited by MAJORdorMo; 05-26-2009 at 07:56 AM.

  • #2
    New to the CF scene
    Join Date
    May 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:

    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;
    }
    Last edited by MAJORdorMo; 05-26-2009 at 10:15 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •