SlideShare a Scribd company logo
1 of 14
Download to read offline
You are to write an efficient program that will read a dictionary of 100,000 words, and then
check a document of 2,000,000 words. The program should insert into an array the position of
each misspelled word. I have provided the driver program SpellRunner.cpp, and the necessary
class stubs in speller.h, and speller.cpp. You may add any classes and/or code you wish to
speller.cpp, and speller.h. You may also use, and alter any Weiss files. CreateDocs.out creates
files used for testing. Doc-3-4-1 and Dict-3-4-1 are two testing files for small file.
_____________________________________________________________________________
_________________
SpellRunner.cpp
#include
#include
#include
#include
#include
#include "CPUTimer.h"
#include "mynew.h"
#include "speller.h"
// uncomment line below for DOS/Windows OS.
// #define DOS
#ifdef DOS
#include
#else
#include
#endif
extern int maxRAM;
extern int currentRAM;
using namespace std;
char* readDictionary(char *filename, char **dictionary, int dictSize)
{
int fileSize;
char *s;
struct stat statbuf;
stat(filename, &statbuf);
fileSize = statbuf.st_size;
s = new char[fileSize + 1];
ifstream inf(filename);
inf.read(s, fileSize);
inf.close();
s[fileSize] = '0';
dictionary[0] = strtok(s, " ");
for(int i = 1; i < dictSize; i++)
dictionary[i] = strtok(NULL, "  ");
return s;
} // readDictionary
char* readDocument(char **document, int dictSize, int docSize, int seed)
{
char *s, filename[80];
int fileSize;
struct stat statbuf;
sprintf(filename, "Doc-%d-%d-%d.txt", dictSize, docSize, seed);
stat(filename, &statbuf);
fileSize = statbuf.st_size;
s = new char[fileSize + 100];
ifstream inf(filename);
inf.read(s, fileSize);
inf.close();
s[fileSize] = '0';
document[0] = strtok(s, " ");
for(int i = 1; i < docSize; i++)
document[i] = strtok(NULL, "  ");
return s;
} // readDocument()
void readWrongs(int *wrongs, int dictSize, int docSize, int seed,
int &wrongCount)
{
char filename[80];
wrongCount = 0;
sprintf(filename, "Wrongs-%d-%d-%d.txt", dictSize, docSize, seed);
ifstream inf(filename);
while(inf >> wrongs[wrongCount++]);
wrongCount--;
} // readWrongs()
void checkAnswers(int wrongs[], int wrongCount, int misspelled[],
int misspelledCount)
{
for(int i = 0; i < wrongCount && i < misspelledCount; i++)
if(wrongs[i] < misspelled[i])
{
cout << "Speller missed misspelled word # " << wrongs[i] << endl;
return;
}
else
if(wrongs[i] > misspelled[i])
{
cout << "Speller thought correctly spelled word # " << misspelled[i]
<< " was wrong ";
return;
}
if(wrongCount != misspelledCount)
cout << "Speller found " << misspelledCount << " misspelled words when "
<< " there were really " << wrongCount << " misspelled words. ";
} // checkAnswers
void cleanup(char **dictionary, char *docFilePtr, char **document, int *wrongs, int
*misspelled)
{
delete [] dictionary;
delete [] docFilePtr;
delete [] document;
delete [] wrongs;
delete [] misspelled;
} // cleanup()
int main(int argc, char* argv[])
{
char line[80], **dictionary, **document, *dictFilePtr, *docFilePtr;
int *wrongs, *misspelled, misspelledCount = 0, seed, dictSize, docSize,
wrongCount, tempMaxRAM, tempCurrentRAM;
strcpy(line, argv[1]);
strtok(line, "-");
dictSize = atoi(strtok(NULL, "-"));
docSize = atoi(strtok(NULL, "-"));
seed = atoi(strtok(NULL, "."));
dictionary = new char*[dictSize + 3];
dictFilePtr = readDictionary(argv[1], dictionary, dictSize);
document = new char*[docSize + 3];
docFilePtr = readDocument(document, dictSize, docSize, seed);
wrongs = new int[docSize];
readWrongs(wrongs, dictSize, docSize, seed, wrongCount);
misspelled = new int[docSize];
CPUTimer ct;
maxRAM = currentRAM = 0;
Speller *speller = new Speller(dictionary, dictSize);
tempMaxRAM = maxRAM;
tempCurrentRAM = currentRAM;
delete [] dictFilePtr;
maxRAM = tempMaxRAM;
currentRAM = tempCurrentRAM;
speller->check(document, docSize, misspelled, &misspelledCount);
cout << "CPU Time: " << ct.cur_CPUTime() << " Real RAM: " << maxRAM << endl;
checkAnswers(wrongs, wrongCount, misspelled, misspelledCount);
cleanup(dictionary, docFilePtr, document, wrongs, misspelled);
delete speller;
return 0;
} // main()
_____________________________________________________________________________
_________________
Speller.h
#ifndef SPELLER_H
#define SPELLER_H
#include "mynew.h"
#include "QuadraticProbing.h"
#include
class Speller
{
public:
QuadraticHashTable *hashDict;
Speller(char *dictionary[], int dictSize);
~Speller();
void check(char *document[], int docSize, int misspelled[], int *misspelledCount);
}; // class Speller
#endif
_____________________________________________________________________________
_________________
Speller.cpp
#include "speller.h"
Speller::Speller(char *dictionary[], int dictSize)
{
hashDict = new QuadraticHashTable (-1, dictSize);
for(int i = 0; i < dictSize; i++)
hashDict->insert(dictionary[i]);
} // Speller()
void Speller::check(char *document[], int docSize, int misspelled[],
int *misspelledCount)
{
*misspelledCount = 0;
//Check document array using dictionary that you stored in constructor
for(int i = 0; i < docSize; i++)
{
if(!(hashDict->find(document[i])))
{
++(*misspelledCount);
misspelled[i] = 1;
}
else
{
misspelled[i] = 0;
}
}
//Store the array index of the misspelled word in the corresponding array and increments the
count of misspelled words
} // check()
Speller::~Speller()
{
} // ~Speller()
_____________________________________________________________________________
____________
QuadraticProbing.cpp
#include "QuadraticProbing.h"
/**
* Internal method to test if a positive number is prime.
* Not an efficient algorithm.
*/
template
bool QuadraticHashTable::isPrime( int n ) const
{
if( n == 2 || n == 3 )
return true;
if( n == 1 || n % 2 == 0 )
return false;
for( int i = 3; i * i <= n; i += 2 )
if( n % i == 0 )
return false;
return true;
}
/**
* Internal method to return a prime number at least as large as n.
* Assumes n > 0.
*/
template
int QuadraticHashTable::nextPrime( int n ) const
{
if( n % 2 == 0 )
n++;
for( ; !isPrime( n ); n += 2 )
;
return n;
}
/**
* Construct the hash table.
*/
template
QuadraticHashTable::QuadraticHashTable( const HashedObj & notFound, int size )
: array( nextPrime( size ) ), ITEM_NOT_FOUND( notFound )
{
makeEmpty( );
}
/**
* Insert item x into the hash table. If the item is
* already present, then do nothing.
*/
template
void QuadraticHashTable::insert( const HashedObj & x )
{
// Insert x as active
int currentPos = findPos( x );
if( isActive( currentPos ) )
return;
array[ currentPos ] = HashEntry( x, ACTIVE );
// Rehash; see Section 5.5
if( ++currentSize > array.size( ) / 2 )
rehash( );
}
/**
* Expand the hash table.
*/
template
void QuadraticHashTable::rehash( )
{
vector oldArray = array;
// Create new double-sized, empty table
array.resize( nextPrime( 2 * oldArray.size( ) ) );
for( int j = 0; j < array.size( ); j++ )
array[ j ].info = EMPTY;
// Copy table over
currentSize = 0;
for( int i = 0; i < oldArray.size( ); i++ )
if( oldArray[ i ].info == ACTIVE )
insert( oldArray[ i ].element );
}
/**
* Method that performs quadratic probing resolution.
* Return the position where the search for x terminates.
*/
template
int QuadraticHashTable::findPos( const HashedObj & x ) const
{
/* 1*/ int collisionNum = 0;
/* 2*/ int currentPos = hash( x, array.size( ) );
/* 3*/ while( array[ currentPos ].info != EMPTY &&
array[ currentPos ].element != x )
{
/* 4*/ currentPos += 2 * ++collisionNum - 1; // Compute ith probe
/* 5*/ if( currentPos >= array.size( ) )
/* 6*/ currentPos -= array.size( );
}
/* 7*/ return currentPos;
}
/**
* Remove item x from the hash table.
*/
template
void QuadraticHashTable::remove( const HashedObj & x )
{
int currentPos = findPos( x );
if( isActive( currentPos ) )
array[ currentPos ].info = DELETED;
}
/**
* Find item x in the hash table.
* Return the matching item, or ITEM_NOT_FOUND, if not found.
*/
template
const HashedObj & QuadraticHashTable::find( const HashedObj & x ) const
{
int currentPos = findPos( x );
return isActive( currentPos ) ? array[ currentPos ].element : ITEM_NOT_FOUND;
// Above mentions only if active, if it is return array element, else return not found
}
/**
* Make the hash table logically empty.
*/
template
void QuadraticHashTable::makeEmpty( )
{
currentSize = 0;
for( int i = 0; i < array.size( ); i++ )
array[ i ].info = EMPTY;
}
/**
* Deep copy.
*/
template
const QuadraticHashTable & QuadraticHashTable::
operator=( const QuadraticHashTable & rhs )
{
if( this != &rhs )
{
array = rhs.array;
currentSize = rhs.currentSize;
}
return *this;
}
/**
* Return true if currentPos exists and is active.
*/
template
bool QuadraticHashTable::isActive( int currentPos ) const
{
return array[ currentPos ].info == ACTIVE;
}
/**
* A hash routine for string objects.
*/
template
int QuadraticHashTable::hash( const string & key, int tableSize ) const
{
int hashVal = 0;
for( int i = 0; i < key.length( ); i++ )
hashVal = 37 * hashVal + key[ i ];
hashVal %= tableSize;
if( hashVal < 0 )
hashVal += tableSize;
return hashVal;
}
/**
* A hash routine for ints.
*/
template
int QuadraticHashTable::hash( int key, int tableSize ) const
{
if( key < 0 ) key = -key;
return key % tableSize;
}
_____________________________________________________________________________
____________
QuadraticProbing.h
#ifndef _QUADRATIC_PROBING_H_
#define _QUADRATIC_PROBING_H_
#include "vector.h"
#include "mystring.h"
// QuadraticProbing Hash table class
//
// CONSTRUCTION: an initialization for ITEM_NOT_FOUND
// and an approximate initial size or default of 101
//
// ******************PUBLIC OPERATIONS*********************
// void insert( x ) --> Insert x
// void remove( x ) --> Remove x
// Hashable find( x ) --> Return item that matches x
// void makeEmpty( ) --> Remove all items
// int hash( String str, int tableSize )
// --> Static method to hash strings
class QuadraticHashTable
{
public:
explicit QuadraticHashTable( const HashedObj & notFound, int size = 101 );
QuadraticHashTable( const QuadraticHashTable & rhs )
: ITEM_NOT_FOUND( rhs.ITEM_NOT_FOUND ),
array( rhs.array ), currentSize( rhs.currentSize ) { }
const HashedObj & find( const HashedObj & x ) const;
void makeEmpty( );
void insert( const HashedObj & x );
void remove( const HashedObj & x );
const QuadraticHashTable & operator=( const QuadraticHashTable & rhs );
enum EntryType { ACTIVE, EMPTY, DELETED };
private:
struct HashEntry
{
HashedObj element;
EntryType info;
HashEntry( const HashedObj & e = HashedObj( ), EntryType i = EMPTY )
: element( e ), info( i ) { }
};
vector array;
int currentSize;
const HashedObj ITEM_NOT_FOUND;
bool isPrime( int n ) const;
int nextPrime( int n ) const;
bool isActive( int currentPos ) const;
char* findPos( const HashedObj & x ) const;
char* hash( const string & key, int tableSize ) const;
int hash( int key, int tableSize ) const;
void rehash( );
};
#include "QuadraticProbing.cpp"
#endif
_____________________________________________________________________________
____________
Dict-3-4-1.txt (Dictionary)
luov
vwucoxynjm
lsrwwvov
_____________________________________________________________________________
____________
Doc-3-4-1.txt (Checking for misspell words)
lsrwwvov
vwuboxynjm
luov
vwucoxynj
Solution
int main(int argc, char* argv[])
{
char line[80], **dictionary, **document, *dictFilePtr, *docFilePtr;
int *wrongs, *misspelled, misspelledCount = 0, seed, dictSize, docSize,
wrongCount, tempMaxRAM, tempCurrentRAM;
strcpy(line, argv[1]);
strtok(line, "-");
dictSize = atoi(strtok(NULL, "-"));
docSize = atoi(strtok(NULL, "-"));
seed = atoi(strtok(NULL, "."));
dictionary = new char*[dictSize + 3];
dictFilePtr = readDictionary(argv[1], dictionary, dictSize);
document = new char*[docSize + 3];
docFilePtr = readDocument(document, dictSize, docSize, seed);
wrongs = new int[docSize];
readWrongs(wrongs, dictSize, docSize, seed, wrongCount);
misspelled = new int[docSize];
CPUTimer ct;
maxRAM = currentRAM = 0;
Speller *speller = new Speller(dictionary, dictSize);
tempMaxRAM = maxRAM;
tempCurrentRAM = currentRAM;
delete [] dictFilePtr;
maxRAM = tempMaxRAM;
currentRAM = tempCurrentRAM;
speller->check(document, docSize, misspelled, &misspelledCount);
cout << "CPU Time: " << ct.cur_CPUTime() << " Real RAM: " << maxRAM << endl;
checkAnswers(wrongs, wrongCount, misspelled, misspelledCount);
cleanup(dictionary, docFilePtr, document, wrongs, misspelled);
delete speller;
return 0;
} // main()ZZ
[ssdavis@lect1 p4]$ CreateDocs.out
Dictionary size: 3
Document size: 4
Seed: 1
[ssdavis@lect1 p4]$ cat Dict-3-4-1.txt
luov
vwucoxynjm
lsrwwvov
[ssdavis@lect1 p4]
[ssdavis@lect1 p4]$ cat Doc-3-4-1.txt
lsrwwvov
vwuboxynjm
luov
vwucoxynjm
[ssdavis@lect1 p4]
[ssdavis@lect1 p4]$ cat Wrongs-3-4-1.txt
1
[ssdavis@lect1 p4]$
[ssdavis@lect1 p4]$ speller.out Dict-100000-2000000-7.txt
CPU Time: 0.170697 Real RAM: 1230872
[ssdavis@lect1 p4]$

