C++/c++

Advertisement


Question
QUESTION: hi zlatko,
i have to Developed the algorithm in c + + which create a dictionary containing publicly 50 thousand different word publicly at least stored in a text file (to generate random words random lengths). would you guide me how to write this in c + +? Tools I don't understand the algorithm and I am not that familiar with the syntax

ANSWER: Hello
You will have to give me a little more information. Does your "dictionary" conatain just words, or words and definitions? Will the program be looking up words and displaying the definitions? What type of data structures have you studied? Has your instructor made a recomendation about what kind of data structure to use to store the dictionary?

---------- FOLLOW-UP ----------

QUESTION: hello,
my dictionary contain just words so that the program be looking up words
the type of data structures have i studied is record ,tree ,stack,queue...

ANSWER: Hello

You can create a record to hold one word and store the records in a binary tree. You say you have studied trees. Do you understand how to make a binary tree in C or C++?

Another choice is to read the list of words into an array, sort the array, and then use a binary search to search through the array? If using a sorted array, the program will probably perform better than a  binary tree for searching. Although you will need some processing time to actually do the sort, it would be done only once.

Which method do you like better?

---------- FOLLOW-UP ----------

QUESTION: hello,
i prefer the second method ,but in it where we use the file text
please can you tell me how to write this program in c++?
thanks for you

Answer
Hello

Sir. You haven't really told me what you are having trouble with, and you haven't given me very much information about what your instructor expects, and you haven't made any effort to write any code yourself, so I cannot be sure that what I am giving you will be useful, but I am sure that I don't want to do any more work on this question. I am assuming that you are allowed to use the string and vector containers from the standard template library. If this was a professional program, then the dictionary would be implemented using the standard library set or map data structure and there would be very little coding to do. Since this is probably a school assignment, I am guessing that your instructor wants to see some coding, so I have made only a small use of the standard template library. Here is the code. I have done a little testing on it.
Good luck and best regards.

/*
The easiest thing to do is store all the word as strings
in a vector
*/

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

using namespace std;

vector<string> dict;

/* You need three functions
1) read data into vector
2) sort vector
3) binary search of the vector
*/

void readDictionaryFile()
{
   ifstream in("dict.txt");
   if (in.good())
   {
       while( !in.eof())
       {
         string word;
         getline(in, word);
         dict.push_back(word);
       }
   }
   else
   {
       cout << "Could not open file for reading\n";
   }
}

/* This is a simple selection sort.
The sort is made from the following two functions
*/
int findLowestElement(size_t ix)
{
   size_t lowestIx = ix;
   ++ix;
   while(ix < dict.size())
   {
       if (dict[ix] < dict[lowestIx]) lowestIx = ix;
       ++ix;
   }
   return lowestIx;
}

void sortDictionary()
{
   for (size_t i = 0; i < dict.size()-1; ++i)
   {
       int lowest = findLowestElement(i);
       string temp = dict[i];
       dict[i] = dict[lowest];
       dict[lowest] = temp;
   }
}

/* This is a binary search of the dictionary */
int searchDictionary(const string& searchString)
{
   int firstIndex = 0;
   int lastIndex = dict.size() - 1;
   
   while(firstIndex <= lastIndex)
   {
       int midPoint = (firstIndex + lastIndex)/2;
       
       int cmpResult = searchString.compare(dict[midPoint]);
       if (cmpResult < 0)
       {
         lastIndex = midPoint - 1;
       }
       else if (cmpResult > 0)
       {
         firstIndex = midPoint + 1;
       }
       else
       {
         return midPoint + 1;
       }
   }
   return 0;
}

void test(const string& searchString)
{
   cout << searchString << " found at spot " << searchDictionary(searchString) << endl;
}

int main(void)
{
   readDictionaryFile();
   sortDictionary();
   test("aaa");
   test("bbb");
   test("ccc");
   test("ddd");
   return 0;
}

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2016 About.com. All rights reserved.