More Related Content

Similar to You are to write an efficient program that will read a dictionary of.pdf

Javascript variables and datatypes
Javascript variables and datatypesJavascript variables and datatypes
Javascript variables and datatypesVarun C M
 
C# 6 and 7 and Futures 20180607
C# 6 and 7 and Futures 20180607C# 6 and 7 and Futures 20180607
C# 6 and 7 and Futures 20180607Kevin Hazzard
 
Assignment 13assg-13.cppAssignment 13assg-13.cpp   @auth.docx
Assignment 13assg-13.cppAssignment 13assg-13.cpp   @auth.docxAssignment 13assg-13.cppAssignment 13assg-13.cpp   @auth.docx
Assignment 13assg-13.cppAssignment 13assg-13.cpp   @auth.docxbraycarissa250
 
Problem 1 Show the comparison of runtime of linear search and binar.pdf
Problem 1 Show the comparison of runtime of linear search and binar.pdfProblem 1 Show the comparison of runtime of linear search and binar.pdf
Problem 1 Show the comparison of runtime of linear search and binar.pdfebrahimbadushata00
 
Introduction to Client-Side Javascript
Introduction to Client-Side JavascriptIntroduction to Client-Side Javascript
Introduction to Client-Side JavascriptJulie Iskander
 
APPLICATION TO DOCUMENT ALL THE DETAILS OF JAVA CLASSES OF A PROJECT AT ONCE...
APPLICATION TO DOCUMENT ALL THE  DETAILS OF JAVA CLASSES OF A PROJECT AT ONCE...APPLICATION TO DOCUMENT ALL THE  DETAILS OF JAVA CLASSES OF A PROJECT AT ONCE...
APPLICATION TO DOCUMENT ALL THE DETAILS OF JAVA CLASSES OF A PROJECT AT ONCE...DEEPANSHU GUPTA
 
2011 Mongo FR - Indexing in MongoDB
2011 Mongo FR - Indexing in MongoDB2011 Mongo FR - Indexing in MongoDB
2011 Mongo FR - Indexing in MongoDBantoinegirbal
 
Library functions in c++
Library functions in c++Library functions in c++
Library functions in c++Neeru Mittal
 
So I already have most of the code and now I have to1. create an .pdf
So I already have most of the code and now I have to1. create an .pdfSo I already have most of the code and now I have to1. create an .pdf
So I already have most of the code and now I have to1. create an .pdfarjuncollection
 
05 JavaScript #burningkeyboards
05 JavaScript #burningkeyboards05 JavaScript #burningkeyboards
05 JavaScript #burningkeyboardsDenis Ristic
 
Hello Everyone!!!I’m writing a c++ program that presents a menu to.pdf
Hello Everyone!!!I’m writing a c++ program that presents a menu to.pdfHello Everyone!!!I’m writing a c++ program that presents a menu to.pdf
Hello Everyone!!!I’m writing a c++ program that presents a menu to.pdfamittripathi2002
 
C++ Language -- Dynamic Memory -- There are 7 files in this project- a.pdf
C++ Language -- Dynamic Memory -- There are 7 files in this project- a.pdfC++ Language -- Dynamic Memory -- There are 7 files in this project- a.pdf
C++ Language -- Dynamic Memory -- There are 7 files in this project- a.pdfaassecuritysystem
 
Data Structure In C#
Data Structure In C#Data Structure In C#
Data Structure In C#Shahzad
 
C++11 - A Change in Style - v2.0
C++11 - A Change in Style - v2.0C++11 - A Change in Style - v2.0
C++11 - A Change in Style - v2.0Yaser Zhian
 
JQuery Presentation
JQuery PresentationJQuery Presentation
JQuery PresentationSony Jain
 
Complete code in Java The hashtable you'll be making will use String.pdf
Complete code in Java   The hashtable you'll be making will use String.pdfComplete code in Java   The hashtable you'll be making will use String.pdf
Complete code in Java The hashtable you'll be making will use String.pdfaarifi9988
 

Similar to You are to write an efficient program that will read a dictionary of.pdf (20)

Javascript variables and datatypes
Javascript variables and datatypesJavascript variables and datatypes
Javascript variables and datatypes
 
C# 6 and 7 and Futures 20180607
C# 6 and 7 and Futures 20180607C# 6 and 7 and Futures 20180607
C# 6 and 7 and Futures 20180607
 
Assignment 13assg-13.cppAssignment 13assg-13.cpp   @auth.docx
Assignment 13assg-13.cppAssignment 13assg-13.cpp   @auth.docxAssignment 13assg-13.cppAssignment 13assg-13.cpp   @auth.docx
Assignment 13assg-13.cppAssignment 13assg-13.cpp   @auth.docx
 
Problem 1 Show the comparison of runtime of linear search and binar.pdf
Problem 1 Show the comparison of runtime of linear search and binar.pdfProblem 1 Show the comparison of runtime of linear search and binar.pdf
Problem 1 Show the comparison of runtime of linear search and binar.pdf
 
Introduction to Client-Side Javascript
Introduction to Client-Side JavascriptIntroduction to Client-Side Javascript
Introduction to Client-Side Javascript
 
APPLICATION TO DOCUMENT ALL THE DETAILS OF JAVA CLASSES OF A PROJECT AT ONCE...
APPLICATION TO DOCUMENT ALL THE  DETAILS OF JAVA CLASSES OF A PROJECT AT ONCE...APPLICATION TO DOCUMENT ALL THE  DETAILS OF JAVA CLASSES OF A PROJECT AT ONCE...
APPLICATION TO DOCUMENT ALL THE DETAILS OF JAVA CLASSES OF A PROJECT AT ONCE...
 
2011 Mongo FR - Indexing in MongoDB
2011 Mongo FR - Indexing in MongoDB2011 Mongo FR - Indexing in MongoDB
2011 Mongo FR - Indexing in MongoDB
 
Library functions in c++
Library functions in c++Library functions in c++
Library functions in c++
 
Hash table (2)
Hash table (2)Hash table (2)
Hash table (2)
 
So I already have most of the code and now I have to1. create an .pdf
So I already have most of the code and now I have to1. create an .pdfSo I already have most of the code and now I have to1. create an .pdf
So I already have most of the code and now I have to1. create an .pdf
 
JavaScript Lessons 2023
JavaScript Lessons 2023JavaScript Lessons 2023
JavaScript Lessons 2023
 
05 JavaScript #burningkeyboards
05 JavaScript #burningkeyboards05 JavaScript #burningkeyboards
05 JavaScript #burningkeyboards
 
Hello Everyone!!!I’m writing a c++ program that presents a menu to.pdf
Hello Everyone!!!I’m writing a c++ program that presents a menu to.pdfHello Everyone!!!I’m writing a c++ program that presents a menu to.pdf
Hello Everyone!!!I’m writing a c++ program that presents a menu to.pdf
 
lab4_php
lab4_phplab4_php
lab4_php
 
lab4_php
lab4_phplab4_php
lab4_php
 
C++ Language -- Dynamic Memory -- There are 7 files in this project- a.pdf
C++ Language -- Dynamic Memory -- There are 7 files in this project- a.pdfC++ Language -- Dynamic Memory -- There are 7 files in this project- a.pdf
C++ Language -- Dynamic Memory -- There are 7 files in this project- a.pdf
 
Data Structure In C#
Data Structure In C#Data Structure In C#
Data Structure In C#
 
C++11 - A Change in Style - v2.0
C++11 - A Change in Style - v2.0C++11 - A Change in Style - v2.0
C++11 - A Change in Style - v2.0
 
JQuery Presentation
JQuery PresentationJQuery Presentation
JQuery Presentation
 
Complete code in Java The hashtable you'll be making will use String.pdf
Complete code in Java   The hashtable you'll be making will use String.pdfComplete code in Java   The hashtable you'll be making will use String.pdf
Complete code in Java The hashtable you'll be making will use String.pdf
 

More from fortmdu

How is a Decision Support Systems (DSS) different from a Management .pdf
How is a Decision Support Systems (DSS) different from a Management .pdfHow is a Decision Support Systems (DSS) different from a Management .pdf
How is a Decision Support Systems (DSS) different from a Management .pdffortmdu
 
I am trying to create a program That works with two other programs i.pdf
I am trying to create a program That works with two other programs i.pdfI am trying to create a program That works with two other programs i.pdf
I am trying to create a program That works with two other programs i.pdffortmdu
 
How does anti-malware software detect virusesWhat techniques are .pdf
How does anti-malware software detect virusesWhat techniques are .pdfHow does anti-malware software detect virusesWhat techniques are .pdf
How does anti-malware software detect virusesWhat techniques are .pdffortmdu
 
How can I upload a picture in here I tried with image properties ic.pdf
How can I upload a picture in here I tried with image properties ic.pdfHow can I upload a picture in here I tried with image properties ic.pdf
How can I upload a picture in here I tried with image properties ic.pdffortmdu
 
Explain the role of ATP in the action of Hsp70 and GroELES.Solu.pdf
Explain the role of ATP in the action of Hsp70 and GroELES.Solu.pdfExplain the role of ATP in the action of Hsp70 and GroELES.Solu.pdf
Explain the role of ATP in the action of Hsp70 and GroELES.Solu.pdffortmdu
 
Files Please respond to the following •Suppose you are creating.pdf
Files Please respond to the following •Suppose you are creating.pdfFiles Please respond to the following •Suppose you are creating.pdf
Files Please respond to the following •Suppose you are creating.pdffortmdu
 
evil_server.cpp#include string #include cstdlib #include.pdf
evil_server.cpp#include string #include cstdlib #include.pdfevil_server.cpp#include string #include cstdlib #include.pdf
evil_server.cpp#include string #include cstdlib #include.pdffortmdu
 
ecorrect Question 32 0 1 pts Referencing the diagram above, use the .pdf
ecorrect Question 32 0 1 pts Referencing the diagram above, use the .pdfecorrect Question 32 0 1 pts Referencing the diagram above, use the .pdf
ecorrect Question 32 0 1 pts Referencing the diagram above, use the .pdffortmdu
 
Disneys Expedition EverestOne of the newest thrill rides to open.pdf
Disneys Expedition EverestOne of the newest thrill rides to open.pdfDisneys Expedition EverestOne of the newest thrill rides to open.pdf
Disneys Expedition EverestOne of the newest thrill rides to open.pdffortmdu
 
Discuss ONE risk that a company faces when trying to diversify inte.pdf
Discuss ONE risk that a company faces when trying to diversify inte.pdfDiscuss ONE risk that a company faces when trying to diversify inte.pdf
Discuss ONE risk that a company faces when trying to diversify inte.pdffortmdu
 
Describe at least one reason why transitioning from PVST+ to Rapid P.pdf
Describe at least one reason why transitioning from PVST+ to Rapid P.pdfDescribe at least one reason why transitioning from PVST+ to Rapid P.pdf
Describe at least one reason why transitioning from PVST+ to Rapid P.pdffortmdu
 
CASE 2-1 BUILDING UP OUR ASSETS DHR CONSTRUCTIONIn August 2011, w.pdf
CASE 2-1 BUILDING UP OUR ASSETS DHR CONSTRUCTIONIn August 2011, w.pdfCASE 2-1 BUILDING UP OUR ASSETS DHR CONSTRUCTIONIn August 2011, w.pdf
CASE 2-1 BUILDING UP OUR ASSETS DHR CONSTRUCTIONIn August 2011, w.pdffortmdu
 
C++ Write a function that takes two numbers are parameters and retu.pdf
C++ Write a function that takes two numbers are parameters and retu.pdfC++ Write a function that takes two numbers are parameters and retu.pdf
C++ Write a function that takes two numbers are parameters and retu.pdffortmdu
 
B.1 Reaction of a Hydrate Addition of Water (2) Appearance Heating (1.pdf
B.1 Reaction of a Hydrate Addition of Water (2) Appearance Heating (1.pdfB.1 Reaction of a Hydrate Addition of Water (2) Appearance Heating (1.pdf
B.1 Reaction of a Hydrate Addition of Water (2) Appearance Heating (1.pdffortmdu
 
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdfAssignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdffortmdu
 
You maintain several virtual machines (VMs) in an offline state. How.pdf
You maintain several virtual machines (VMs) in an offline state. How.pdfYou maintain several virtual machines (VMs) in an offline state. How.pdf
You maintain several virtual machines (VMs) in an offline state. How.pdffortmdu
 
Which of the following statements is not TRUE1)For a 64-bit compute.pdf
Which of the following statements is not TRUE1)For a 64-bit compute.pdfWhich of the following statements is not TRUE1)For a 64-bit compute.pdf
Which of the following statements is not TRUE1)For a 64-bit compute.pdffortmdu
 
You are running an ELISA on a sample to test for the presence of .pdf
You are running an ELISA on a sample to test for the presence of .pdfYou are running an ELISA on a sample to test for the presence of .pdf
You are running an ELISA on a sample to test for the presence of .pdffortmdu
 
X = C B - B C D; Use Accumulator Register-Register (LoadSt.pdf
X = C B - B  C  D; Use Accumulator  Register-Register (LoadSt.pdfX = C B - B  C  D; Use Accumulator  Register-Register (LoadSt.pdf
X = C B - B C D; Use Accumulator Register-Register (LoadSt.pdffortmdu
 
Tic-Tac-Toe Simulator [C# Visual Basic] Create an application th.pdf
Tic-Tac-Toe Simulator [C# Visual Basic] Create an application th.pdfTic-Tac-Toe Simulator [C# Visual Basic] Create an application th.pdf
Tic-Tac-Toe Simulator [C# Visual Basic] Create an application th.pdffortmdu
 

More from fortmdu (20)

How is a Decision Support Systems (DSS) different from a Management .pdf
How is a Decision Support Systems (DSS) different from a Management .pdfHow is a Decision Support Systems (DSS) different from a Management .pdf
How is a Decision Support Systems (DSS) different from a Management .pdf
 
I am trying to create a program That works with two other programs i.pdf
I am trying to create a program That works with two other programs i.pdfI am trying to create a program That works with two other programs i.pdf
I am trying to create a program That works with two other programs i.pdf
 
How does anti-malware software detect virusesWhat techniques are .pdf
How does anti-malware software detect virusesWhat techniques are .pdfHow does anti-malware software detect virusesWhat techniques are .pdf
How does anti-malware software detect virusesWhat techniques are .pdf
 
How can I upload a picture in here I tried with image properties ic.pdf
How can I upload a picture in here I tried with image properties ic.pdfHow can I upload a picture in here I tried with image properties ic.pdf
How can I upload a picture in here I tried with image properties ic.pdf
 
Explain the role of ATP in the action of Hsp70 and GroELES.Solu.pdf
Explain the role of ATP in the action of Hsp70 and GroELES.Solu.pdfExplain the role of ATP in the action of Hsp70 and GroELES.Solu.pdf
Explain the role of ATP in the action of Hsp70 and GroELES.Solu.pdf
 
Files Please respond to the following •Suppose you are creating.pdf
Files Please respond to the following •Suppose you are creating.pdfFiles Please respond to the following •Suppose you are creating.pdf
Files Please respond to the following •Suppose you are creating.pdf
 
evil_server.cpp#include string #include cstdlib #include.pdf
evil_server.cpp#include string #include cstdlib #include.pdfevil_server.cpp#include string #include cstdlib #include.pdf
evil_server.cpp#include string #include cstdlib #include.pdf
 
ecorrect Question 32 0 1 pts Referencing the diagram above, use the .pdf
ecorrect Question 32 0 1 pts Referencing the diagram above, use the .pdfecorrect Question 32 0 1 pts Referencing the diagram above, use the .pdf
ecorrect Question 32 0 1 pts Referencing the diagram above, use the .pdf
 
Disneys Expedition EverestOne of the newest thrill rides to open.pdf
Disneys Expedition EverestOne of the newest thrill rides to open.pdfDisneys Expedition EverestOne of the newest thrill rides to open.pdf
Disneys Expedition EverestOne of the newest thrill rides to open.pdf
 
Discuss ONE risk that a company faces when trying to diversify inte.pdf
Discuss ONE risk that a company faces when trying to diversify inte.pdfDiscuss ONE risk that a company faces when trying to diversify inte.pdf
Discuss ONE risk that a company faces when trying to diversify inte.pdf
 
Describe at least one reason why transitioning from PVST+ to Rapid P.pdf
Describe at least one reason why transitioning from PVST+ to Rapid P.pdfDescribe at least one reason why transitioning from PVST+ to Rapid P.pdf
Describe at least one reason why transitioning from PVST+ to Rapid P.pdf
 
CASE 2-1 BUILDING UP OUR ASSETS DHR CONSTRUCTIONIn August 2011, w.pdf
CASE 2-1 BUILDING UP OUR ASSETS DHR CONSTRUCTIONIn August 2011, w.pdfCASE 2-1 BUILDING UP OUR ASSETS DHR CONSTRUCTIONIn August 2011, w.pdf
CASE 2-1 BUILDING UP OUR ASSETS DHR CONSTRUCTIONIn August 2011, w.pdf
 
C++ Write a function that takes two numbers are parameters and retu.pdf
C++ Write a function that takes two numbers are parameters and retu.pdfC++ Write a function that takes two numbers are parameters and retu.pdf
C++ Write a function that takes two numbers are parameters and retu.pdf
 
B.1 Reaction of a Hydrate Addition of Water (2) Appearance Heating (1.pdf
B.1 Reaction of a Hydrate Addition of Water (2) Appearance Heating (1.pdfB.1 Reaction of a Hydrate Addition of Water (2) Appearance Heating (1.pdf
B.1 Reaction of a Hydrate Addition of Water (2) Appearance Heating (1.pdf
 
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdfAssignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
 
You maintain several virtual machines (VMs) in an offline state. How.pdf
You maintain several virtual machines (VMs) in an offline state. How.pdfYou maintain several virtual machines (VMs) in an offline state. How.pdf
You maintain several virtual machines (VMs) in an offline state. How.pdf
 
Which of the following statements is not TRUE1)For a 64-bit compute.pdf
Which of the following statements is not TRUE1)For a 64-bit compute.pdfWhich of the following statements is not TRUE1)For a 64-bit compute.pdf
Which of the following statements is not TRUE1)For a 64-bit compute.pdf
 
You are running an ELISA on a sample to test for the presence of .pdf
You are running an ELISA on a sample to test for the presence of .pdfYou are running an ELISA on a sample to test for the presence of .pdf
You are running an ELISA on a sample to test for the presence of .pdf
 
X = C B - B C D; Use Accumulator Register-Register (LoadSt.pdf
X = C B - B  C  D; Use Accumulator  Register-Register (LoadSt.pdfX = C B - B  C  D; Use Accumulator  Register-Register (LoadSt.pdf
X = C B - B C D; Use Accumulator Register-Register (LoadSt.pdf
 
Tic-Tac-Toe Simulator [C# Visual Basic] Create an application th.pdf
Tic-Tac-Toe Simulator [C# Visual Basic] Create an application th.pdfTic-Tac-Toe Simulator [C# Visual Basic] Create an application th.pdf
Tic-Tac-Toe Simulator [C# Visual Basic] Create an application th.pdf
 

Recently uploaded

Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 

Recently uploaded (20)

Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 

You are to write an efficient program that will read a dictionary of.pdf

  • 1. You are to write an efficient program that will read a dictionary of 100,000 words, and then check a document of 2,000,000 words. The program should insert into an array the position of each misspelled word. I have provided the driver program SpellRunner.cpp, and the necessary class stubs in speller.h, and speller.cpp. You may add any classes and/or code you wish to speller.cpp, and speller.h. You may also use, and alter any Weiss files. CreateDocs.out creates files used for testing. Doc-3-4-1 and Dict-3-4-1 are two testing files for small file. _____________________________________________________________________________ _________________ SpellRunner.cpp #include #include #include #include #include #include "CPUTimer.h" #include "mynew.h" #include "speller.h" // uncomment line below for DOS/Windows OS. // #define DOS #ifdef DOS #include #else #include #endif extern int maxRAM; extern int currentRAM; using namespace std; char* readDictionary(char *filename, char **dictionary, int dictSize) { int fileSize; char *s; struct stat statbuf; stat(filename, &statbuf); fileSize = statbuf.st_size; s = new char[fileSize + 1];
  • 2. ifstream inf(filename); inf.read(s, fileSize); inf.close(); s[fileSize] = '0'; dictionary[0] = strtok(s, " "); for(int i = 1; i < dictSize; i++) dictionary[i] = strtok(NULL, " "); return s; } // readDictionary char* readDocument(char **document, int dictSize, int docSize, int seed) { char *s, filename[80]; int fileSize; struct stat statbuf; sprintf(filename, "Doc-%d-%d-%d.txt", dictSize, docSize, seed); stat(filename, &statbuf); fileSize = statbuf.st_size; s = new char[fileSize + 100]; ifstream inf(filename); inf.read(s, fileSize); inf.close(); s[fileSize] = '0'; document[0] = strtok(s, " "); for(int i = 1; i < docSize; i++) document[i] = strtok(NULL, " "); return s; } // readDocument() void readWrongs(int *wrongs, int dictSize, int docSize, int seed, int &wrongCount) { char filename[80]; wrongCount = 0; sprintf(filename, "Wrongs-%d-%d-%d.txt", dictSize, docSize, seed); ifstream inf(filename); while(inf >> wrongs[wrongCount++]); wrongCount--;
  • 3. } // readWrongs() void checkAnswers(int wrongs[], int wrongCount, int misspelled[], int misspelledCount) { for(int i = 0; i < wrongCount && i < misspelledCount; i++) if(wrongs[i] < misspelled[i]) { cout << "Speller missed misspelled word # " << wrongs[i] << endl; return; } else if(wrongs[i] > misspelled[i]) { cout << "Speller thought correctly spelled word # " << misspelled[i] << " was wrong "; return; } if(wrongCount != misspelledCount) cout << "Speller found " << misspelledCount << " misspelled words when " << " there were really " << wrongCount << " misspelled words. "; } // checkAnswers void cleanup(char **dictionary, char *docFilePtr, char **document, int *wrongs, int *misspelled) { delete [] dictionary; delete [] docFilePtr; delete [] document; delete [] wrongs; delete [] misspelled; } // cleanup() int main(int argc, char* argv[]) { char line[80], **dictionary, **document, *dictFilePtr, *docFilePtr; int *wrongs, *misspelled, misspelledCount = 0, seed, dictSize, docSize,
  • 4. wrongCount, tempMaxRAM, tempCurrentRAM; strcpy(line, argv[1]); strtok(line, "-"); dictSize = atoi(strtok(NULL, "-")); docSize = atoi(strtok(NULL, "-")); seed = atoi(strtok(NULL, ".")); dictionary = new char*[dictSize + 3]; dictFilePtr = readDictionary(argv[1], dictionary, dictSize); document = new char*[docSize + 3]; docFilePtr = readDocument(document, dictSize, docSize, seed); wrongs = new int[docSize]; readWrongs(wrongs, dictSize, docSize, seed, wrongCount); misspelled = new int[docSize]; CPUTimer ct; maxRAM = currentRAM = 0; Speller *speller = new Speller(dictionary, dictSize); tempMaxRAM = maxRAM; tempCurrentRAM = currentRAM; delete [] dictFilePtr; maxRAM = tempMaxRAM; currentRAM = tempCurrentRAM; speller->check(document, docSize, misspelled, &misspelledCount); cout << "CPU Time: " << ct.cur_CPUTime() << " Real RAM: " << maxRAM << endl; checkAnswers(wrongs, wrongCount, misspelled, misspelledCount); cleanup(dictionary, docFilePtr, document, wrongs, misspelled); delete speller; return 0; } // main() _____________________________________________________________________________ _________________ Speller.h #ifndef SPELLER_H #define SPELLER_H #include "mynew.h" #include "QuadraticProbing.h" #include
  • 5. class Speller { public: QuadraticHashTable *hashDict; Speller(char *dictionary[], int dictSize); ~Speller(); void check(char *document[], int docSize, int misspelled[], int *misspelledCount); }; // class Speller #endif _____________________________________________________________________________ _________________ Speller.cpp #include "speller.h" Speller::Speller(char *dictionary[], int dictSize) { hashDict = new QuadraticHashTable (-1, dictSize); for(int i = 0; i < dictSize; i++) hashDict->insert(dictionary[i]); } // Speller() void Speller::check(char *document[], int docSize, int misspelled[], int *misspelledCount) { *misspelledCount = 0; //Check document array using dictionary that you stored in constructor for(int i = 0; i < docSize; i++) { if(!(hashDict->find(document[i]))) { ++(*misspelledCount); misspelled[i] = 1; } else { misspelled[i] = 0; }
  • 6. } //Store the array index of the misspelled word in the corresponding array and increments the count of misspelled words } // check() Speller::~Speller() { } // ~Speller() _____________________________________________________________________________ ____________ QuadraticProbing.cpp #include "QuadraticProbing.h" /** * Internal method to test if a positive number is prime. * Not an efficient algorithm. */ template bool QuadraticHashTable::isPrime( int n ) const { if( n == 2 || n == 3 ) return true; if( n == 1 || n % 2 == 0 ) return false; for( int i = 3; i * i <= n; i += 2 ) if( n % i == 0 ) return false; return true; } /** * Internal method to return a prime number at least as large as n. * Assumes n > 0. */ template int QuadraticHashTable::nextPrime( int n ) const {
  • 7. if( n % 2 == 0 ) n++; for( ; !isPrime( n ); n += 2 ) ; return n; } /** * Construct the hash table. */ template QuadraticHashTable::QuadraticHashTable( const HashedObj & notFound, int size ) : array( nextPrime( size ) ), ITEM_NOT_FOUND( notFound ) { makeEmpty( ); } /** * Insert item x into the hash table. If the item is * already present, then do nothing. */ template void QuadraticHashTable::insert( const HashedObj & x ) { // Insert x as active int currentPos = findPos( x ); if( isActive( currentPos ) ) return; array[ currentPos ] = HashEntry( x, ACTIVE ); // Rehash; see Section 5.5 if( ++currentSize > array.size( ) / 2 ) rehash( ); } /** * Expand the hash table. */ template void QuadraticHashTable::rehash( )
  • 8. { vector oldArray = array; // Create new double-sized, empty table array.resize( nextPrime( 2 * oldArray.size( ) ) ); for( int j = 0; j < array.size( ); j++ ) array[ j ].info = EMPTY; // Copy table over currentSize = 0; for( int i = 0; i < oldArray.size( ); i++ ) if( oldArray[ i ].info == ACTIVE ) insert( oldArray[ i ].element ); } /** * Method that performs quadratic probing resolution. * Return the position where the search for x terminates. */ template int QuadraticHashTable::findPos( const HashedObj & x ) const { /* 1*/ int collisionNum = 0; /* 2*/ int currentPos = hash( x, array.size( ) ); /* 3*/ while( array[ currentPos ].info != EMPTY && array[ currentPos ].element != x ) { /* 4*/ currentPos += 2 * ++collisionNum - 1; // Compute ith probe /* 5*/ if( currentPos >= array.size( ) ) /* 6*/ currentPos -= array.size( ); } /* 7*/ return currentPos; } /** * Remove item x from the hash table. */ template void QuadraticHashTable::remove( const HashedObj & x )
  • 9. { int currentPos = findPos( x ); if( isActive( currentPos ) ) array[ currentPos ].info = DELETED; } /** * Find item x in the hash table. * Return the matching item, or ITEM_NOT_FOUND, if not found. */ template const HashedObj & QuadraticHashTable::find( const HashedObj & x ) const { int currentPos = findPos( x ); return isActive( currentPos ) ? array[ currentPos ].element : ITEM_NOT_FOUND; // Above mentions only if active, if it is return array element, else return not found } /** * Make the hash table logically empty. */ template void QuadraticHashTable::makeEmpty( ) { currentSize = 0; for( int i = 0; i < array.size( ); i++ ) array[ i ].info = EMPTY; } /** * Deep copy. */ template const QuadraticHashTable & QuadraticHashTable:: operator=( const QuadraticHashTable & rhs ) { if( this != &rhs ) { array = rhs.array;
  • 10. currentSize = rhs.currentSize; } return *this; } /** * Return true if currentPos exists and is active. */ template bool QuadraticHashTable::isActive( int currentPos ) const { return array[ currentPos ].info == ACTIVE; } /** * A hash routine for string objects. */ template int QuadraticHashTable::hash( const string & key, int tableSize ) const { int hashVal = 0; for( int i = 0; i < key.length( ); i++ ) hashVal = 37 * hashVal + key[ i ]; hashVal %= tableSize; if( hashVal < 0 ) hashVal += tableSize; return hashVal; } /** * A hash routine for ints. */ template int QuadraticHashTable::hash( int key, int tableSize ) const { if( key < 0 ) key = -key; return key % tableSize;
  • 11. } _____________________________________________________________________________ ____________ QuadraticProbing.h #ifndef _QUADRATIC_PROBING_H_ #define _QUADRATIC_PROBING_H_ #include "vector.h" #include "mystring.h" // QuadraticProbing Hash table class // // CONSTRUCTION: an initialization for ITEM_NOT_FOUND // and an approximate initial size or default of 101 // // ******************PUBLIC OPERATIONS********************* // void insert( x ) --> Insert x // void remove( x ) --> Remove x // Hashable find( x ) --> Return item that matches x // void makeEmpty( ) --> Remove all items // int hash( String str, int tableSize ) // --> Static method to hash strings class QuadraticHashTable { public: explicit QuadraticHashTable( const HashedObj & notFound, int size = 101 ); QuadraticHashTable( const QuadraticHashTable & rhs ) : ITEM_NOT_FOUND( rhs.ITEM_NOT_FOUND ), array( rhs.array ), currentSize( rhs.currentSize ) { } const HashedObj & find( const HashedObj & x ) const; void makeEmpty( ); void insert( const HashedObj & x ); void remove( const HashedObj & x ); const QuadraticHashTable & operator=( const QuadraticHashTable & rhs ); enum EntryType { ACTIVE, EMPTY, DELETED }; private: struct HashEntry
  • 12. { HashedObj element; EntryType info; HashEntry( const HashedObj & e = HashedObj( ), EntryType i = EMPTY ) : element( e ), info( i ) { } }; vector array; int currentSize; const HashedObj ITEM_NOT_FOUND; bool isPrime( int n ) const; int nextPrime( int n ) const; bool isActive( int currentPos ) const; char* findPos( const HashedObj & x ) const; char* hash( const string & key, int tableSize ) const; int hash( int key, int tableSize ) const; void rehash( ); }; #include "QuadraticProbing.cpp" #endif _____________________________________________________________________________ ____________ Dict-3-4-1.txt (Dictionary) luov vwucoxynjm lsrwwvov _____________________________________________________________________________ ____________ Doc-3-4-1.txt (Checking for misspell words) lsrwwvov vwuboxynjm luov vwucoxynj Solution int main(int argc, char* argv[])
  • 13. { char line[80], **dictionary, **document, *dictFilePtr, *docFilePtr; int *wrongs, *misspelled, misspelledCount = 0, seed, dictSize, docSize, wrongCount, tempMaxRAM, tempCurrentRAM; strcpy(line, argv[1]); strtok(line, "-"); dictSize = atoi(strtok(NULL, "-")); docSize = atoi(strtok(NULL, "-")); seed = atoi(strtok(NULL, ".")); dictionary = new char*[dictSize + 3]; dictFilePtr = readDictionary(argv[1], dictionary, dictSize); document = new char*[docSize + 3]; docFilePtr = readDocument(document, dictSize, docSize, seed); wrongs = new int[docSize]; readWrongs(wrongs, dictSize, docSize, seed, wrongCount); misspelled = new int[docSize]; CPUTimer ct; maxRAM = currentRAM = 0; Speller *speller = new Speller(dictionary, dictSize); tempMaxRAM = maxRAM; tempCurrentRAM = currentRAM; delete [] dictFilePtr; maxRAM = tempMaxRAM; currentRAM = tempCurrentRAM; speller->check(document, docSize, misspelled, &misspelledCount); cout << "CPU Time: " << ct.cur_CPUTime() << " Real RAM: " << maxRAM << endl; checkAnswers(wrongs, wrongCount, misspelled, misspelledCount); cleanup(dictionary, docFilePtr, document, wrongs, misspelled); delete speller; return 0; } // main()ZZ [ssdavis@lect1 p4]$ CreateDocs.out Dictionary size: 3 Document size: 4 Seed: 1 [ssdavis@lect1 p4]$ cat Dict-3-4-1.txt
  • 14. luov vwucoxynjm lsrwwvov [ssdavis@lect1 p4] [ssdavis@lect1 p4]$ cat Doc-3-4-1.txt lsrwwvov vwuboxynjm luov vwucoxynjm [ssdavis@lect1 p4] [ssdavis@lect1 p4]$ cat Wrongs-3-4-1.txt 1 [ssdavis@lect1 p4]$ [ssdavis@lect1 p4]$ speller.out Dict-100000-2000000-7.txt CPU Time: 0.170697 Real RAM: 1230872 [ssdavis@lect1 p4]$